mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2024-12-23 04:05:12 +00:00
Compare commits
No commits in common. "8f17ae789699650a8f6b2eaee82f03879154b88f" and "061d1a46e00604b888878303fe1a42d0e96c5610" have entirely different histories.
8f17ae7896
...
061d1a46e0
|
@ -6,7 +6,6 @@ import express, {Request, Response} from 'express';
|
|||
import * as PriceService from './prices.service';
|
||||
import {Price} from './price.interface';
|
||||
import {Prices} from './prices.interface';
|
||||
import * as UserService from '../users/users.service';
|
||||
|
||||
|
||||
/**
|
||||
|
@ -101,28 +100,3 @@ pricesRouter.get('/byProduct/list/:ids', async (req: Request, res: Response) =>
|
|||
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||
}
|
||||
});
|
||||
|
||||
// POST prices/
|
||||
pricesRouter.post('/', async (req: Request, res: Response) => {
|
||||
try {
|
||||
// Authenticate user
|
||||
const user_ip = req.connection.remoteAddress ?? '';
|
||||
const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip);
|
||||
|
||||
// Get required parameters
|
||||
const vendor_id = req.body.vendor_id;
|
||||
const product_id = req.body.product_id;
|
||||
const price_in_cents = req.body.price_in_cents;
|
||||
|
||||
const success = await PriceService.createPriceEntry(user.user_id, vendor_id, product_id, price_in_cents);
|
||||
|
||||
if (success) {
|
||||
res.sendStatus(200);
|
||||
} else {
|
||||
res.sendStatus(500);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Error handling a request: ' + e.message);
|
||||
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||
}
|
||||
});
|
||||
|
|
|
@ -376,29 +376,3 @@ export const findListByProducts = async (productIds: [number]): Promise<Prices>
|
|||
|
||||
return priceRows;
|
||||
};
|
||||
|
||||
export const createPriceEntry = async (user_id: number, vendor_id: number, product_id: number, price_in_cents: number): Promise<Boolean> => {
|
||||
let conn;
|
||||
try {
|
||||
conn = await pool.getConnection();
|
||||
|
||||
// Check if the user is authorized to manage the requested vendor
|
||||
const user_vendor_rows = await conn.query('SELECT vendor_id FROM vendors WHERE vendor_id = ? AND admin_id = ?', [vendor_id, user_id]);
|
||||
if (user_vendor_rows.length !== 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create price entry
|
||||
const res = await conn.query('INSERT INTO prices (product_id, vendor_id, price_in_cents) VALUES (?,?,?)', [product_id, vendor_id, price_in_cents]);
|
||||
|
||||
// If there are more / less than 1 affected rows, return false
|
||||
return res.affectedRows === 1;
|
||||
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -87,22 +87,3 @@ productsRouter.get('/list/:ids', async (req: Request, res: Response) => {
|
|||
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||
}
|
||||
});
|
||||
|
||||
// GET products/vendor/:id
|
||||
productsRouter.get('/vendor/:id', async (req: Request, res: Response) => {
|
||||
const id: number = parseInt(req.params.id, 10);
|
||||
|
||||
if (!id) {
|
||||
res.status(400).send('Missing parameters.');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const products: Products = await ProductService.findByVendor(id);
|
||||
|
||||
res.status(200).send(products);
|
||||
} catch (e) {
|
||||
console.log('Error handling a request: ' + e.message);
|
||||
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||
}
|
||||
});
|
||||
|
|
|
@ -159,41 +159,3 @@ export const findList = async (ids: [number]): Promise<Products> => {
|
|||
|
||||
return prodRows;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches and returns the products that the given vendor has price entries for
|
||||
* @param id The id of the vendor to fetch the products for
|
||||
*/
|
||||
export const findByVendor = async (id: number): Promise<Products> => {
|
||||
let conn;
|
||||
let prodRows = [];
|
||||
try {
|
||||
conn = await pool.getConnection();
|
||||
|
||||
// Get the relevant product ids
|
||||
let relevant_prod_ids = [];
|
||||
const relevantProds = await conn.query('SELECT product_id FROM prices WHERE vendor_id = ? GROUP BY product_id', id);
|
||||
for (let row in relevantProds) {
|
||||
if (row !== 'meta') {
|
||||
relevant_prod_ids.push(relevantProds[row].product_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch products
|
||||
const rows = await conn.query('SELECT product_id, name, asin, is_active, short_description, long_description, image_guid, date_added, last_modified, manufacturer_id, selling_rank, category_id FROM products WHERE product_id IN (?)', [relevant_prod_ids]);
|
||||
for (let row in rows) {
|
||||
if (row !== 'meta') {
|
||||
prodRows.push(rows[row]);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
|
||||
return prodRows;
|
||||
};
|
||||
|
|
|
@ -171,7 +171,10 @@ export const deactivateListing = async (user_id: number, vendor_id: number, prod
|
|||
|
||||
const status = await conn.query('UPDATE prices SET active_listing = false WHERE vendor_id = ? and product_id = ?', [vendor_id, product_id]);
|
||||
|
||||
return status.affectedRows > 0;
|
||||
if(status.affectedRows > 0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
|
|
Loading…
Reference in New Issue
Block a user