diff --git a/src/config.ts b/src/config.ts index abb2050..b77e33d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,6 +4,7 @@ export const CONFIG = { MAX_PRODUCTS_PER_CATEGORY: Number(process.env.MAX_PRODUCTS_PER_CATEGORY), LAST_MONTHS_PRODUCTS_IN_ORDER: Number(process.env.LAST_MONTHS_PRODUCTS_IN_ORDER), MAX_BESTSELLS_PRODUCTS: Number(process.env.MAX_BESTSELLS_PRODUCTS), + HITS_OF_WEEK_DAYS_PERIOD: Number(process.env.HITS_OF_WEEK_DAYS_PERIOD), SCORES_MAX_SIZE: Number(process.env.SCORES_MAX_SIZE), BASE_PRODUCT_COMMENTS_COUNT: Number(process.env.BASE_PRODUCT_COMMENTS_COUNT), MAX_SEARCH_RESULT: Number(process.env.MAX_SEARCH_RESULT) diff --git a/src/products/product.ts b/src/products/product.ts index a76d510..8264500 100644 --- a/src/products/product.ts +++ b/src/products/product.ts @@ -64,6 +64,14 @@ export const getAllBestsellerProducts = async () => { return getMostSoldProductsByDate(bestsellerMonthsPriorToToday); }; +export const getHitsOfWeekProducts = async () => { + const hitsOfWeekData = new Date(); + hitsOfWeekData.setDate( + hitsOfWeekData.getDate() - CONFIG.HITS_OF_WEEK_DAYS_PERIOD + ); + return getMostSoldProductsByDate(hitsOfWeekData); +}; + export const getProductKeyValueVotes = ( productId: number, productScoresDb: ProductScore[] diff --git a/src/products/routes.ts b/src/products/routes.ts index 50d88c9..df6581a 100644 --- a/src/products/routes.ts +++ b/src/products/routes.ts @@ -2,6 +2,7 @@ import express, { Request, Response } from "express"; import { getAllBestsellerProducts, getAllProducts, + getHitsOfWeekProducts, getProductById, getProductsByName, } from "./product"; @@ -17,6 +18,10 @@ function createProductRouter() { const bestsellerProducts = await getAllBestsellerProducts(); resp.status(200).json(bestsellerProducts); }) + .get("/hitsofweek", async (_, resp: Response) => { + const hitsOfWeekProducts = await getHitsOfWeekProducts(); + resp.status(200).json(hitsOfWeekProducts); + }) .get("/search/:productName", async (req: Request, resp: Response) => { const allProducts = await getProductsByName(req.params.productName); resp.status(200).json(allProducts);