feat(F-106): completed feature
This commit is contained in:
@@ -12,6 +12,7 @@ interface OrderRow {
|
||||
discount_cents: number;
|
||||
tax_cents: number;
|
||||
total_cents: number;
|
||||
tracking_number: string | null;
|
||||
created_at: Date;
|
||||
updated_at: Date;
|
||||
}
|
||||
@@ -177,19 +178,62 @@ export class PgOrderRepository implements OrderRepository {
|
||||
return { ...toOrder(order), items: items.rows.map(toItem) };
|
||||
}
|
||||
|
||||
async updateState(id: string, state: OrderState): Promise<OrderView | undefined> {
|
||||
async updateState(id: string, state: OrderState, trackingNumber?: string): Promise<OrderView | undefined> {
|
||||
const result = await this.pool.query<OrderRow>(
|
||||
`UPDATE orders_orders SET state = $2, updated_at = now()
|
||||
`UPDATE orders_orders SET state = $2, tracking_number = COALESCE($3, tracking_number), updated_at = now()
|
||||
WHERE id = $1 RETURNING *`,
|
||||
[id, state],
|
||||
[id, state, trackingNumber ?? null],
|
||||
);
|
||||
const order = result.rows[0];
|
||||
if (!order) return undefined;
|
||||
const items = await this.pool.query<ItemRow>(
|
||||
'SELECT * FROM orders_items WHERE order_id = $1 ORDER BY created_at, id',
|
||||
[id],
|
||||
);
|
||||
return { ...toOrder(order), items: items.rows.map(toItem) };
|
||||
return this.findById(order.id);
|
||||
}
|
||||
|
||||
async replaceItems(
|
||||
id: string,
|
||||
items: OrderItemInput[],
|
||||
totals: { subtotalCents: number; discountCents: number; taxCents: number; totalCents: number },
|
||||
): Promise<OrderView | undefined> {
|
||||
const client = await this.pool.connect();
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
const orderResult = await client.query<OrderRow>(
|
||||
`UPDATE orders_orders
|
||||
SET subtotal_cents = $2, discount_cents = $3, tax_cents = $4, total_cents = $5, updated_at = now()
|
||||
WHERE id = $1 RETURNING *`,
|
||||
[id, totals.subtotalCents, totals.discountCents, totals.taxCents, totals.totalCents],
|
||||
);
|
||||
if (!orderResult.rows[0]) {
|
||||
await client.query('ROLLBACK');
|
||||
return undefined;
|
||||
}
|
||||
await client.query('DELETE FROM orders_items WHERE order_id = $1', [id]);
|
||||
for (const item of items) {
|
||||
await client.query<ItemRow>(
|
||||
`INSERT INTO orders_items (order_id, product_id, variant_id, sku, ean, name, unit_price_cents, discount_cents, tax_cents, quantity)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`,
|
||||
[
|
||||
id,
|
||||
item.productId,
|
||||
item.variantId,
|
||||
item.sku,
|
||||
item.ean,
|
||||
item.name,
|
||||
item.unitPriceCents,
|
||||
item.discountCents,
|
||||
item.taxCents,
|
||||
item.quantity,
|
||||
],
|
||||
);
|
||||
}
|
||||
await client.query('COMMIT');
|
||||
return this.findById(id);
|
||||
} catch (error) {
|
||||
await client.query('ROLLBACK');
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,6 +248,7 @@ function toOrder(row: OrderRow): Order {
|
||||
discountCents: row.discount_cents,
|
||||
taxCents: row.tax_cents,
|
||||
totalCents: row.total_cents,
|
||||
trackingNumber: row.tracking_number,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user