diff --git a/src/controllers/wishlistController.ts b/src/controllers/wishlistController.ts index a0fda075..091e46fa 100644 --- a/src/controllers/wishlistController.ts +++ b/src/controllers/wishlistController.ts @@ -7,13 +7,13 @@ import User from '../database/models/user'; export const addToWishlist = async (req: Request, res: Response) => { try { - const { id } = req.params; - const user = req.user as User; + const { sizeId } = req.params; + const { id } = req.user as User; const itemExist = await Wishlist.findOne({ where: { - userId: user.id, - sizeId: id, + userId: id, + sizeId: sizeId, }, }); if (itemExist) { @@ -23,8 +23,8 @@ export const addToWishlist = async (req: Request, res: Response) => { }); } await Wishlist.create({ - userId: user.id, - sizeId: id, + userId: id, + sizeId: sizeId, }); return res.status(201).json({ diff --git a/src/docs/wishlist.yaml b/src/docs/wishlist.yaml index a0dd9ec2..f059b687 100644 --- a/src/docs/wishlist.yaml +++ b/src/docs/wishlist.yaml @@ -1,44 +1,49 @@ tags: - name: Wishlist - description: Buyer Wishlist + description: Operations related to buyer wishlist paths: - /api/wishlist/add-wishlist/{id}: + /api/wishlist/add-wishlist/{sizeId}: post: - summary: Add product to wishlist - tags: - - Wishlist + summary: Add a product to the wishlist security: - bearerAuth: [] + tags: + - Wishlist parameters: - in: path - name: id - require: true - type: string + name: sizeId + required: true + description: The ID of the size of the product to add to the wishlist + schema: + type: string responses: 201: description: Product added to wishlist successfully - schema: - type: object - properties: - ok: - type: boolean - message: - type: string - data: - $ref: '#/definitions/WishlistItem' + content: + application/json: + schema: + type: object + properties: + ok: + type: boolean + message: + type: string 400: - description: Product not found - schema: - $ref: '#/definitions/Error' + description: Product already in wishlist or invalid size ID + content: + application/json: + schema: + $ref: '#/definitions/Error' 500: description: Internal Server Error - schema: - $ref: '#/definitions/Error' - + content: + application/json: + schema: + $ref: '#/definitions/Error' /api/wishlist/get-wishlist: get: - summary: Get user's wishlist + summary: Get the user's wishlist tags: - Wishlist security: @@ -46,25 +51,29 @@ paths: responses: 200: description: Wishlist fetched successfully - schema: - type: object - properties: - ok: - type: boolean - message: - type: string - wishlistItems: - type: array - items: - $ref: '#/definitions/WishlistItem' + content: + application/json: + schema: + type: object + properties: + ok: + type: boolean + message: + type: string + wishlistItems: + type: array + items: + $ref: '#/definitions/WishlistItem' 500: description: Internal Server Error - schema: - $ref: '#/definitions/Error' + content: + application/json: + schema: + $ref: '#/definitions/Error' /api/wishlist: delete: - summary: Clear user's wishlist + summary: Clear the user's wishlist tags: - Wishlist security: @@ -72,21 +81,25 @@ paths: responses: 200: description: Wishlist cleared successfully - schema: - type: object - properties: - ok: - type: boolean - message: - type: string + content: + application/json: + schema: + type: object + properties: + ok: + type: boolean + message: + type: string 500: description: Internal Server Error - schema: - $ref: '#/definitions/Error' + content: + application/json: + schema: + $ref: '#/definitions/Error' - /api/wishlist/{id}: + /api/wishlist/item/{id}: delete: - summary: Delete a product from wishlist + summary: Delete a product from the wishlist tags: - Wishlist security: @@ -94,23 +107,28 @@ paths: parameters: - in: path name: id - description: wishList item id + description: The ID of the wishlist item to delete required: true - type: string + schema: + type: string responses: 200: description: Wishlist item deleted successfully - schema: - type: object - properties: - ok: - type: boolean - message: - type: string + content: + application/json: + schema: + type: object + properties: + ok: + type: boolean + message: + type: string 500: description: Internal Server Error - schema: - $ref: '#/definitions/Error' + content: + application/json: + schema: + $ref: '#/definitions/Error' definitions: WishlistItem: diff --git a/src/middlewares/authMiddlewares.ts b/src/middlewares/authMiddlewares.ts index a2e93c5f..80e86bb5 100644 --- a/src/middlewares/authMiddlewares.ts +++ b/src/middlewares/authMiddlewares.ts @@ -13,8 +13,9 @@ config(); export const isAuthenticated = async (req: Request, res: Response, next: NextFunction) => { try { - const token = req.headers.authorization ?? req.params.token; - + const token = req.headers.authorization?.includes('Bearer') + ? req.headers.authorization.split(' ')[1] + : req.headers.authorization ?? req.params.token; if (!token) { logger.error('Authentication required.'); return res.status(401).json({ message: 'Authentication required.' }); diff --git a/src/routes/wishlistRoute.ts b/src/routes/wishlistRoute.ts index e479f2da..120f5038 100644 --- a/src/routes/wishlistRoute.ts +++ b/src/routes/wishlistRoute.ts @@ -4,9 +4,9 @@ import { isAuthenticated, checkUserRoles } from '../middlewares/authMiddlewares' const router = Router(); -router.post('/add-wishlist/:id', isAuthenticated, checkUserRoles('buyer'), addToWishlist); +router.post('/add-wishlist/:sizeId', isAuthenticated, checkUserRoles('buyer'), addToWishlist); router.get('/get-wishlist', isAuthenticated, checkUserRoles('buyer'), getWishlist); -router.delete('/:id', isAuthenticated, checkUserRoles('buyer'), deleteWishlistItem); -router.delete('/', isAuthenticated, checkUserRoles('buyer'), clearWishList); +router.delete('/item/:id', isAuthenticated, checkUserRoles('buyer'), deleteWishlistItem); +router.delete('/clear', isAuthenticated, checkUserRoles('buyer'), clearWishList); export default router;