diff --git a/README.md b/README.md index 2fc57be..1ca925f 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ The old manager account IDs are retained while the original addresses become `al Head can add merchants from the directory and optionally assign a manager. Managers create merchants assigned to themselves. Company name, contact name and email are required; website, country, industry and volume can be supplied later. Compliance has no creation access. New leads begin at `NEW_LEAD` with three missing KYB checklist records and an attributed creation history. The API rejects caller-supplied stage, audit identity and public-submission fields. -Directory search matches company/legal name, contact name, email and website. `status`, ISO `country`, canonical `industry`, and `ownerId` combine with search. Head and Compliance may use `ownerId=unassigned` or a manager ID. Manager owner filters return 403. All records, totals and filter options use the same authorization scope. Results retain eight-record pages and clamp excessive page numbers to the last available page. Search, filters and pagination survive URL sharing, reload and browser navigation. +Directory search matches company/legal name, contact name, email and website. `status`, ISO `country`, canonical `industry`, and `ownerId` combine with search. Head and Compliance may use `ownerId=unassigned` or a manager ID. Manager owner filters return 403. All records, totals and filter options use the same authorization scope. Results support 10, 20 or 30 records per page (`pageSize`, default 10) and clamp excessive page numbers to the last available page. Changing the page size returns to page one. Search, filters and pagination survive URL sharing, reload and browser navigation. | Method | Endpoint | Purpose | | ------ | -------------------------------- | ---------------------------------------------------------------------- | diff --git a/apps/api/src/crm.ts b/apps/api/src/crm.ts index a96572d..150841c 100644 --- a/apps/api/src/crm.ts +++ b/apps/api/src/crm.ts @@ -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' }, ); diff --git a/apps/api/test/sales-workspace.test.mjs b/apps/api/test/sales-workspace.test.mjs index 69c7156..f71c869 100644 --- a/apps/api/test/sales-workspace.test.mjs +++ b/apps/api/test/sales-workspace.test.mjs @@ -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, diff --git a/apps/web/src/components/dashboard.tsx b/apps/web/src/components/dashboard.tsx index 368a03b..6566699 100644 --- a/apps/web/src/components/dashboard.tsx +++ b/apps/web/src/components/dashboard.tsx @@ -172,12 +172,19 @@ function SalesDashboard({ data, user }: { data: Data; user: User }) {
{data.distribution.map((d) => ( - {t(`statuses.${d.status}`)} - + + + {f.number(d.count)} diff --git a/apps/web/src/components/lead-form.tsx b/apps/web/src/components/lead-form.tsx index 37f2de3..798d333 100644 --- a/apps/web/src/components/lead-form.tsx +++ b/apps/web/src/components/lead-form.tsx @@ -1,7 +1,7 @@ import { useEffect, useMemo, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useMutation } from '@tanstack/react-query'; -import { useForm } from 'react-hook-form'; +import { Controller, useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { ArrowUpRight, Check, LoaderCircle } from 'lucide-react'; import { @@ -13,7 +13,7 @@ import { type PublicLeadInput, } from '@merchantflow/contracts'; import { Input } from '@merchantflow/ui/components/input'; -import { NativeSelect } from '@merchantflow/ui/components/native-select'; +import { Select, SelectOption } from '@merchantflow/ui/components/select'; import { FormField } from '@merchantflow/ui/components/form-field'; import { Button } from '@merchantflow/ui/components/button'; import { api, ApiError } from '../lib/api'; @@ -202,37 +202,59 @@ export function LeadForm() { ))} - - - {countries.map(({ code, name }) => ( - - ))} - + ( + + )} + /> - - - {industries.map((industry) => ( - - ))} - + ( + + )} + />
setOpen(false)} />