-
Notifications
You must be signed in to change notification settings - Fork 0
/
mappers.ts
35 lines (29 loc) · 909 Bytes
/
mappers.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
import { OrderDTO, OrderListResponseData } from "./types/dto.ts";
import { Order, OrderItem, Product } from "./types/models.ts";
//////////////////////////////////////////////////
/// Map the model data to Response data (DTO) ///
//////////////////////////////////////////////////
function toOrdersWithProducts(
orders: (
& Pick<OrderItem, "order_id">
& Product
& Pick<Order, "user_id" | "status">
)[],
): OrderListResponseData["orders"] {
const orderWithProducts: {
[orderId: number]: OrderDTO;
} = {};
orders.forEach(({ order_id, user_id, status, ...product }) => {
if (!orderWithProducts[order_id]) {
orderWithProducts[order_id] = {
user_id,
id: order_id,
status,
items: [],
};
}
orderWithProducts[order_id].items.push({ product });
});
return Object.values(orderWithProducts);
}
export { toOrdersWithProducts };