create-mock-backend
Version:
This is npx starter package for installing your favourite backend template for mockBee.
97 lines (87 loc) • 2.26 kB
JavaScript
import { Response } from "miragejs";
import { requiresAuth } from "../utils/authUtils";
/**
* All the routes related to Wishlist are present here.
* These are private routes.
* Client needs to add "authorization" header with JWT token in it to access it.
* */
/**
* This handler handles getting items to user's wishlist.
* send GET Request at /api/user/wishlist
* */
export const getWishListItemsHandler = function (schema, request) {
const user = requiresAuth.call(this, request);
if (!user) {
new Response(
404,
{},
{
errors: ["The email you entered is not Registered. Not Found error"],
}
);
}
return new Response(200, {}, { wishList: user.wishList });
};
/**
* This handler handles adding items to user's wishlist.
* send POST Request at /api/user/wishlist
* body contains {product}
* */
export const addItemToWishListHandler = function (schema, request) {
const user = requiresAuth.call(this, request);
try {
if (!user) {
new Response(
404,
{},
{
errors: ["The email you entered is not Registered. Not Found error"],
}
);
}
const { product } = JSON.parse(request.requestBody);
user.wishList.push(product);
return new Response(201, {}, { wishList: user.wishList });
} catch (error) {
return new Response(
500,
{},
{
error,
}
);
}
};
/**
* This handler handles removing items to user's wishlist.
* send DELETE Request at /api/user/wishlist
* body contains {product}
* */
export const removeItemFromWishListHandler = function (schema, request) {
const user = requiresAuth.call(this, request);
try {
if (!user) {
new Response(
404,
{},
{
errors: ["The email you entered is not Registered. Not Found error"],
}
);
}
const productId = request.params.productId;
const filteredWishList = user.wishList.filter(
(item) => item._id !== productId
);
this.db.users.update({ wishList: filteredWishList });
return new Response(200, {}, { wishList: filteredWishList });
} catch (error) {
return new Response(
500,
{},
{
error,
}
);
}
};