feat(POS-004): completed feature
This commit is contained in:
@@ -50,23 +50,23 @@ export class PgStoreRepository implements PosStoreRepository {
|
||||
async list(options: ListStoresOptions = {}): Promise<{ stores: PosStore[]; total: number }> {
|
||||
const { active = true, limit = 50, offset = 0 } = options;
|
||||
const where = active !== undefined ? 'WHERE active = $1' : '';
|
||||
const params = active !== undefined ? [active] : [];
|
||||
params.push(limit, offset);
|
||||
const countParams: number[] = active !== undefined ? [active ? 1 : 0] : [];
|
||||
const listParams: number[] = [...countParams, limit, offset];
|
||||
|
||||
const [countResult, listResult] = await Promise.all([
|
||||
this.pool.query<{ count: string }>(
|
||||
`SELECT COUNT(*) as count FROM pos_stores ${where}`,
|
||||
params.slice(0, active !== undefined ? 1 : 0),
|
||||
countParams,
|
||||
),
|
||||
this.pool.query<StoreRow>(
|
||||
`SELECT * FROM pos_stores ${where} ORDER BY name LIMIT $${params.length - 1} OFFSET $${params.length}`,
|
||||
params,
|
||||
`SELECT * FROM pos_stores ${where} ORDER BY name LIMIT $${listParams.length - 1} OFFSET $${listParams.length}`,
|
||||
listParams,
|
||||
),
|
||||
]);
|
||||
|
||||
return {
|
||||
stores: listResult.rows.map(toStore),
|
||||
total: parseInt(countResult.rows[0].count, 10),
|
||||
total: parseInt(countResult.rows[0]?.count ?? '0', 10),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user