Added some small enchances

This commit is contained in:
Oleksandr Prokhorenko
2026-09-24 22:41:06 +03:00
parent 9ccbac4aca
commit 7f3a56a4ca
21 changed files with 441 additions and 211 deletions
+5 -5
View File
@@ -64,20 +64,20 @@ export class CrmController {
});
}
@Get('merchants') async merchants(@Req() req: AuthRequest, @Query() query: unknown) {
const { page: requestedPage } = merchantQuerySchema.parse(query);
const { page: requestedPage, pageSize } = merchantQuerySchema.parse(query);
const where = merchantWhere(req.user, query);
return this.db.$transaction(
async (tx) => {
const total = await tx.merchant.count({ where });
const page = Math.min(requestedPage, Math.max(1, Math.ceil(total / 8)));
const page = Math.min(requestedPage, Math.max(1, Math.ceil(total / pageSize)));
const items = await tx.merchant.findMany({
where,
select: merchantSelect,
orderBy: [{ createdAt: 'desc' }, { id: 'asc' }],
take: 8,
skip: (page - 1) * 8,
take: pageSize,
skip: (page - 1) * pageSize,
});
return { items, total, page, pageSize: 8 };
return { items, total, page, pageSize };
},
{ isolationLevel: 'RepeatableRead' },
);
+26 -7
View File
@@ -142,7 +142,7 @@ test('staff creation is atomic, minimally required, role-bound and strict', asyn
test('search, combined filters, pagination and metadata apply ownership scope before counts', async () => {
const prefix = `Directory-${randomUUID()}`;
const ids = [];
for (let index = 0; index < 10; index++)
for (let index = 0; index < 31; index++)
ids.push(
await create('alex', {
name: `${prefix} ${index}`,
@@ -174,11 +174,20 @@ test('search, combined filters, pagination and metadata apply ownership scope be
const query = `search=${prefix}&country=GB&industry=SOFTWARE&status=NEW_LEAD`;
const page1 = (await request(`/merchants?${query}`, 'alex')).body;
const page2 = (await request(`/merchants?${query}&page=2`, 'alex')).body;
assert.equal(page1.total, 10);
assert.equal(page1.items.length, 8);
assert.equal(page2.items.length, 2);
assert.equal(new Set([...page1.items, ...page2.items].map((m) => m.id)).size, 10);
assert.equal((await request(`/merchants?${query}&page=10000`, 'alex')).body.page, 2);
assert.equal(page1.total, 31);
assert.equal(page1.items.length, 10);
assert.equal(page2.items.length, 10);
assert.equal(new Set([...page1.items, ...page2.items].map((m) => m.id)).size, 20);
assert.equal((await request(`/merchants?${query}&page=10000`, 'alex')).body.page, 4);
for (const pageSize of [10, 20, 30]) {
const first = (await request(`/merchants?${query}&pageSize=${pageSize}`, 'alex')).body;
const last = (await request(`/merchants?${query}&pageSize=${pageSize}&page=10000`, 'alex'))
.body;
assert.equal(first.pageSize, pageSize);
assert.equal(first.items.length, pageSize);
assert.equal(last.page, Math.ceil(31 / pageSize));
assert.equal(last.items.length, 31 % pageSize);
}
assert.equal((await request(`/merchants?${query}&ownerId=user-manager2`, 'alex')).status, 403);
assert.equal(
(await request(`/merchants?search=${prefix}&ownerId=user-manager2`, 'head')).body.items[0].id,
@@ -190,7 +199,17 @@ test('search, combined filters, pagination and metadata apply ownership scope be
unassigned,
);
assert.equal((await request(`/merchants?search=${prefix}&country=JP`, 'alex')).body.total, 0);
for (const invalid of ['country=ZZ', 'industry=INVALID', 'status=Live', 'page=1.5', 'page=-1'])
for (const invalid of [
'country=ZZ',
'industry=INVALID',
'status=Live',
'page=1.5',
'page=-1',
'pageSize=8',
'pageSize=0',
'pageSize=15',
'pageSize=1000',
])
assert.equal((await request(`/merchants?${invalid}`)).status, 400);
const ownFilters = merchantFiltersSchema.parse(
(await request('/merchants/filters', 'alex')).body,