diff --git a/Backend/src/models/prices/prices.router.ts b/Backend/src/models/prices/prices.router.ts index 12ffaf1..047ce1b 100644 --- a/Backend/src/models/prices/prices.router.ts +++ b/Backend/src/models/prices/prices.router.ts @@ -6,6 +6,7 @@ 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'; /** @@ -100,3 +101,28 @@ 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.'})); + } +}); diff --git a/Backend/src/models/prices/prices.service.ts b/Backend/src/models/prices/prices.service.ts index 405cfd4..0a5ab2a 100644 --- a/Backend/src/models/prices/prices.service.ts +++ b/Backend/src/models/prices/prices.service.ts @@ -376,3 +376,29 @@ export const findListByProducts = async (productIds: [number]): Promise return priceRows; }; + +export const createPriceEntry = async (user_id: number, vendor_id: number, product_id: number, price_in_cents: number): Promise => { + 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(); + } + } +}; diff --git a/Backend/src/models/vendors/vendors.router.ts b/Backend/src/models/vendors/vendors.router.ts index 7ca180d..7440f4f 100644 --- a/Backend/src/models/vendors/vendors.router.ts +++ b/Backend/src/models/vendors/vendors.router.ts @@ -99,7 +99,7 @@ vendorsRouter.put('/manage/deactivatelisting', async (req: Request, res: Respons const success = await VendorService.deactivateListing(user.user_id, vendor_id, product_id); - if(success) { + if (success) { res.sendStatus(200); } else { res.sendStatus(500); diff --git a/Backend/src/models/vendors/vendors.service.ts b/Backend/src/models/vendors/vendors.service.ts index 1dc2220..39fee7d 100644 --- a/Backend/src/models/vendors/vendors.service.ts +++ b/Backend/src/models/vendors/vendors.service.ts @@ -171,10 +171,7 @@ 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]); - if(status.affectedRows > 0){ - return true; - } - return false; + return status.affectedRows > 0; } catch (err) { throw err; } finally {