Skip to content

Commit

Permalink
ft(relatedProducts):buyer should see related products
Browse files Browse the repository at this point in the history
  • Loading branch information
JacquelineTuyisenge committed Jul 12, 2024
1 parent 23ef274 commit a0b2829
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/__test__/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { generateAccessToken } from "../helpers/security.helpers";
import { read_function } from "../utils/db_methods";
import { response } from "express";
import searchProduct from "../controllers/searchProduct";
import { Console } from "node:console";

jest.setTimeout(100000);

Expand All @@ -35,6 +36,7 @@ function logErrors(

const Jest_request = request(app.use(logErrors));
let seller_token: string;
let buyer_token: string;
let category_id: string;
let product_id: any;
describe("PRODUCT API TEST", () => {
Expand Down Expand Up @@ -188,7 +190,40 @@ describe("PRODUCT API TEST", () => {
const { body } = await Jest_request.get(`/api/v1/products/${product_id}`)
.set("Authorization", `Bearer ${seller_token}`)
.expect(200);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Product fetched successfully!");
expect(body.data).toBeDefined();
});

it("buyer should get single product and related products", async () => {
const user = await database_models.User.findOne({
where: { role: "11afd4f1-0bed-4a3b-8ad5-0978dabf8fcd" },
});
const products = await database_models.Product.findAll({
where: { isAvailable: true },
});
const product = products[0];

console.log(
"product found ****************************************************************************************************",
product,
);

const productId = product?.dataValues.id as string;
console.log(
"id found ****************************************************************************************************",
productId,
);

const authenticatetoken = generateAccessToken({
id: user?.dataValues.id as string,
role: "BUYER",
});
buyer_token = authenticatetoken;

const { body } = await Jest_request.get(`/api/v1/products/${productId}`)
.set("Authorization", `Bearer ${buyer_token}`)
.expect(200);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Product fetched successfully!");
expect(body.data).toBeDefined();
Expand Down
18 changes: 17 additions & 1 deletion src/controllers/productController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { insert_function, read_function } from "../utils/db_methods";
import { category_utils } from "../utils/controller";
import { Info, Message } from "../types/upload";
import { isAvailable } from "../utils/nodeEvents";
import { OrderItem } from "sequelize";

let product_id;
const include = [
Expand Down Expand Up @@ -206,12 +207,27 @@ const read_single_product = async (req: Request, res: Response) => {
"Product not found or not owned!",
);
}

const condition_three = {
where: { categoryId: product.categoryId },
isAvailable: true,
include,
limit: 5,
order: [["createdAt", "DESC"]] as OrderItem[],
};

const relatedProducts = await read_function<ProductAttributes>(
"Product",
"findAll",
condition_three,
);

return sendResponse(
res,
200,
"SUCCESS",
"Product fetched successfully!",
product,
{ product, relatedProducts },
);
}
} catch (error: unknown) {
Expand Down
9 changes: 9 additions & 0 deletions src/database/seeders/20240430162648-demo-product.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
sellerId: "7321d946-7265-45a1-9ce3-3da1789e657e",
categoryId: "8dfe453c-b779-453c-b96e-afe656eeebab",
expiryDate: "2324-04-30T00:00:00.000Z",
isAvailable: true,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -41,6 +42,7 @@ module.exports = {
sellerId: "7321d946-7265-45a1-9ce3-3da1789e657e",
categoryId: "8dfe453c-b779-453c-b96e-afe656eeebab",
expiryDate: "2324-04-20T00:00:00.000Z",
isAvailable: true,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -59,6 +61,7 @@ module.exports = {
sellerId: "7321d946-7265-45a1-9ce3-3da1789e657e",
categoryId: "8dfe453c-b779-453c-b96e-afe656eeebab",
expiryDate: "2324-04-30T00:00:00.000Z",
isAvailable: true,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -77,6 +80,7 @@ module.exports = {
sellerId: "7321d946-7265-45a1-9ce3-3da1789e657e",
categoryId: "8dfe453c-b779-453c-b96e-afe656eeebab",
expiryDate: "2324-04-30T00:00:00.000Z",
isAvailable: true,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -95,6 +99,7 @@ module.exports = {
sellerId: "7321d946-7265-45a1-9ce3-3da1789e657e",
categoryId: "8dfe453c-b779-453c-b96e-afe656eeebab",
expiryDate: "2324-04-30T00:00:00.000Z",
isAvailable: true,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -113,6 +118,7 @@ module.exports = {
sellerId: "7321d946-7265-45a1-9ce3-3da1789e657e",
categoryId: "8dfe453c-b779-453c-b96e-afe656eeebab",
expiryDate: "2324-04-30T00:00:00.000Z",
isAvailable: false,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -130,6 +136,7 @@ module.exports = {
categoryId: "a64aeab5-eaa1-48f4-9e52-8a0924a0490a",
sellerId: "5ea39bd8-ad6c-42a7-a136-32357cbede3a",
expiryDate: "2025-01-01T00:00:00.000Z",
isAvailable: true,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -147,6 +154,7 @@ module.exports = {
categoryId: "a64aeab5-eaa1-48f4-9e52-8a0924a0490a",
sellerId: "5ea39bd8-ad6c-42a7-a136-32357cbede3a",
expiryDate: "2025-05-01T00:00:00.000Z",
isAvailable: true,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -165,6 +173,7 @@ module.exports = {
sellerId: "1001d946-7265-45a1-9ce3-3da1789e100a",
categoryId: "8dfe453c-b779-453c-b96e-afe656eeebab",
expiryDate: "2324-04-30T00:00:00.000Z",
isAvailable: false,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand Down

0 comments on commit a0b2829

Please sign in to comment.