feat(POS-012): completed feature
This commit is contained in:
@@ -804,4 +804,73 @@ export async function registerPosRoutes(app: FastifyInstance, deps: PosRouteDeps
|
||||
return reply.send({ items: result.rows });
|
||||
});
|
||||
|
||||
|
||||
|
||||
// ── POS-012: Refund + receipt print + analytics ────────────────────────────
|
||||
|
||||
app.post<{ Params: { id: string } }>('/pos/sales/:id/refund', {
|
||||
schema: {
|
||||
tags: ['POS Terminal'],
|
||||
summary: 'Refund a POS sale',
|
||||
params: { type: 'object', properties: { id: { type: 'string', format: 'uuid' } } },
|
||||
body: {
|
||||
type: 'object',
|
||||
required: ['refundAmountCents', 'reason'],
|
||||
properties: { refundAmountCents: { type: 'integer', minimum: 1 }, reason: { type: 'string', minLength: 1 } },
|
||||
},
|
||||
response: { 400: errorSchema, 401: errorSchema, 403: errorSchema, 404: errorSchema },
|
||||
} as FastifySchema,
|
||||
}, async (request, reply) => {
|
||||
const user = await authenticate(request);
|
||||
requireAnyRole(user, ['admin', 'pos_manager', 'pos_cashier'] as ReadonlyArray<Role>);
|
||||
const { id } = request.params;
|
||||
const body = (request.body ?? {}) as { refundAmountCents?: number; reason?: string };
|
||||
const order = await pool.query<{ id: string; total_cents: number }>('SELECT id, total_cents FROM orders_orders WHERE id = $1', [id]);
|
||||
if (!order.rows[0]) throw new AppError(404, 'NOT_FOUND', 'Order not found');
|
||||
if ((body.refundAmountCents ?? 0) > (order.rows[0].total_cents ?? 0)) throw new AppError(400, 'REFUND_EXCEEDS', 'Refund exceeds amount');
|
||||
await pool.query(`INSERT INTO payments_transactions (provider, provider_event_id, provider_payment_id, order_id, amount_cents, currency, status, raw) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`, ['pos_refund', `ref-${id}`, `ref-${Date.now()}`, id, body.refundAmountCents, 'EUR', 'COMPLETED', JSON.stringify({ reason: body.reason, by: user.id })]);
|
||||
await pool.query(`INSERT INTO orders_order_events (order_id, event, actor_id, metadata) VALUES ($1, 'REFUNDED', $2, $3)`, [id, user.id, JSON.stringify({ amt: body.refundAmountCents, reason: body.reason })]);
|
||||
return reply.send({ ok: true, refundedCents: body.refundAmountCents });
|
||||
});
|
||||
|
||||
app.get<{ Params: { id: string } }>('/pos/sales/:id/print', {
|
||||
schema: {
|
||||
tags: ['POS Terminal'],
|
||||
summary: 'Get printable receipt',
|
||||
params: { type: 'object', properties: { id: { type: 'string', format: 'uuid' } } },
|
||||
response: { 401: errorSchema, 404: errorSchema },
|
||||
} as FastifySchema,
|
||||
}, async (request, reply) => {
|
||||
const user = await authenticate(request);
|
||||
requireAnyRole(user, ['admin', 'pos_manager', 'pos_cashier'] as ReadonlyArray<Role>);
|
||||
const { id } = request.params;
|
||||
const order = await pool.query('SELECT * FROM orders_orders WHERE id = $1', [id]);
|
||||
if (!order.rows[0]) throw new AppError(404, 'NOT_FOUND', 'Sale not found');
|
||||
const items = await pool.query('SELECT name, quantity, unit_price_cents, discount_cents, tax_cents FROM orders_items WHERE order_id = $1', [id]);
|
||||
const payments = await pool.query<{ amount_cents: number; provider: string }>('SELECT amount_cents, provider FROM payments_transactions WHERE order_id = $1', [id]);
|
||||
return reply.send({ receipt: { orderId: id, storeName: 'Mercado de Vida', terminalName: 'TPV', totalCents: order.rows[0].total_cents, createdAt: order.rows[0].created_at, items: items.rows.map(i => ({ name: i.name, qty: i.quantity, unitPrice: i.unit_price_cents, discount: i.discount_cents, tax: i.tax_cents, line: (i.unit_price_cents - i.discount_cents + i.tax_cents) * i.quantity })), payments: payments.rows.map(p => ({ amountCents: p.amount_cents, kind: p.provider })) } });
|
||||
});
|
||||
|
||||
app.get('/pos/analytics/summary', {
|
||||
schema: {
|
||||
tags: ['POS Admin'],
|
||||
summary: 'POS sales analytics',
|
||||
querystring: { type: 'object', properties: { storeId: { type: 'string', format: 'uuid' }, from: { type: 'string' }, to: { type: 'string' } } },
|
||||
response: { 401: errorSchema, 403: errorSchema },
|
||||
} as FastifySchema,
|
||||
}, async (request, reply) => {
|
||||
const user = await authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const { storeId, from, to } = request.query as { storeId?: string; from?: string; to?: string };
|
||||
const params: unknown[] = [];
|
||||
let df = '';
|
||||
if (from) { params.push(from); df += ` AND o.created_at >= $${params.length}`; }
|
||||
if (to) { params.push(to); df += ` AND o.created_at <= $${params.length}`; }
|
||||
let sf = '';
|
||||
if (storeId) { params.push(storeId); sf = ` AND cs.store_id = $${params.length}`; }
|
||||
const sum = await pool.query(`SELECT COUNT(*) AS cnt, COALESCE(SUM(o.total_cents),0) AS total, COALESCE(SUM(o.discount_cents),0) AS disc FROM orders_orders o LEFT JOIN pos_cash_sessions cs ON cs.id = o.cash_session_id WHERE o.idempotency_key IS NOT NULL${df}${sf}`, params);
|
||||
const byPay = await pool.query(`SELECT provider, COUNT(*) AS cnt, SUM(amount_cents) AS total FROM payments_transactions WHERE status='COMPLETED'${df} GROUP BY provider`, params);
|
||||
return reply.send({ summary: sum.rows[0], byPayment: byPay.rows });
|
||||
});
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user