feat(F-102): completed feature

This commit is contained in:
chattie
2026-08-21 12:02:15 +02:00
parent e46faa869a
commit 027cacd871
31 changed files with 510 additions and 80 deletions

View File

@@ -57,6 +57,19 @@ export async function registerCheckoutRoutes(
orderLookup,
metrics,
tracer,
getCartWeightKg: async (items) => {
if (items.length === 0) return 0;
const productIds = [...new Set(items.map((item) => item.productId))];
const result = await deps.pool.query<{ id: string; unit_weight_kg: string | number }>(
`SELECT id, unit_weight_kg FROM catalog_products WHERE id = ANY($1::uuid[])`,
[productIds],
);
const weights = new Map(result.rows.map((row) => [row.id, Number(row.unit_weight_kg ?? 1)]));
return items.reduce(
(total, item) => total + (weights.get(item.productId) ?? 1) * item.quantity,
0,
);
},
});
const checkoutSchema: FastifySchema = {

View File

@@ -27,6 +27,10 @@ export interface CheckoutServiceDeps {
orderLookup: CheckoutOrderLookup;
metrics: CheckoutMetrics;
tracer?: Tracer;
/** Peso total del carrito en kg (cantidad × peso unitario del producto). */
getCartWeightKg?: (
items: Array<{ productId: string; quantity: number }>,
) => Promise<number>;
}
export interface CheckoutCommand {
@@ -132,8 +136,18 @@ export class CheckoutService {
}
}
const cartWeightKg = this.deps.getCartWeightKg
? await this.deps.getCartWeightKg(
cart.items.map((item) => ({ productId: item.productId, quantity: item.quantity })),
).catch(() => 0)
: 0;
const shipping = await this.deps.shipping
.calculate(Math.max(0, netSubtotalCents + taxCents - discountCents), command.address)
.calculate(
Math.max(0, netSubtotalCents + taxCents - discountCents),
command.address,
cartWeightKg,
)
.catch((error: unknown) => {
if (error instanceof Error && error.name === 'ShippingZoneNotFoundError') return null;
throw error;