diff options
Diffstat (limited to 'src/frontend/pages/api/recommendations.ts')
| -rw-r--r-- | src/frontend/pages/api/recommendations.ts | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/frontend/pages/api/recommendations.ts b/src/frontend/pages/api/recommendations.ts new file mode 100644 index 0000000..dd975a9 --- /dev/null +++ b/src/frontend/pages/api/recommendations.ts @@ -0,0 +1,33 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +import type { NextApiRequest, NextApiResponse } from 'next'; +import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware'; +import RecommendationsGateway from '../../gateways/rpc/Recommendations.gateway'; +import { Empty, Product } from '../../protos/demo'; +import ProductCatalogService from '../../services/ProductCatalog.service'; + +type TResponse = Product[] | Empty; + +const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse<TResponse>) => { + switch (method) { + case 'GET': { + const { productIds = [], sessionId = '', currencyCode = '' } = query; + const { productIds: productList } = await RecommendationsGateway.listRecommendations( + sessionId as string, + productIds as string[] + ); + const recommendedProductList = await Promise.all( + productList.slice(0, 4).map(id => ProductCatalogService.getProduct(id, currencyCode as string)) + ); + + return res.status(200).json(recommendedProductList); + } + + default: { + return res.status(405).send(''); + } + } +}; + +export default InstrumentationMiddleware(handler); |
