feat(POS-009): completed feature
This commit is contained in:
@@ -589,4 +589,55 @@ export async function registerPosRoutes(app: FastifyInstance, deps: PosRouteDeps
|
||||
return reply.code(201).send(result);
|
||||
});
|
||||
|
||||
|
||||
// ── POS-009: Customer search for POS ──────────────────────────────────────
|
||||
|
||||
app.get('/pos/customers/search', {
|
||||
schema: {
|
||||
tags: ['POS Terminal'],
|
||||
summary: 'Search customers for POS association',
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: { q: { type: 'string', minLength: 2 }, limit: { type: 'integer', minimum: 1, maximum: 20, default: 10 } },
|
||||
},
|
||||
response: { 401: errorSchema },
|
||||
} as FastifySchema,
|
||||
}, async (request, reply) => {
|
||||
const user = await authenticate(request);
|
||||
requireAnyRole(user, ['admin', 'pos_manager', 'pos_cashier'] as ReadonlyArray<Role>);
|
||||
const { q, limit = 10 } = request.query as { q?: string; limit?: number };
|
||||
if (!q || q.trim().length < 2) return reply.send({ items: [] });
|
||||
const result = await pool.query(
|
||||
`SELECT u.id, u.email, p.first_name AS "firstName", p.last_name AS "lastName", p.phone
|
||||
FROM identity_users u
|
||||
LEFT JOIN users_profiles p ON p.user_id = u.id
|
||||
WHERE u.email ILIKE $1 OR p.first_name ILIKE $1 OR p.last_name ILIKE $1 OR p.phone ILIKE $1
|
||||
ORDER BY p.last_name LIMIT $2`,
|
||||
[`%${q.trim()}%`, limit],
|
||||
);
|
||||
return reply.send({ items: result.rows });
|
||||
});
|
||||
|
||||
app.get<{ Params: { id: string } }>('/pos/customers/:id', {
|
||||
schema: {
|
||||
tags: ['POS Terminal'],
|
||||
summary: 'Get customer details for POS',
|
||||
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 result = await pool.query(
|
||||
`SELECT u.id, u.email, p.first_name AS "firstName", p.last_name AS "lastName", p.phone
|
||||
FROM identity_users u
|
||||
LEFT JOIN users_profiles p ON p.user_id = u.id
|
||||
WHERE u.id = $1 LIMIT 1`,
|
||||
[id],
|
||||
);
|
||||
if (!result.rows[0]) throw new AppError(404, 'NOT_FOUND', 'Customer not found');
|
||||
return reply.send(result.rows[0]);
|
||||
});
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user