diff --git a/src/__test__/product.test.ts b/src/__test__/product.test.ts index 32c0d99..4fefe73 100644 --- a/src/__test__/product.test.ts +++ b/src/__test__/product.test.ts @@ -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); @@ -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", () => { @@ -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(); diff --git a/src/controllers/productController.ts b/src/controllers/productController.ts index 3ff382d..64ed7b2 100644 --- a/src/controllers/productController.ts +++ b/src/controllers/productController.ts @@ -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 = [ @@ -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( + "Product", + "findAll", + condition_three, + ); + return sendResponse( res, 200, "SUCCESS", "Product fetched successfully!", - product, + { product, relatedProducts }, ); } } catch (error: unknown) { diff --git a/src/database/seeders/20240430162648-demo-product.js b/src/database/seeders/20240430162648-demo-product.js index f9afefe..a7da422 100644 --- a/src/database/seeders/20240430162648-demo-product.js +++ b/src/database/seeders/20240430162648-demo-product.js @@ -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(), }, @@ -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(), }, @@ -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(), }, @@ -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(), }, @@ -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(), }, @@ -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(), }, @@ -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(), }, @@ -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(), }, @@ -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(), },