Skip to content

Commit

Permalink
Merge pull request #89 from atlp-rwanda/fix-wishlist-bug
Browse files Browse the repository at this point in the history
Fixing wishlist bug
  • Loading branch information
niyontwali authored Jul 3, 2024
2 parents 44883ed + a106da5 commit fe09130
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 71 deletions.
12 changes: 6 additions & 6 deletions src/controllers/wishlistController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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({
Expand Down
138 changes: 78 additions & 60 deletions src/docs/wishlist.yaml
Original file line number Diff line number Diff line change
@@ -1,116 +1,134 @@
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:
- bearerAuth: []
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:
- bearerAuth: []
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:
- bearerAuth: []
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:
Expand Down
5 changes: 3 additions & 2 deletions src/middlewares/authMiddlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.' });
Expand Down
6 changes: 3 additions & 3 deletions src/routes/wishlistRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit fe09130

Please sign in to comment.