feat(F-140): completed feature
This commit is contained in:
@@ -15,9 +15,28 @@ interface OrderRow {
|
||||
total_cents: number;
|
||||
tracking_number: string | null;
|
||||
courier: string | null;
|
||||
customer_name: string | null;
|
||||
customer_phone: string | null;
|
||||
phone: string | null;
|
||||
created_at: Date;
|
||||
updated_at: Date;
|
||||
}
|
||||
|
||||
interface AddressRow {
|
||||
recipient_name: string;
|
||||
street: string;
|
||||
city: string;
|
||||
postal_code: string;
|
||||
country: string;
|
||||
}
|
||||
|
||||
interface PaymentRow {
|
||||
provider: string;
|
||||
status: string;
|
||||
amount_cents: number;
|
||||
provider_payment_id: string | null;
|
||||
created_at: Date;
|
||||
}
|
||||
interface ItemRow {
|
||||
id: string;
|
||||
order_id: string;
|
||||
@@ -154,7 +173,10 @@ export class PgOrderRepository implements OrderRepository {
|
||||
|
||||
async findById(id: string): Promise<OrderView | undefined> {
|
||||
const orderResult = await this.pool.query<OrderRow>(
|
||||
'SELECT o.*, u.email FROM orders_orders o LEFT JOIN identity_users u ON u.id = o.user_id WHERE o.id = $1',
|
||||
`SELECT o.*, u.email, u.phone
|
||||
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];
|
||||
@@ -163,12 +185,32 @@ export class PgOrderRepository implements OrderRepository {
|
||||
'SELECT * FROM orders_items WHERE order_id = $1 ORDER BY created_at, id',
|
||||
[id],
|
||||
);
|
||||
return toOrderView(order, items.rows.map(toItem));
|
||||
const [shippingRow] = await this.pool.query<AddressRow>(
|
||||
`SELECT recipient_name, street, city, postal_code, country
|
||||
FROM users_addresses WHERE user_id = $1 AND type = 'shipping' LIMIT 1`,
|
||||
[order.user_id],
|
||||
).then((r) => r.rows);
|
||||
const [billingRow] = await this.pool.query<AddressRow>(
|
||||
`SELECT recipient_name, street, city, postal_code, country
|
||||
FROM users_addresses WHERE user_id = $1 AND type = 'billing' LIMIT 1`,
|
||||
[order.user_id],
|
||||
).then((r) => r.rows);
|
||||
const [paymentRow] = await this.pool.query<PaymentRow>(
|
||||
`SELECT provider, status, amount_cents, provider_payment_id, created_at
|
||||
FROM payments_transactions
|
||||
WHERE order_id = $1 AND status = 'succeeded'
|
||||
ORDER BY created_at DESC LIMIT 1`,
|
||||
[id],
|
||||
).then((r) => r.rows);
|
||||
return toOrderView(order, items.rows.map(toItem), shippingRow, billingRow, paymentRow);
|
||||
}
|
||||
|
||||
async findByIdAndUserId(id: string, userId: string): Promise<OrderView | undefined> {
|
||||
const orderResult = await this.pool.query<OrderRow>(
|
||||
'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',
|
||||
`SELECT o.*, u.email, u.phone
|
||||
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];
|
||||
@@ -257,11 +299,24 @@ function toOrder(row: OrderRow): Order {
|
||||
};
|
||||
}
|
||||
|
||||
function toOrderView(row: OrderRow, items: OrderItem[]): OrderView {
|
||||
function toOrderView(
|
||||
row: OrderRow,
|
||||
items: OrderItem[],
|
||||
shipping?: AddressRow | null,
|
||||
billing?: AddressRow | null,
|
||||
payment?: PaymentRow | null,
|
||||
): OrderView {
|
||||
const last4 = payment?.provider_payment_id
|
||||
? payment.provider_payment_id.replace(/[^0-9]/g, '').slice(-4)
|
||||
: null;
|
||||
return {
|
||||
...toOrder(row),
|
||||
email: row.email,
|
||||
phone: row.phone ?? null,
|
||||
items,
|
||||
shippingAddress: shipping ? { recipientName: shipping.recipient_name, street: shipping.street, city: shipping.city, postalCode: shipping.postal_code, country: shipping.country } : null,
|
||||
billingAddress: billing ? { recipientName: billing.recipient_name, street: billing.street, city: billing.city, postalCode: billing.postal_code, country: billing.country } : null,
|
||||
payment: payment ? { provider: payment.provider, status: payment.status, amountCents: payment.amount_cents, last4, createdAt: payment.created_at } : null,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user