diff --git a/src/orders/place-order.ts b/src/orders/place-order.ts index 623e29f..10a0212 100644 --- a/src/orders/place-order.ts +++ b/src/orders/place-order.ts @@ -5,6 +5,7 @@ import { findOrder, insertOrder, sumSalesByProduct } from './repository.ts'; import type { NewOrder, Order, OrderLine, ProductSales } from './types.ts'; export const MAX_ITEMS_PER_ORDER = 5; +export const MAX_GIFT_NOTE_LENGTH = 140; function checkItems(order: NewOrder): void { if (order.items.length < 1 || order.items.length > MAX_ITEMS_PER_ORDER) { @@ -12,6 +13,9 @@ function checkItems(order: NewOrder): void { } const ids = order.items.map((item) => item.productId); if (new Set(ids).size !== ids.length) throw invalidInput('Una orden no repite un producto'); + if (order.giftNote !== undefined && order.giftNote.length > MAX_GIFT_NOTE_LENGTH) { + throw invalidInput(`La dedicatoria tiene como máximo ${MAX_GIFT_NOTE_LENGTH} caracteres`); + } } /** Descuenta el stock y guarda la orden, todo o nada. */ @@ -34,7 +38,7 @@ export async function placeOrder(order: NewOrder): Promise { unitPriceCents: product.priceCents, }); } - return insertOrder(tx, order.buyerEmail, lines); + return insertOrder(tx, order.buyerEmail, lines, order.giftNote); }); } diff --git a/src/orders/repository.ts b/src/orders/repository.ts index 04e41b9..722d8f7 100644 --- a/src/orders/repository.ts +++ b/src/orders/repository.ts @@ -3,16 +3,21 @@ import type { Order, OrderLine, ProductSales } from './types.ts'; // El único archivo que conoce las tablas del esquema orders. -type OrderRow = { id: number; buyer_email: string; total_cents: number; created_at: Date }; +type OrderRow = { id: number; buyer_email: string; total_cents: number; created_at: Date; gift_note: string | null }; type LineRow = { product_id: number; title: string; quantity: number; unit_price_cents: number }; type SalesRow = { product_id: number; units: number; revenue_cents: number }; -export async function insertOrder(tx: Tx, buyerEmail: string, lines: OrderLine[]): Promise { +export async function insertOrder( + tx: Tx, + buyerEmail: string, + lines: OrderLine[], + giftNote?: string, +): Promise { const totalCents = lines.reduce((sum, line) => sum + line.quantity * line.unitPriceCents, 0); const inserted = await tx.query( - `INSERT INTO orders.orders (buyer_email, total_cents) VALUES ($1, $2) - RETURNING id, buyer_email, total_cents, created_at`, - [buyerEmail, totalCents], + `INSERT INTO orders.orders (buyer_email, total_cents, gift_note) VALUES ($1, $2, $3) + RETURNING id, buyer_email, total_cents, created_at, gift_note`, + [buyerEmail, totalCents, giftNote ?? null], ); const order = inserted.rows[0]!; for (const line of lines) { @@ -27,13 +32,14 @@ export async function insertOrder(tx: Tx, buyerEmail: string, lines: OrderLine[] buyerEmail: order.buyer_email, totalCents: order.total_cents, createdAt: order.created_at.toISOString(), + giftNote: order.gift_note ?? undefined, lines, }; } export async function findOrder(id: number): Promise { const orders = await query( - 'SELECT id, buyer_email, total_cents, created_at FROM orders.orders WHERE id = $1', + 'SELECT id, buyer_email, total_cents, created_at, gift_note FROM orders.orders WHERE id = $1', [id], ); const order = orders[0]; @@ -50,6 +56,7 @@ export async function findOrder(id: number): Promise { buyerEmail: order.buyer_email, totalCents: order.total_cents, createdAt: order.created_at.toISOString(), + giftNote: order.gift_note ?? undefined, lines: lines.map((line) => ({ productId: line.product_id, title: line.title, diff --git a/src/orders/routes.ts b/src/orders/routes.ts index 5fbc642..bb91769 100644 --- a/src/orders/routes.ts +++ b/src/orders/routes.ts @@ -26,7 +26,10 @@ function parseNewOrder(body: unknown): NewOrder { throw invalidInput('buyerEmail no es un correo válido'); } if (!Array.isArray(body.items)) throw invalidInput('items debe ser una lista'); - return { buyerEmail: body.buyerEmail.toLowerCase(), items: body.items.map(parseItem) }; + if (body.giftNote !== undefined && typeof body.giftNote !== 'string') { + throw invalidInput('giftNote debe ser un texto'); + } + return { buyerEmail: body.buyerEmail.toLowerCase(), items: body.items.map(parseItem), giftNote: body.giftNote }; } export const ordersRoutes: Route[] = [ diff --git a/src/orders/types.ts b/src/orders/types.ts index ea0c281..9cd9464 100644 --- a/src/orders/types.ts +++ b/src/orders/types.ts @@ -3,6 +3,7 @@ export type OrderItem = { productId: number; quantity: number }; export type NewOrder = { buyerEmail: string; items: OrderItem[]; + giftNote?: string; }; export type OrderLine = { @@ -17,6 +18,7 @@ export type Order = { buyerEmail: string; totalCents: number; createdAt: string; + giftNote?: string; lines: OrderLine[]; };