feat(F-153): completed feature
This commit is contained in:
@@ -5,6 +5,7 @@ import type { Order, OrderItem, OrderItemInput, OrderState, OrderView } from '..
|
||||
interface OrderRow {
|
||||
id: string;
|
||||
user_id: string;
|
||||
email: string | null;
|
||||
idempotency_key: string | null;
|
||||
state: OrderState;
|
||||
currency: 'EUR';
|
||||
@@ -92,7 +93,7 @@ export class PgOrderRepository implements OrderRepository {
|
||||
|
||||
async findAll(): Promise<OrderView[]> {
|
||||
const result = await this.pool.query<OrderRow>(
|
||||
'SELECT * FROM orders_orders ORDER BY created_at DESC',
|
||||
'SELECT o.*, u.email FROM orders_orders o LEFT JOIN identity_users u ON u.id = o.user_id ORDER BY o.created_at DESC',
|
||||
);
|
||||
return Promise.all(
|
||||
result.rows.map(async (order) => {
|
||||
@@ -100,7 +101,7 @@ export class PgOrderRepository implements OrderRepository {
|
||||
'SELECT * FROM orders_items WHERE order_id = $1 ORDER BY id',
|
||||
[order.id],
|
||||
);
|
||||
return { ...toOrder(order), items: items.rows.map(toItem) };
|
||||
return toOrderView(order, items.rows.map(toItem));
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -132,7 +133,7 @@ export class PgOrderRepository implements OrderRepository {
|
||||
const total = Number(countResult.rows[0]?.count ?? '0');
|
||||
values.push(filters.limit, filters.offset);
|
||||
const result = await this.pool.query<OrderRow>(
|
||||
`SELECT o.* FROM orders_orders o
|
||||
`SELECT o.*, u.email FROM orders_orders o
|
||||
LEFT JOIN identity_users u ON u.id = o.user_id
|
||||
${whereSql}
|
||||
ORDER BY o.created_at DESC
|
||||
@@ -145,7 +146,7 @@ export class PgOrderRepository implements OrderRepository {
|
||||
'SELECT * FROM orders_items WHERE order_id = $1 ORDER BY id',
|
||||
[order.id],
|
||||
);
|
||||
return { ...toOrder(order), items: itemResult.rows.map(toItem) };
|
||||
return toOrderView(order, itemResult.rows.map(toItem));
|
||||
}),
|
||||
);
|
||||
return { items, total };
|
||||
@@ -153,7 +154,7 @@ export class PgOrderRepository implements OrderRepository {
|
||||
|
||||
async findById(id: string): Promise<OrderView | undefined> {
|
||||
const orderResult = await this.pool.query<OrderRow>(
|
||||
'SELECT * FROM orders_orders WHERE id = $1',
|
||||
'SELECT o.*, u.email FROM orders_orders o LEFT JOIN identity_users u ON u.id = o.user_id WHERE o.id = $1',
|
||||
[id],
|
||||
);
|
||||
const order = orderResult.rows[0];
|
||||
@@ -162,12 +163,12 @@ export class PgOrderRepository implements OrderRepository {
|
||||
'SELECT * FROM orders_items WHERE order_id = $1 ORDER BY created_at, id',
|
||||
[id],
|
||||
);
|
||||
return { ...toOrder(order), items: items.rows.map(toItem) };
|
||||
return toOrderView(order, items.rows.map(toItem));
|
||||
}
|
||||
|
||||
async findByIdAndUserId(id: string, userId: string): Promise<OrderView | undefined> {
|
||||
const orderResult = await this.pool.query<OrderRow>(
|
||||
'SELECT * FROM orders_orders WHERE id = $1 AND user_id = $2',
|
||||
'SELECT o.*, u.email FROM orders_orders o LEFT JOIN identity_users u ON u.id = o.user_id WHERE o.id = $1 AND o.user_id = $2',
|
||||
[id, userId],
|
||||
);
|
||||
const order = orderResult.rows[0];
|
||||
@@ -176,7 +177,7 @@ export class PgOrderRepository implements OrderRepository {
|
||||
'SELECT * FROM orders_items WHERE order_id = $1 ORDER BY created_at, id',
|
||||
[id],
|
||||
);
|
||||
return { ...toOrder(order), items: items.rows.map(toItem) };
|
||||
return toOrderView(order, items.rows.map(toItem));
|
||||
}
|
||||
|
||||
async updateState(id: string, state: OrderState, trackingNumber?: string, courier?: string): Promise<OrderView | undefined> {
|
||||
@@ -256,6 +257,14 @@ function toOrder(row: OrderRow): Order {
|
||||
};
|
||||
}
|
||||
|
||||
function toOrderView(row: OrderRow, items: OrderItem[]): OrderView {
|
||||
return {
|
||||
...toOrder(row),
|
||||
email: row.email,
|
||||
items,
|
||||
};
|
||||
}
|
||||
|
||||
function toItem(row: ItemRow): OrderItem {
|
||||
return {
|
||||
id: row.id,
|
||||
|
||||
Reference in New Issue
Block a user