-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathcart.ts
executable file
·56 lines (43 loc) · 1.65 KB
/
cart.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
import type { NextApiHandler } from 'next';
import CartGateway from '../../gateways/rpc/Cart.gateway';
import { AddItemRequest, Empty } from '../../protos/demo';
import ProductCatalogService from '../../services/ProductCatalog.service';
import { IProductCart, IProductCartItem } from '../../types/Cart';
import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware';
type TResponse = IProductCart | Empty;
const handler: NextApiHandler<TResponse> = async ({ method, body, query }, res) => {
switch (method) {
case 'GET': {
const { sessionId = '', currencyCode = '' } = query;
const { userId, items } = await CartGateway.getCart(sessionId as string);
const productList: IProductCartItem[] = await Promise.all(
items.map(async ({ productId, quantity }) => {
const product = await ProductCatalogService.getProduct(productId, currencyCode as string);
return {
productId,
quantity,
product,
};
})
);
return res.status(200).json({ userId, items: productList });
}
case 'POST': {
const { userId, item } = body as AddItemRequest;
await CartGateway.addItem(userId, item!);
const cart = await CartGateway.getCart(userId);
return res.status(200).json(cart);
}
case 'DELETE': {
const { userId } = body as AddItemRequest;
await CartGateway.emptyCart(userId);
return res.status(204).send('');
}
default: {
return res.status(405);
}
}
};
export default InstrumentationMiddleware(handler);