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

#187419072 implementation of user cart. #35

Merged
merged 3 commits into from
May 15, 2024
Merged

Conversation

Angemichel12
Copy link
Collaborator

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:

  1. Implementation of the GET endpoint at api/v1/carts/ to view the cart.
  2. Implementation of the POST endpoint at api/v1/carts/ to add items to the cart.
  3. Implementation of the PATCH endpoint at api/v1/carts/ to update the cart.
  4. Implementation of the DELETE endpoint at 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:

  • Scenario 1:
    • When the requesting user/token is a buyer and the request body is validated, the item should be added to the buyer's cart. The product details (name, price, image) should be retrieved from the database. The new cart total should be calculated and saved to the buyer's account or session. A confirmation message should be sent to the frontend to be displayed to the buyer.
  • Scenario 2:
    • When the request body fails any validation checks or the token is not of the role buyer, the appropriate 4XX response message should be sent to the frontend describing the validation error.

Developer notes

  • All inputs should be validated for sanity.
  • Only reference URLs should be stored in the database for the case of image URLs.
  • The cart should be saved to the buyer's account or session for future use.

@@ -0,0 +1,47 @@
import request from "supertest";
Copy link

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'


jest.setTimeout(100000);

function logErrors(
Copy link

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 () => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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.

category_id = body.data?.id;
});

it("Seller should create a product", async () => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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.

import cartService from "../services/carts.services";
import { sendResponse } from "../utils/http.exception";

const addItemToCart = async (req: Request, res: Response) => {
Copy link

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.

return sendResponse(res, 500, "ERROR", "Internal Server Error");
}
};
const viewCart = async (req: Request, res: Response) => {
Copy link

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.

return sendResponse(res, 500, "ERROR", "Internal Server Error");
}
};
const clearCart = async (req: Request, res: Response) => {
Copy link

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.

}
};

const updateCart = async (req: Request, res: Response) => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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");
expect(body.message).toStrictEqual("Internal Server Error");
});
it("should handle internal server error", async () => {
Copy link

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");
expect(body.message).toStrictEqual("Internal Server Error");
});
it("should handle internal server error", async () => {
Copy link

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("BAD REQUEST");
expect(body.message).toStrictEqual("Invalid product id");
});
it("should return 404 and Not enough quantity in stock", async () => {
Copy link

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 () => {
Copy link

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/`)
Copy link

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("SERVER ERROR");
expect(body.message).toStrictEqual("Something went wrong!");
});
it("should handle server error", async () => {
Copy link

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("SERVER ERROR");
expect(body.message).toStrictEqual("Something went wrong!");
});
it("should handle server error", async () => {
Copy link

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", async () => {
Copy link

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", async () => {
Copy link

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/`)
Copy link

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.

throw new Error("Internal server error");
});

const { body } = await Jest_request.patch(`/api/v1/carts/`)
Copy link

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: {
Copy link

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.

@Angemichel12 Angemichel12 force-pushed the ft-user-cart-187419072 branch 3 times, most recently from eed6081 to 7e1d13e Compare May 9, 2024 22:35
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("added to cart successfully");
});
it("should return 200 and cart with product", async () => {
Copy link

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 () => {
Copy link

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/`)
Copy link

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/`)
Copy link

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("Invalid product id");
});
it("should return 404 and Not enough quantity in stock", async () => {
const { body } = await Jest_request.post(`/api/v1/carts/`)
Copy link

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("SERVER ERROR");
expect(body.message).toStrictEqual("Something went wrong!");
});
it("should handle server error", async () => {
Copy link

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.

expect(body.status).toStrictEqual("SERVER ERROR");
expect(body.message).toStrictEqual("Something went wrong!");
});
it("should handle server error", async () => {
Copy link

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.

expect(body.message).toStrictEqual("Cart Successfully Clear");
});

it("should handle internal server error", async () => {
Copy link

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.

expect(body.status).toStrictEqual("ERROR");
expect(body.message).toStrictEqual("Internal Server Error");
});
it("should handle internal server error", async () => {
Copy link

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 () => {
Copy link

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.

@Angemichel12 Angemichel12 force-pushed the ft-user-cart-187419072 branch 3 times, most recently from fc25d23 to 6663ab4 Compare May 13, 2024 17:38
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Cart Successfully fetched");
});
it("should return 404 and product not found in cart", async () => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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 () => {
Copy link

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.

@Angemichel12 Angemichel12 force-pushed the ft-user-cart-187419072 branch 2 times, most recently from df73f08 to 53b6c72 Compare May 13, 2024 20:13
@@ -0,0 +1,23 @@
import { Request, Response, NextFunction } from "express";
Copy link

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";
Copy link

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";
Copy link

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 = {
Copy link

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 {
Copy link

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";
Copy link

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";
Copy link

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 = {
Copy link

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 {
Copy link

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";
Copy link

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";
Copy link

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";
Copy link

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'

import { responses } from "../responses";

const cart_routes = {
create_cart: {
Copy link

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 403 and product is expired", async () => {
Copy link

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(result.newTotal).toBe(item.totalPrice);
});

it("should handle internal server error in getRequestUserId", async () => {
Copy link

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 () => {
Copy link

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.

{ productStatus: "Available" },
{ where: { id: product_id } },
);
const { body } = await Jest_request.post(`/api/v1/carts/`)
Copy link

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/`)
Copy link

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.

@Angemichel12 Angemichel12 force-pushed the ft-user-cart-187419072 branch 2 times, most recently from cb238a3 to 16c6f4e Compare May 14, 2024 19:22
Copy link

codeclimate bot commented May 14, 2024

Code Climate has analyzed commit 16c6f4e and detected 28 issues on this pull request.

Here's the issue category breakdown:

Category Count
Duplication 28

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]
@leandreAlly leandreAlly merged commit 5de5d62 into develop May 15, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants