Skip to content

Commit 23ef274

Browse files
authored
Merge pull request #70 from atlp-rwanda/fix-cart-187419072
FIX(add cart item): add item in cart by increment and decrement
2 parents 3075ff5 + 1de4126 commit 23ef274

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed

.vscode/extensions.json

-8
This file was deleted.

.vscode/settings.json

-3
This file was deleted.

src/__test__/order.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ describe("ORDER API TEST", () => {
222222

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

244244
it("should return 201 and added to cart successfully", async () => {
245245
await database_models.Product.update(
246-
{ isAvailable, price: 499000 },
246+
{ isAvailable: true, price: 499000 },
247247
{ where: { id: product_id } },
248248
);
249249
const { body } = await Jest_request.post(`/api/v1/carts/`)
250250
.set("Authorization", `Bearer ${token}`)
251-
.send({ productId: product_id, quantity: 10 });
251+
.send({ productId: product_id, quantity: 4 });
252+
252253
expect(body.status).toStrictEqual("SUCCESS");
253254
expect(body.message).toStrictEqual("Added to cart successfully");
254255
});
255256
it("should return 201 and added to cart successfully again", async () => {
256257
const { body } = await Jest_request.post(`/api/v1/carts/`)
257258
.set("Authorization", `Bearer ${token}`)
258-
.send({ productId: product_id, quantity: 10 });
259+
.send({ productId: product_id, quantity: 4 });
259260
expect(body.status).toStrictEqual("SUCCESS");
260261
expect(body.message).toStrictEqual("Added to cart successfully");
261262
});

src/__test__/sales.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ describe("SALE API TEST", () => {
231231

232232
it("should return 201 and added to cart successfully", async () => {
233233
await database_models.Product.update(
234-
{ isAvailable, price: 5 },
234+
{ isAvailable, price: 500 },
235235
{ where: { id: product_id } },
236236
);
237237
const { body } = await Jest_request.post(`/api/v1/carts/`)
238238
.set("Authorization", `Bearer ${token}`)
239-
.send({ productId: product_id, quantity: 10 });
239+
.send({ productId: product_id, quantity: 1 });
240240
expect(body.status).toStrictEqual("SUCCESS");
241241
expect(body.message).toStrictEqual("Added to cart successfully");
242242
});
@@ -257,14 +257,14 @@ describe("SALE API TEST", () => {
257257
);
258258
const { body } = await Jest_request.post(`/api/v1/carts/`)
259259
.set("Authorization", `Bearer ${token}`)
260-
.send({ productId: product_id, quantity: 10 });
260+
.send({ productId: product_id, quantity: 4 });
261261
expect(body.status).toStrictEqual("SUCCESS");
262262
expect(body.message).toStrictEqual("Added to cart successfully");
263263
});
264264
it("should return 201 and added to cart successfully again", async () => {
265265
const { body } = await Jest_request.post(`/api/v1/carts/`)
266266
.set("Authorization", `Bearer ${token}`)
267-
.send({ productId: product_id, quantity: 10 });
267+
.send({ productId: product_id, quantity: 4 });
268268
expect(body.status).toStrictEqual("SUCCESS");
269269
expect(body.message).toStrictEqual("Added to cart successfully");
270270
});

src/__test__/stripePayment.test.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ describe("STRIPE PAYMENTS API TEST", () => {
172172
);
173173
const { body } = await Jest_request.post(`/api/v1/carts/`)
174174
.set("Authorization", `Bearer ${token}`)
175-
.send({ productId: product_id, quantity: 10 });
175+
.send({ productId: product_id, quantity: 20 });
176176
expect(body.status).toStrictEqual("SUCCESS");
177177
expect(body.message).toStrictEqual("added to cart successfully");
178178
});
179179
it("should return 201 and added to cart successfully again", async () => {
180180
const { body } = await Jest_request.post(`/api/v1/carts/`)
181181
.set("Authorization", `Bearer ${token}`)
182-
.send({ productId: product_id, quantity: 10 });
182+
.send({ productId: product_id, quantity: 20 });
183183
expect(body.status).toStrictEqual("SUCCESS");
184184
expect(body.message).toStrictEqual("Added to cart successfully");
185185
});
@@ -216,12 +216,12 @@ describe("STRIPE PAYMENTS API TEST", () => {
216216

217217
it("should return 201 and added to cart successfully", async () => {
218218
await database_models.Product.update(
219-
{ isAvailable: true, price: 5 },
219+
{ isAvailable: true, price: 500 },
220220
{ where: { id: product_id } },
221221
);
222222
const { body } = await Jest_request.post(`/api/v1/carts/`)
223223
.set("Authorization", `Bearer ${token}`)
224-
.send({ productId: product_id, quantity: 10 });
224+
.send({ productId: product_id, quantity: 1 });
225225
expect(body.status).toStrictEqual("SUCCESS");
226226
expect(body.message).toStrictEqual("Added to cart successfully");
227227
});
@@ -242,14 +242,15 @@ describe("STRIPE PAYMENTS API TEST", () => {
242242
);
243243
const { body } = await Jest_request.post(`/api/v1/carts/`)
244244
.set("Authorization", `Bearer ${token}`)
245-
.send({ productId: product_id, quantity: 10 });
245+
.send({ productId: product_id, quantity: 4 });
246+
246247
expect(body.status).toStrictEqual("SUCCESS");
247248
expect(body.message).toStrictEqual("Added to cart successfully");
248249
});
249250
it("should return 201 and added to cart successfully again", async () => {
250251
const { body } = await Jest_request.post(`/api/v1/carts/`)
251252
.set("Authorization", `Bearer ${token}`)
252-
.send({ productId: product_id, quantity: 10 });
253+
.send({ productId: product_id, quantity: 4 });
253254
expect(body.status).toStrictEqual("SUCCESS");
254255
expect(body.message).toStrictEqual("Added to cart successfully");
255256
});

src/controllers/cartController.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import cartService from "../services/carts.services";
33

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

6-
import { sendResponse } from "../utils/http.exception";
76
import database_models from "../database/config/db.config";
8-
import { read_function } from "../utils/db_methods";
97
import { ExpandedRequest } from "../middlewares/auth";
8+
import { read_function } from "../utils/db_methods";
9+
import { sendResponse } from "../utils/http.exception";
1010
import UserUtils from "../utils/users";
11-
import CartUtils from "../utils/cart.utils";
1211

1312
const addItemToCart = async (req: ExpandedRequest, res: Response) => {
1413
try {
@@ -26,6 +25,7 @@ const addItemToCart = async (req: ExpandedRequest, res: Response) => {
2625
);
2726

2827
const newprice = product!.price * quantity;
28+
2929
const item = {
3030
id: product!.id,
3131
name: product!.name,
@@ -38,34 +38,38 @@ const addItemToCart = async (req: ExpandedRequest, res: Response) => {
3838
const cart = await cartService.findCartByUserIdService(userid);
3939

4040
if (!cart) {
41-
const newCart = await new database_models.Cart({
41+
const newCart = new database_models.Cart({
4242
products: [item],
4343
userId: userid,
4444
total: newprice,
4545
});
4646
await newCart.save();
4747
return sendResponse(res, 201, "SUCCESS", "added to cart successfully");
4848
}
49-
const itemExist = cart.products.findIndex((cItem) => cItem.id === item.id);
50-
let removedProduct;
51-
if (itemExist !== -1) {
52-
removedProduct = cart.products.splice(itemExist, 1);
53-
}
54-
if (removedProduct) {
55-
item.quantity += removedProduct[0].quantity;
56-
item.totalPrice = item.quantity * item.price;
49+
50+
const itemIndex = cart.products.findIndex((cItem) => cItem.id === item.id);
51+
52+
if (itemIndex !== -1) {
53+
cart.products[itemIndex].quantity = item.quantity;
54+
cart.products[itemIndex].totalPrice = item.totalPrice;
55+
} else {
56+
cart.products.push(item);
5757
}
58-
const updateCart = CartUtils.updateCart(cart, item);
5958

60-
await cartService.updateCartProductsAndTotalService(
61-
updateCart.cart,
62-
updateCart.newTotal,
59+
const newTotal = cart.products.reduce(
60+
(acc, cItem) => acc + cItem.totalPrice,
61+
0,
6362
);
63+
cart.total = newTotal;
64+
65+
await cartService.updateCartProductsAndTotalService(cart, newTotal);
66+
6467
return sendResponse(res, 201, "SUCCESS", "Added to cart successfully");
6568
} catch (error) {
6669
return sendResponse(res, 500, "ERROR", "Internal Server error");
6770
}
6871
};
72+
6973
const viewCart = async (req: ExpandedRequest, res: Response) => {
7074
try {
7175
const userId = UserUtils.getRequestUserId(req);

0 commit comments

Comments
 (0)