Skip to content

Commit b856aa5

Browse files
authored
Merge pull request #79 from atlp-rwanda/fix-discount-payment
[FIX]: Implemented discount while adding to cart
2 parents 4779839 + 14251de commit b856aa5

File tree

7 files changed

+40
-5
lines changed

7 files changed

+40
-5
lines changed

src/__test__/cart.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,23 @@ describe("CART API TEST", () => {
395395
"Cannot delete the product because it is linked to other records.",
396396
);
397397
});
398+
399+
it("should return 500 when adding to cart", async () => {
400+
await database_models.Product.update(
401+
{ isAvailable },
402+
{ where: { id: product_id } },
403+
);
404+
405+
jest
406+
.spyOn(cartService, "findCartByUserIdService")
407+
.mockImplementationOnce(() => {
408+
throw new Error("Internal server error");
409+
});
410+
411+
const { body } = await Jest_request.post(`/api/v1/carts/`)
412+
.set("Authorization", `Bearer ${token}`)
413+
.send({ productId: product_id, quantity: 10 });
414+
expect(body.status).toStrictEqual("ERROR");
415+
expect(body.message).toStrictEqual("Internal Server error");
416+
});
398417
});

src/__test__/sales.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ describe("SALE API TEST", () => {
381381
expect(body.message).toStrictEqual("Sale not found");
382382
});
383383

384+
it(" should return 404 when find a single sale", async () => {
385+
const { body } = await Jest_request.get(
386+
"/api/v1/sales/9e555bd6-0f36-454a-a3d5-89edef4ff9d1",
387+
)
388+
.set("Authorization", `Bearer ${seller_token}`)
389+
.expect(404);
390+
391+
expect(body.status).toStrictEqual("NOT FOUND");
392+
expect(body.message).toStrictEqual("Sale not found");
393+
});
394+
384395
it("should return error when status value is neither cancelled, delivered nor pending", async () => {
385396
const { body } = await Jest_request.patch(`/api/v1/sales/${sale_id}/status`)
386397
.set("Authorization", `Bearer ${seller_token}`)

src/controllers/cartController.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ const addItemToCart = async (req: ExpandedRequest, res: Response) => {
2424
condition,
2525
);
2626

27-
const newprice = product!.price * quantity;
27+
const newprice =
28+
(product!.price - (product?.price * product?.discount) / 100) * quantity;
2829

2930
const item = {
3031
id: product!.id,
3132
name: product!.name,
3233
image: product!.images[0],
34+
discount: product?.discount,
3335
quantity: quantity,
3436
price: product!.price,
3537
totalPrice: newprice,

src/controllers/orderController.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const include = [
1616
{
1717
model: Product,
1818
as: "soldProducts",
19-
attributes: ["name", "images", "price"],
19+
attributes: ["name", "images", "price", "discount"],
2020
},
2121
],
2222
},

src/mock/static.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export const new_product = {
189189
name: "BMW",
190190
price: 49900,
191191
images: [image_one_path, image_two_path, image_three_path, image_four_path],
192-
discount: 100,
192+
discount: 10,
193193
quantity: 356,
194194
expiryDate: "2324-04-30T00:00:00.000Z",
195195
};

src/services/payment.services.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export const lineCartItems = (cart: cartModelAttributes) => {
6060
name: product.name,
6161
images: [product.image],
6262
},
63-
unit_amount: Math.floor(product.price),
63+
unit_amount: Math.floor(
64+
product.price - (product.price * product.discount) / 100,
65+
),
6466
},
6567
quantity: product.quantity,
6668
};
@@ -125,7 +127,7 @@ export const orderItems = async (cart: cartModelAttributes) => {
125127
{
126128
model: Product,
127129
as: "soldProducts",
128-
attributes: ["name", "price", "images", "isAvailable"],
130+
attributes: ["name", "price", "images", "isAvailable", "discount"],
129131
},
130132
],
131133
},

src/types/cart.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type cartItem = {
22
id: string;
33
name: string;
44
image: string;
5+
discount: number;
56
quantity: number;
67
price: number;
78
totalPrice: number;

0 commit comments

Comments
 (0)