-
Notifications
You must be signed in to change notification settings - Fork 0
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
#187419072 implementation of user cart. #35
Conversation
src/__test__/cart.test.ts
Outdated
@@ -0,0 +1,47 @@ | |||
import request from "supertest"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
1d2359d
to
b208ed7
Compare
|
||
jest.setTimeout(100000); | ||
|
||
function logErrors( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical blocks of code found in 6 locations. Consider refactoring.
expect(body.status).toStrictEqual("NOT FOUND"); | ||
expect(body.message).toStrictEqual("Not enough quantity in stock"); | ||
}); | ||
it("should return 200 and return empty cart", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 9 locations. Consider refactoring.
expect(body.status).toStrictEqual("SUCCESS"); | ||
expect(body.message).toStrictEqual("added to cart successfully"); | ||
}); | ||
it("should return 200 and cart with product", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 9 locations. Consider refactoring.
expect(body.status).toStrictEqual("SUCCESS"); | ||
expect(body.message).toStrictEqual("Cart successfully updated"); | ||
}); | ||
it("should return 200 and Cart Successfully Clear", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 9 locations. Consider refactoring.
); | ||
}); | ||
|
||
it("should authenticate the user and return SUCCESS", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 3 locations. Consider refactoring.
b208ed7
to
bda0274
Compare
category_id = body.data?.id; | ||
}); | ||
|
||
it("Seller should create a product", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
seller_token = body.data; | ||
}); | ||
|
||
it("should create a new category and return 201", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical blocks of code found in 2 locations. Consider refactoring.
await deleteTableData(database_models.Cart, "carts"); | ||
}); | ||
|
||
it("it should register a user and return 201", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 5 locations. Consider refactoring.
src/controllers/cartController.ts
Outdated
import cartService from "../services/carts.services"; | ||
import { sendResponse } from "../utils/http.exception"; | ||
|
||
const addItemToCart = async (req: Request, res: Response) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
src/controllers/cartController.ts
Outdated
return sendResponse(res, 500, "ERROR", "Internal Server Error"); | ||
} | ||
}; | ||
const viewCart = async (req: Request, res: Response) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
bda0274
to
94b4eed
Compare
src/controllers/cartController.ts
Outdated
return sendResponse(res, 500, "ERROR", "Internal Server Error"); | ||
} | ||
}; | ||
const clearCart = async (req: Request, res: Response) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
src/controllers/cartController.ts
Outdated
} | ||
}; | ||
|
||
const updateCart = async (req: Request, res: Response) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
|
||
product_id = (await request).body.data?.id; | ||
}); | ||
it("it should register a user and return 201", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
.expect(403); | ||
expect(body.message).toStrictEqual("Forbidden"); | ||
}); | ||
it("should return 404 when user try to add not exist product", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
expect(body.status).toStrictEqual("NOT FOUND"); | ||
expect(body.message).toStrictEqual("product is not found"); | ||
}); | ||
it("should return 400 and invalid product id", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
94b4eed
to
5abd4eb
Compare
src/__test__/cart.test.ts
Outdated
expect(body.status).toStrictEqual("ERROR"); | ||
expect(body.message).toStrictEqual("Internal Server Error"); | ||
}); | ||
it("should handle internal server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
src/__test__/cart.test.ts
Outdated
expect(body.status).toStrictEqual("ERROR"); | ||
expect(body.message).toStrictEqual("Internal Server Error"); | ||
}); | ||
it("should handle internal server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
src/__test__/cart.test.ts
Outdated
expect(body.status).toStrictEqual("BAD REQUEST"); | ||
expect(body.message).toStrictEqual("Invalid product id"); | ||
}); | ||
it("should return 404 and Not enough quantity in stock", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
expect(body.status).toStrictEqual("Error"); | ||
}); | ||
|
||
it("should return 201 and added to cart successfully", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
expect(body.message).toStrictEqual("Cart is empty"); | ||
}); | ||
it("should return 400 and quantity is required", async () => { | ||
const { body } = await Jest_request.post(`/api/v1/carts/`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 3 locations. Consider refactoring.
5abd4eb
to
e8a216a
Compare
src/__test__/category.test.ts
Outdated
expect(body.status).toStrictEqual("SERVER ERROR"); | ||
expect(body.message).toStrictEqual("Something went wrong!"); | ||
}); | ||
it("should handle server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
src/__test__/category.test.ts
Outdated
expect(body.status).toStrictEqual("SERVER ERROR"); | ||
expect(body.message).toStrictEqual("Something went wrong!"); | ||
}); | ||
it("should handle server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
src/__test__/cart.test.ts
Outdated
expect(body.status).toStrictEqual("ERROR"); | ||
expect(body.message).toStrictEqual("Internal Server Error"); | ||
}); | ||
it("should handle internal server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
src/__test__/cart.test.ts
Outdated
expect(body.status).toStrictEqual("ERROR"); | ||
expect(body.message).toStrictEqual("Internal Server Error"); | ||
}); | ||
it("should handle internal server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
expect(body.message).toStrictEqual("Product does not exist"); | ||
}); | ||
it("should return 201 and Cart successfully updated", async () => { | ||
const { body } = await Jest_request.patch(`/api/v1/carts/`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 3 locations. Consider refactoring.
e8a216a
to
e5a2203
Compare
throw new Error("Internal server error"); | ||
}); | ||
|
||
const { body } = await Jest_request.patch(`/api/v1/carts/`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 3 locations. Consider refactoring.
consumes: ["application/json"], | ||
responses, | ||
}, | ||
update_cart: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
eed6081
to
7e1d13e
Compare
expect(body.status).toStrictEqual("SUCCESS"); | ||
expect(body.message).toStrictEqual("added to cart successfully"); | ||
}); | ||
it("should return 200 and cart with product", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 8 locations. Consider refactoring.
expect(body.status).toStrictEqual("SUCCESS"); | ||
expect(body.message).toStrictEqual("Cart successfully updated"); | ||
}); | ||
it("should return 200 and Cart Successfully Clear", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 8 locations. Consider refactoring.
expect(body.message).toStrictEqual("Cart is empty"); | ||
}); | ||
it("should return 400 and quantity is required", async () => { | ||
const { body } = await Jest_request.post(`/api/v1/carts/`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
expect(body.message).toStrictEqual("Product does not exist"); | ||
}); | ||
it("should return 201 and Cart successfully updated", async () => { | ||
const { body } = await Jest_request.patch(`/api/v1/carts/`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
src/__test__/cart.test.ts
Outdated
expect(body.message).toStrictEqual("Invalid product id"); | ||
}); | ||
it("should return 404 and Not enough quantity in stock", async () => { | ||
const { body } = await Jest_request.post(`/api/v1/carts/`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
7e1d13e
to
970e817
Compare
src/__test__/category.test.ts
Outdated
expect(body.status).toStrictEqual("SERVER ERROR"); | ||
expect(body.message).toStrictEqual("Something went wrong!"); | ||
}); | ||
it("should handle server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 6 locations. Consider refactoring.
src/__test__/category.test.ts
Outdated
expect(body.status).toStrictEqual("SERVER ERROR"); | ||
expect(body.message).toStrictEqual("Something went wrong!"); | ||
}); | ||
it("should handle server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 6 locations. Consider refactoring.
src/__test__/cart.test.ts
Outdated
expect(body.message).toStrictEqual("Cart Successfully Clear"); | ||
}); | ||
|
||
it("should handle internal server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 6 locations. Consider refactoring.
src/__test__/cart.test.ts
Outdated
expect(body.status).toStrictEqual("ERROR"); | ||
expect(body.message).toStrictEqual("Internal Server Error"); | ||
}); | ||
it("should handle internal server error", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 6 locations. Consider refactoring.
await deleteTableData(database_models.Cart, "carts"); | ||
}); | ||
|
||
it("it should register a user and return 201", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 6 locations. Consider refactoring.
970e817
to
3ca2694
Compare
fc25d23
to
6663ab4
Compare
expect(body.status).toStrictEqual("SUCCESS"); | ||
expect(body.message).toStrictEqual("Cart Successfully fetched"); | ||
}); | ||
it("should return 404 and product not found in cart", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
expect(body.status).toStrictEqual("SUCCESS"); | ||
expect(body.message).toStrictEqual("added to cart successfully"); | ||
}); | ||
it("should return 404 and Not enough quantity in stock", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
expect(body.status).toStrictEqual("NOT FOUND"); | ||
expect(body.message).toStrictEqual("product is not found"); | ||
}); | ||
it("should return 201 and Cart successfully updated", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
6663ab4
to
4200be4
Compare
expect(body.status).toStrictEqual("SUCCESS"); | ||
expect(body.message).toStrictEqual("added to cart successfully"); | ||
}); | ||
it("should return 404 and Not enough quantity in stock", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 3 locations. Consider refactoring.
expect(body.status).toStrictEqual("NOT FOUND"); | ||
expect(body.message).toStrictEqual("product is not found"); | ||
}); | ||
it("should return 201 and Cart successfully updated", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 3 locations. Consider refactoring.
expect(body.status).toStrictEqual("SUCCESS"); | ||
expect(body.message).toStrictEqual("Cart Successfully Clear"); | ||
}); | ||
it("should return 404 and product does not exist on update", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 3 locations. Consider refactoring.
df73f08
to
53b6c72
Compare
53b6c72
to
5a17635
Compare
@@ -0,0 +1,23 @@ | |||
import { Request, Response, NextFunction } from "express"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,32 @@ | |||
import express from "express"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,30 @@ | |||
import database_models from "../database/config/db.config"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,17 @@ | |||
export type cartItem = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,17 @@ | |||
export default class CartUtils { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,32 @@ | |||
import express from "express"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,30 @@ | |||
import database_models from "../database/config/db.config"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,17 @@ | |||
export type cartItem = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,17 @@ | |||
export default class CartUtils { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,8 @@ | |||
import { ExpandedRequest } from "../middlewares/auth"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,8 @@ | |||
import { ExpandedRequest } from "../middlewares/auth"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
@@ -0,0 +1,11 @@ | |||
import Joi from "joi"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing error: 'import' and 'export' may appear only with 'sourceType: module'
5a17635
to
2e67e48
Compare
import { responses } from "../responses"; | ||
|
||
const cart_routes = { | ||
create_cart: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.
2e67e48
to
77eb201
Compare
expect(body.status).toStrictEqual("NOT FOUND"); | ||
expect(body.message).toStrictEqual("product is not found"); | ||
}); | ||
it("should return 403 and product is expired", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 3 locations. Consider refactoring.
77eb201
to
ecb4f0f
Compare
expect(result.newTotal).toBe(item.totalPrice); | ||
}); | ||
|
||
it("should handle internal server error in getRequestUserId", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
expect(body.status).toStrictEqual("ERROR"); | ||
expect(body.message).toStrictEqual("Internal Server Error"); | ||
}); | ||
it("should handle internal server error on clear cart", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 4 locations. Consider refactoring.
ecb4f0f
to
82a8df7
Compare
{ productStatus: "Available" }, | ||
{ where: { id: product_id } }, | ||
); | ||
const { body } = await Jest_request.post(`/api/v1/carts/`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical blocks of code found in 2 locations. Consider refactoring.
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/`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical blocks of code found in 2 locations. Consider refactoring.
cb238a3
to
16c6f4e
Compare
Code Climate has analyzed commit 16c6f4e and detected 28 issues on this pull request. Here's the issue category breakdown:
The test coverage on the diff in this pull request is 99.1% (60% is the threshold). This pull request will bring the total coverage in the repository to 96.0% (0.5% change). View more on Code Climate. |
- user can create cart. - user can clear cart. - user can get cart. - user can update cart. [Delivers #187419072]
What does this PR do?
This PR allows users/buyers to manage their carts.
Description of tasks to be completed.
The tasks completed in this PR include:
api/v1/carts/
to view the cart.api/v1/carts/
to add items to the cart.api/v1/carts/
to update the cart.api/v1/carts/
to clear the cart.Why this PR is needed
This PR is needed to handle the addition of items to the cart, including storing the cart id and calculating the new cart total. The cart can be either a new cart or an existing cart.
Acceptance criteria
Given a post request to add an item to the cart:
Developer notes