feat(F-103): completed feature

This commit is contained in:
chattie
2026-08-21 07:55:18 +02:00
parent 5177a851aa
commit c07776822d
40 changed files with 886 additions and 156 deletions

View File

@@ -103,6 +103,52 @@ export class PgOrderRepository implements OrderRepository {
);
}
async search(filters: {
state?: string;
q?: string;
limit: number;
offset: number;
}): Promise<{ items: OrderView[]; total: number }> {
const where: string[] = [];
const values: unknown[] = [];
if (filters.state) {
values.push(filters.state);
where.push(`o.state = $${values.length}`);
}
if (filters.q?.trim()) {
values.push(`%${filters.q.trim()}%`);
where.push(`(o.id::text ILIKE $${values.length} OR u.email ILIKE $${values.length})`);
}
const whereSql = where.length ? `WHERE ${where.join(' AND ')}` : '';
const countResult = await this.pool.query<{ count: string }>(
`SELECT COUNT(*)::text AS count
FROM orders_orders o
LEFT JOIN identity_users u ON u.id = o.user_id
${whereSql}`,
values,
);
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
LEFT JOIN identity_users u ON u.id = o.user_id
${whereSql}
ORDER BY o.created_at DESC
LIMIT $${values.length - 1} OFFSET $${values.length}`,
values,
);
const items = await Promise.all(
result.rows.map(async (order) => {
const itemResult = await this.pool.query<ItemRow>(
'SELECT * FROM orders_items WHERE order_id = $1 ORDER BY id',
[order.id],
);
return { ...toOrder(order), items: itemResult.rows.map(toItem) };
}),
);
return { items, total };
}
async findById(id: string): Promise<OrderView | undefined> {
const orderResult = await this.pool.query<OrderRow>(
'SELECT * FROM orders_orders WHERE id = $1',