From 95d45fc423a5ecbd1ecdb548095aade4029bbafb Mon Sep 17 00:00:00 2001 From: Heisjabo Date: Mon, 27 May 2024 15:09:45 +0200 Subject: [PATCH] feat(fake-ads): generate fake ads from aws - using lambda function to generate ads from aliexpress Delivers #187658789 --- __test__/product.test.ts | 6 ++++++ package.json | 1 + src/controllers/adsController.ts | 18 ++++++++++++++++++ src/docs/products.ts | 30 ++++++++++++++++++++++++++++++ src/docs/swagger.ts | 4 ++++ src/routes/productsRoute.ts | 2 ++ src/services/ads.service.ts | 14 ++++++++++++++ 7 files changed, 75 insertions(+) create mode 100644 src/controllers/adsController.ts create mode 100644 src/services/ads.service.ts diff --git a/__test__/product.test.ts b/__test__/product.test.ts index 5b95c9e..95c2d4d 100644 --- a/__test__/product.test.ts +++ b/__test__/product.test.ts @@ -422,6 +422,12 @@ expect(response.body).toEqual({ }) }) +test("should return 200 when getting ads from aliexpress", async () => { + const response = await request(app) + .get("/api/v1/products/ads?query=fashion"); + expect(response.status).toBe(200); +}) + test("Return 500 for handle error", async () => { const response = await request(app) .get("/api/v1/products/review") diff --git a/package.json b/package.json index 3becaa5..b0eb729 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "dependencies": { "@types/bcrypt": "^5.0.2", "@types/socket.io": "^3.0.2", + "axios": "^1.7.2", "bcrypt": "^5.1.1", "bcryptjs": "^2.4.3", "cloudinary": "^2.2.0", diff --git a/src/controllers/adsController.ts b/src/controllers/adsController.ts new file mode 100644 index 0000000..fce5544 --- /dev/null +++ b/src/controllers/adsController.ts @@ -0,0 +1,18 @@ +import { fetchAds } from "../services/ads.service"; +import { Request, Response } from "express"; + +export const getAds = async ( req: Request, res: Response) => { + try{ + const { query } = req.query || "electronics"; + //@ts-ignore + const ads = await fetchAds(query); + return res.status(200).json({ + message: 'ads was successfully retrieved', + ads + }); + }catch(err: any){ + return res.status(500).json({ + message: err.message + }) + } +} diff --git a/src/docs/products.ts b/src/docs/products.ts index 4a15d74..8b4fbc6 100644 --- a/src/docs/products.ts +++ b/src/docs/products.ts @@ -268,3 +268,33 @@ export const getProducts = { } } }; + + export const getAdProducts = { + tags: ["Products"], + security: [{ bearerAuth: [] }], + summary: "Fetch ads from aliexpress", + description: "This endpoint fetches advertisements based on a query parameter. If no query is provided, it defaults to 'electronics'.", + parameters: [ + { + name: "query", + in: "query", + required: false, + description: "The search query for fetching ads.", + schema: { + type: "string", + example: "electronics" + } + } + ], + responses: { + 200: { + description: "Success", + }, + 404: { + description: "Not Found", + }, + 500: { + description: "Internal Server Error", + } + } + } diff --git a/src/docs/swagger.ts b/src/docs/swagger.ts index b4ba080..d3b2439 100644 --- a/src/docs/swagger.ts +++ b/src/docs/swagger.ts @@ -45,6 +45,7 @@ import { getAllNotifications, readNotification } from "./notifications"; import { homepage } from "./home"; import { payment } from "./payments"; import { createReviewProduct, deleteReview, getReviewProduct, reviewSchema, updateReviewProduct } from "./reviews"; +import { getAdProducts } from "./products"; const docRouter = express.Router(); @@ -139,6 +140,9 @@ const options = { patch: updateProducts, delete: deleteProducts, }, + "/api/v1/products/ads": { + get: getAdProducts + }, "/api/v1/categories": { get: getCategories, post: addCategories, diff --git a/src/routes/productsRoute.ts b/src/routes/productsRoute.ts index c445721..164a3a6 100644 --- a/src/routes/productsRoute.ts +++ b/src/routes/productsRoute.ts @@ -10,9 +10,11 @@ import { addReviewController, deleteReviewController, getreviewController, updat import { addReviewValidate, updateReviewValidate } from "../schemas/review"; import { hasPurchasedProduct } from "../middlewares/hasPurchased"; import { isPasswordOutOfDate } from "../middlewares/isPasswordOutOfDate"; +import { getAds } from "../controllers/adsController"; const productsRouter = Router(); productsRouter.get("/search",isPasswordOutOfDate, searchProductController) +productsRouter.get('/ads', getAds); productsRouter.get("/",isLoggedIn,isPasswordOutOfDate,fetchProducts); productsRouter.get("/:id",isLoggedIn,isPasswordOutOfDate,fetchSingleProduct); diff --git a/src/services/ads.service.ts b/src/services/ads.service.ts new file mode 100644 index 0000000..693121a --- /dev/null +++ b/src/services/ads.service.ts @@ -0,0 +1,14 @@ +import axios from 'axios'; + +export const fetchAds = async (query: string) => { + try { + const response = await axios.get(`https://idtk1v5fm4.execute-api.eu-north-1.amazonaws.com/products/ads`, { + params: { query } + }); + const data = await response.data + return data; + } catch (error: any) { + throw Error(error.message); + } +} +