Skip to content

Commit

Permalink
feat(fake-ads): generate fake ads from aws
Browse files Browse the repository at this point in the history
- using lambda function to generate ads from aliexpress

Delivers #187658789
  • Loading branch information
Heisjabo committed May 27, 2024
1 parent 9b8d8e4 commit 95d45fc
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions __test__/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions src/controllers/adsController.ts
Original file line number Diff line number Diff line change
@@ -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
})
}
}
30 changes: 30 additions & 0 deletions src/docs/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
}
4 changes: 4 additions & 0 deletions src/docs/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -139,6 +140,9 @@ const options = {
patch: updateProducts,
delete: deleteProducts,
},
"/api/v1/products/ads": {
get: getAdProducts
},
"/api/v1/categories": {
get: getCategories,
post: addCategories,
Expand Down
2 changes: 2 additions & 0 deletions src/routes/productsRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/services/ads.service.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 95d45fc

Please sign in to comment.