feat(POS-014): completed feature

This commit is contained in:
chattie
2026-08-22 13:51:23 +02:00
parent d8f942d4bd
commit 4ad758de4e
10 changed files with 146 additions and 10 deletions

View File

@@ -985,4 +985,75 @@ export async function registerPosRoutes(app: FastifyInstance, deps: PosRouteDeps
]});
});
// ── POS-014: Shifts + tax rates + daily/end-of-day reports ───────────────
app.get('/pos/shifts', {
schema: { tags: ['POS Admin'], summary: 'List POS user shifts', querystring: { type: 'object', properties: { storeId: { type: 'string', format: 'uuid' }, date: { type: 'string' } } }, response: { 401: errorSchema, 403: errorSchema } } as FastifySchema,
}, async (request, reply) => {
const user = await authenticate(request);
requireRole(user, 'admin');
const { storeId, date } = request.query as { storeId?: string; date?: string };
const params: unknown[] = [];
let filter = '';
if (storeId) { params.push(storeId); filter += ` AND cs.store_id = $${params.length}`; }
if (date) { params.push(date); filter += ` AND DATE(cs.created_at) = $${params.length}`; }
const result = await pool.query(`SELECT cs.id, cs.store_id AS "storeId", cs.user_id AS "userId", u.email AS "userEmail", cs.status, cs.opening_cash_cents AS "openingCash", cs.expected_cash_cents AS "expectedCash", cs.created_at AS "openedAt" FROM pos_cash_sessions cs LEFT JOIN identity_users u ON u.id = cs.user_id WHERE 1=1${filter} ORDER BY cs.created_at DESC LIMIT 50`, params);
return reply.send({ items: result.rows });
});
app.get('/pos/tax-rates', {
schema: { tags: ['POS Terminal'], summary: 'Get configured tax rates' } as FastifySchema,
}, async (request, reply) => {
await authenticate(request);
return reply.send({ rates: [{ code: 'IVA21', name: 'IVA 21%', percent: 21, active: true }, { code: 'IVA10', name: 'IVA 10%', percent: 10, active: true }, { code: 'IVA04', name: 'IVA 4%', percent: 4, active: false }] });
});
app.get('/pos/stores', {
schema: { tags: ['POS Admin'], summary: 'List all POS stores', response: { 401: errorSchema, 403: errorSchema } } as FastifySchema,
}, async (request, reply) => {
const user = await authenticate(request);
requireRole(user, 'admin');
const result = await pool.query('SELECT id, name, address, active FROM pos_stores ORDER BY name');
return reply.send({ items: result.rows });
});
app.get('/pos/notifications', {
schema: { tags: ['POS Terminal'], summary: 'Get active POS notifications', querystring: { type: 'object', properties: { storeId: { type: 'string', format: 'uuid' } } }, response: { 401: errorSchema } } as FastifySchema,
}, async (request, reply) => {
const user = await authenticate(request);
requireAnyRole(user, ['admin', 'pos_manager', 'pos_cashier'] as ReadonlyArray<Role>);
const { storeId } = request.query as { storeId?: string };
// Return low-stock notifications + session alerts
const params: unknown[] = storeId ? [storeId] : [];
const lowStock = await pool.query(`SELECT v.name, v.sku, COALESCE(s.quantity, 0) AS stock FROM catalog_product_variants v LEFT JOIN LATERAL (SELECT quantity FROM inventory_stock WHERE variant_id = v.id AND store_id = $${params.length} LIMIT 1) s ON true WHERE v.active = true AND COALESCE(s.quantity, 0) < 5 LIMIT 10`, params);
const notifications = lowStock.rows.map((r: Record<string, unknown>) => ({ type: 'low-stock', message: `Stock bajo: ${r.name} (${r.stock} uds)`, severity: 'warning' }));
return reply.send({ items: notifications });
});
app.get('/pos/reports/daily', {
schema: { tags: ['POS Admin'], summary: 'Daily sales report', querystring: { type: 'object', required: ['storeId', 'date'], properties: { storeId: { type: 'string', format: 'uuid' }, date: { type: 'string' } } }, response: { 401: errorSchema, 403: errorSchema } } as FastifySchema,
}, async (request, reply) => {
const user = await authenticate(request);
requireRole(user, 'admin');
const { storeId, date } = request.query as { storeId?: string; date?: string };
const sessions = await pool.query(`SELECT cs.id, cs.user_id, u.email, cs.status, COALESCE(cs.expected_cash_cents, 0) AS expected_cash, cs.opening_cash_cents AS opening_cash, cs.created_at FROM pos_cash_sessions cs LEFT JOIN identity_users u ON u.id = cs.user_id WHERE cs.store_id = $1 AND DATE(cs.created_at) = $2`, [storeId, date]);
const salesCount = await pool.query(`SELECT COUNT(*) AS cnt, COALESCE(SUM(o.total_cents), 0) AS total, COALESCE(SUM(o.discount_cents), 0) AS discount FROM orders_orders o WHERE o.idempotency_key IS NOT NULL AND DATE(o.created_at) = $1`, [date]);
return reply.send({ date, storeId, sessions: sessions.rows, summary: salesCount.rows[0] });
});
app.get('/pos/reports/end-of-day', {
schema: { tags: ['POS Admin'], summary: 'End-of-day report', querystring: { type: 'object', required: ['storeId', 'date'], properties: { storeId: { type: 'string', format: 'uuid' }, date: { type: 'string' } } }, response: { 401: errorSchema, 403: errorSchema } } as FastifySchema,
}, async (request, reply) => {
const user = await authenticate(request);
requireRole(user, 'admin');
const { storeId, date } = request.query as { storeId?: string; date?: string };
const params = [storeId, date];
const sessions = await pool.query(`SELECT cs.id, cs.status, COALESCE(cs.expected_cash_cents, 0) AS expected, cs.opening_cash_cents AS opening FROM pos_cash_sessions cs WHERE cs.store_id = $1 AND DATE(cs.created_at) = $2`, params);
const sales = await pool.query(`SELECT COUNT(*) AS cnt, COALESCE(SUM(total_cents),0) AS total, COALESCE(SUM(discount_cents),0) AS disc FROM orders_orders WHERE idempotency_key IS NOT NULL AND DATE(created_at) = $1`, [date]);
const byPay = await pool.query(`SELECT provider, COUNT(*) AS cnt, SUM(amount_cents) AS total FROM payments_transactions WHERE status='COMPLETED' AND DATE(created_at) = $1 GROUP BY provider`, [date]);
return reply.send({ date, storeId, sessions: sessions.rows, sales: sales.rows[0], byPayment: byPay.rows });
});
}