Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX(add cart item): add item in cart by increment and decrement #70

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .vscode/extensions.json

This file was deleted.

3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

11 changes: 6 additions & 5 deletions src/__test__/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ describe("ORDER API TEST", () => {

it("should return 201 and added to cart successfully", async () => {
await database_models.Product.update(
{ isAvailable, price: 5 },
{ isAvailable: true, price: 500 },
{ where: { id: product_id } },
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 1 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand All @@ -243,19 +243,20 @@ describe("ORDER API TEST", () => {

it("should return 201 and added to cart successfully", async () => {
await database_models.Product.update(
{ isAvailable, price: 499000 },
{ isAvailable: true, price: 499000 },
{ where: { id: product_id } },
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });

expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
it("should return 201 and added to cart successfully again", async () => {
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand Down
8 changes: 4 additions & 4 deletions src/__test__/sales.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ describe("SALE API TEST", () => {

it("should return 201 and added to cart successfully", async () => {
await database_models.Product.update(
{ isAvailable, price: 5 },
{ isAvailable, price: 500 },
{ where: { id: product_id } },
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 1 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand All @@ -257,14 +257,14 @@ describe("SALE API TEST", () => {
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
it("should return 201 and added to cart successfully again", async () => {
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand Down
13 changes: 7 additions & 6 deletions src/__test__/stripePayment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ describe("STRIPE PAYMENTS API TEST", () => {
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 20 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("added to cart successfully");
});
it("should return 201 and added to cart successfully again", async () => {
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 20 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand Down Expand Up @@ -216,12 +216,12 @@ describe("STRIPE PAYMENTS API TEST", () => {

it("should return 201 and added to cart successfully", async () => {
await database_models.Product.update(
{ isAvailable: true, price: 5 },
{ isAvailable: true, price: 500 },
{ where: { id: product_id } },
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 1 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand All @@ -242,14 +242,15 @@ describe("STRIPE PAYMENTS API TEST", () => {
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });

expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
it("should return 201 and added to cart successfully again", async () => {
const { body } = await Jest_request.post(`/api/v1/carts/`)
.set("Authorization", `Bearer ${token}`)
.send({ productId: product_id, quantity: 10 });
.send({ productId: product_id, quantity: 4 });
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Added to cart successfully");
});
Expand Down
36 changes: 20 additions & 16 deletions src/controllers/cartController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import cartService from "../services/carts.services";

import { ProductAttributes } from "../types/model";

import { sendResponse } from "../utils/http.exception";
import database_models from "../database/config/db.config";
import { read_function } from "../utils/db_methods";
import { ExpandedRequest } from "../middlewares/auth";
import { read_function } from "../utils/db_methods";
import { sendResponse } from "../utils/http.exception";
import UserUtils from "../utils/users";
import CartUtils from "../utils/cart.utils";

const addItemToCart = async (req: ExpandedRequest, res: Response) => {
try {
Expand All @@ -26,6 +25,7 @@ const addItemToCart = async (req: ExpandedRequest, res: Response) => {
);

const newprice = product!.price * quantity;

const item = {
id: product!.id,
name: product!.name,
Expand All @@ -38,34 +38,38 @@ const addItemToCart = async (req: ExpandedRequest, res: Response) => {
const cart = await cartService.findCartByUserIdService(userid);

if (!cart) {
const newCart = await new database_models.Cart({
const newCart = new database_models.Cart({
products: [item],
userId: userid,
total: newprice,
});
await newCart.save();
return sendResponse(res, 201, "SUCCESS", "added to cart successfully");
}
const itemExist = cart.products.findIndex((cItem) => cItem.id === item.id);
let removedProduct;
if (itemExist !== -1) {
removedProduct = cart.products.splice(itemExist, 1);
}
if (removedProduct) {
item.quantity += removedProduct[0].quantity;
item.totalPrice = item.quantity * item.price;

const itemIndex = cart.products.findIndex((cItem) => cItem.id === item.id);

if (itemIndex !== -1) {
cart.products[itemIndex].quantity = item.quantity;
cart.products[itemIndex].totalPrice = item.totalPrice;
} else {
cart.products.push(item);
}
const updateCart = CartUtils.updateCart(cart, item);

await cartService.updateCartProductsAndTotalService(
updateCart.cart,
updateCart.newTotal,
const newTotal = cart.products.reduce(
(acc, cItem) => acc + cItem.totalPrice,
0,
);
cart.total = newTotal;

await cartService.updateCartProductsAndTotalService(cart, newTotal);

return sendResponse(res, 201, "SUCCESS", "Added to cart successfully");
} catch (error) {
return sendResponse(res, 500, "ERROR", "Internal Server error");
}
};

const viewCart = async (req: ExpandedRequest, res: Response) => {
try {
const userId = UserUtils.getRequestUserId(req);
Expand Down