diff --git a/src/db/order-repository.ts b/src/db/order-repository.ts index e9d7f51..3f53b48 100644 --- a/src/db/order-repository.ts +++ b/src/db/order-repository.ts @@ -1,15 +1,20 @@ import type { Order, OrderLine } from '../types.ts'; import { query, type Tx } from './pool.ts'; -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 }; -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 (buyer_email, total_cents) VALUES ($1, $2) - RETURNING id, buyer_email, total_cents, created_at`, - [buyerEmail, totalCents], + `INSERT INTO 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) { @@ -23,14 +28,16 @@ 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 WHERE id = $1', [ - id, - ]); + const orders = await query( + 'SELECT id, buyer_email, total_cents, created_at, gift_note FROM orders WHERE id = $1', + [id], + ); const order = orders[0]; if (!order) return undefined; // El título no se guarda en la línea: se trae del producto con un JOIN. @@ -47,6 +54,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/http/order-handlers.ts b/src/http/order-handlers.ts index 402727f..246916d 100644 --- a/src/http/order-handlers.ts +++ b/src/http/order-handlers.ts @@ -29,7 +29,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 placeOrderHandler: Handler = async ({ body }) => { diff --git a/src/services/order-service.ts b/src/services/order-service.ts index a8e0143..b0fefc7 100644 --- a/src/services/order-service.ts +++ b/src/services/order-service.ts @@ -5,6 +5,7 @@ import { invalidInput, notFound, outOfStock } from '../errors.ts'; import type { NewOrder, Order, OrderLine } 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. */ @@ -35,7 +39,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/types.ts b/src/types.ts index 75005dd..560375e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,6 +16,7 @@ export type OrderItem = { productId: number; quantity: number }; export type NewOrder = { buyerEmail: string; items: OrderItem[]; + giftNote?: string; }; export type OrderLine = { @@ -30,6 +31,7 @@ export type Order = { buyerEmail: string; totalCents: number; createdAt: string; + giftNote?: string; lines: OrderLine[]; };