2026-04-24 12:45:10 +02:00
|
|
|
import { describe, it, expect } from 'vitest';
|
|
|
|
|
|
feat(platform): residential module + admin UI + reliability fixes
Residential platform
- New schema: residentialClients, residentialInterests (separate from
marina/yacht clients) with migration 0010
- Service layer with CRUD + audit + sockets + per-port portal toggle
- v1 + public API routes (/api/v1/residential/*, /api/public/residential-inquiries)
- List + detail pages with inline editing for clients and interests
- Per-user residentialAccess toggle on userPortRoles (migration 0011)
- Permission keys: residential_clients, residential_interests
- Sidebar nav + role form integration
- Smoke spec covering page loads, UI create flow, public endpoint
Admin & shared UI
- Admin → Forms (form templates CRUD) with validators + service
- Notification preferences page (in-app + email per type)
- Email composition + accounts list + threads view
- Branded auth shell shared across CRM + portal auth surfaces
- Inline editing extended to yacht/company/interest detail pages
- InlineTagEditor + per-entity tags endpoints (yachts, companies)
- Notes service polymorphic across clients/interests/yachts/companies
- Client list columns: yachtCount + companyCount badges
- Reservation file-download via presigned URL (replaces stale <a href>)
Route handler refactor
- Extracted yachts/companies/berths reservation handlers to sibling
handlers.ts files (Next.js 15 route.ts only allows specific exports)
Reliability fixes
- apiFetch double-stringify bug fixed across 13 components
(apiFetch already JSON.stringifies its body; passing a stringified
body produced double-encoded JSON which failed zod validation)
- SocketProvider gated behind useSyncExternalStore-based mount check
to avoid useSession() SSR crashes under React 19 + Next 15
- apiFetch falls back to URL-pathname → port-id resolution when the
Zustand store hasn't hydrated yet (fresh contexts, e2e tests)
- CRM invite flow (schema, service, route, email, dev script)
- Dashboard route → [portSlug]/dashboard/page.tsx + redirect
- Document the dev-server restart-after-migration gotcha in CLAUDE.md
Tests
- 5-case residential smoke spec
- Integration test updates for new service signatures
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 21:54:32 +02:00
|
|
|
import { listHandler, createHandler } from '@/app/api/v1/companies/handlers';
|
|
|
|
|
import { getHandler, patchHandler, deleteHandler } from '@/app/api/v1/companies/[id]/handlers';
|
fix(build): extract route.ts handlers to handlers.ts (CLAUDE.md convention)
8 API route files were exporting handler functions directly from route.ts,
which Next.js 15 rejects with "$NAME is not a valid Route export field".
Per CLAUDE.md convention, service-tested handler functions live in sibling
handlers.ts files and route.ts only re-exports the GET/POST/etc. wrapped
in withAuth(withPermission(...)).
Discovered during the mobile-foundation Task 24 build validation; the route
files predate this branch but the build was never re-run on data-model.
Files:
- berth-reservations/[id], companies/autocomplete, companies/[id]/members
+ nested mid/set-primary, yachts/autocomplete, yachts/[id]/transfer,
yachts/[id]/ownership-history
- Integration tests updated to import from handlers.ts (companies,
memberships, reservations, yachts-detail)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 15:14:40 +02:00
|
|
|
import { autocompleteHandler } from '@/app/api/v1/companies/autocomplete/handlers';
|
2026-04-24 12:45:10 +02:00
|
|
|
import { db } from '@/lib/db';
|
|
|
|
|
import { companies } from '@/lib/db/schema';
|
|
|
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
|
import { makeMockCtx, makeMockRequest } from '../../helpers/route-tester';
|
|
|
|
|
import { makePort, makeCompany, makeFullPermissions } from '../../helpers/factories';
|
|
|
|
|
|
|
|
|
|
describe('POST /api/v1/companies', () => {
|
|
|
|
|
it('creates a company and returns 201', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const name = `Acme Corp ${Math.random().toString(36).slice(2, 8)}`;
|
|
|
|
|
const req = makeMockRequest('POST', 'http://localhost/api/v1/companies', {
|
|
|
|
|
body: { name },
|
|
|
|
|
});
|
|
|
|
|
const res = await createHandler(req, ctx, {});
|
|
|
|
|
expect(res.status).toBe(201);
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(body.data.name).toBe(name);
|
|
|
|
|
expect(body.data.portId).toBe(port.id);
|
|
|
|
|
expect(body.data.status).toBe('active');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns 409 (ConflictError) on duplicate name case-insensitive in same port', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const name = `DupeCo ${Math.random().toString(36).slice(2, 8)}`;
|
|
|
|
|
|
|
|
|
|
const req1 = makeMockRequest('POST', 'http://localhost/api/v1/companies', {
|
|
|
|
|
body: { name },
|
|
|
|
|
});
|
|
|
|
|
const res1 = await createHandler(req1, ctx, {});
|
|
|
|
|
expect(res1.status).toBe(201);
|
|
|
|
|
|
|
|
|
|
// Same name with different case — should still conflict.
|
|
|
|
|
const req2 = makeMockRequest('POST', 'http://localhost/api/v1/companies', {
|
|
|
|
|
body: { name: name.toUpperCase() },
|
|
|
|
|
});
|
|
|
|
|
const res2 = await createHandler(req2, ctx, {});
|
|
|
|
|
expect(res2.status).toBe(409);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns 400 on missing name', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest('POST', 'http://localhost/api/v1/companies', {
|
|
|
|
|
body: {},
|
|
|
|
|
});
|
|
|
|
|
const res = await createHandler(req, ctx, {});
|
|
|
|
|
expect(res.status).toBe(400);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('allows same name in different port', async () => {
|
|
|
|
|
const portA = await makePort();
|
|
|
|
|
const portB = await makePort();
|
|
|
|
|
const name = `SharedName ${Math.random().toString(36).slice(2, 8)}`;
|
|
|
|
|
|
|
|
|
|
const ctxA = makeMockCtx({ portId: portA.id, permissions: makeFullPermissions() });
|
|
|
|
|
const resA = await createHandler(
|
|
|
|
|
makeMockRequest('POST', 'http://localhost/api/v1/companies', { body: { name } }),
|
|
|
|
|
ctxA,
|
|
|
|
|
{},
|
|
|
|
|
);
|
|
|
|
|
expect(resA.status).toBe(201);
|
|
|
|
|
|
|
|
|
|
const ctxB = makeMockCtx({ portId: portB.id, permissions: makeFullPermissions() });
|
|
|
|
|
const resB = await createHandler(
|
|
|
|
|
makeMockRequest('POST', 'http://localhost/api/v1/companies', { body: { name } }),
|
|
|
|
|
ctxB,
|
|
|
|
|
{},
|
|
|
|
|
);
|
|
|
|
|
expect(resB.status).toBe(201);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('GET /api/v1/companies', () => {
|
|
|
|
|
it('returns tenant-scoped companies with pagination shape', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
await makeCompany({ portId: port.id, overrides: { name: 'ListedCo' } });
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest(
|
|
|
|
|
'GET',
|
|
|
|
|
'http://localhost/api/v1/companies?page=1&limit=20&order=desc',
|
|
|
|
|
);
|
|
|
|
|
const res = await listHandler(req, ctx, {});
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(body.data.some((c: { name: string }) => c.name === 'ListedCo')).toBe(true);
|
|
|
|
|
expect(body.pagination.page).toBe(1);
|
|
|
|
|
expect(body.pagination.pageSize).toBe(20);
|
|
|
|
|
expect(typeof body.pagination.total).toBe('number');
|
|
|
|
|
expect(typeof body.pagination.totalPages).toBe('number');
|
|
|
|
|
expect(typeof body.pagination.hasNextPage).toBe('boolean');
|
|
|
|
|
expect(typeof body.pagination.hasPreviousPage).toBe('boolean');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('filters by status', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
await makeCompany({
|
|
|
|
|
portId: port.id,
|
|
|
|
|
overrides: { name: 'ActiveCo', status: 'active' },
|
|
|
|
|
});
|
|
|
|
|
await makeCompany({
|
|
|
|
|
portId: port.id,
|
|
|
|
|
overrides: { name: 'DissolvedCo', status: 'dissolved' },
|
|
|
|
|
});
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest(
|
|
|
|
|
'GET',
|
|
|
|
|
'http://localhost/api/v1/companies?page=1&limit=20&order=desc&status=dissolved',
|
|
|
|
|
);
|
|
|
|
|
const res = await listHandler(req, ctx, {});
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(body.data.every((c: { status: string }) => c.status === 'dissolved')).toBe(true);
|
|
|
|
|
expect(body.data.some((c: { name: string }) => c.name === 'DissolvedCo')).toBe(true);
|
|
|
|
|
expect(body.data.some((c: { name: string }) => c.name === 'ActiveCo')).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('GET /api/v1/companies/[id]', () => {
|
|
|
|
|
it('returns company for valid id + port', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
const company = await makeCompany({
|
|
|
|
|
portId: port.id,
|
|
|
|
|
overrides: { name: 'DetailCo' },
|
|
|
|
|
});
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest('GET', `http://localhost/api/v1/companies/${company.id}`);
|
|
|
|
|
const res = await getHandler(req, ctx, { id: company.id });
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(body.data.id).toBe(company.id);
|
|
|
|
|
expect(body.data.name).toBe('DetailCo');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns 404 for cross-tenant', async () => {
|
|
|
|
|
const portA = await makePort();
|
|
|
|
|
const portB = await makePort();
|
|
|
|
|
const company = await makeCompany({ portId: portA.id });
|
|
|
|
|
const ctx = makeMockCtx({ portId: portB.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest('GET', `http://localhost/api/v1/companies/${company.id}`);
|
|
|
|
|
const res = await getHandler(req, ctx, { id: company.id });
|
|
|
|
|
expect(res.status).toBe(404);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('PATCH /api/v1/companies/[id]', () => {
|
|
|
|
|
it('updates allowed fields', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
const company = await makeCompany({
|
|
|
|
|
portId: port.id,
|
|
|
|
|
overrides: { name: 'BeforeCo' },
|
|
|
|
|
});
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest('PATCH', `http://localhost/api/v1/companies/${company.id}`, {
|
|
|
|
|
body: { name: 'AfterCo', notes: 'updated notes' },
|
|
|
|
|
});
|
|
|
|
|
const res = await patchHandler(req, ctx, { id: company.id });
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(body.data.name).toBe('AfterCo');
|
|
|
|
|
expect(body.data.notes).toBe('updated notes');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns 404 for cross-tenant', async () => {
|
|
|
|
|
const portA = await makePort();
|
|
|
|
|
const portB = await makePort();
|
|
|
|
|
const company = await makeCompany({ portId: portA.id });
|
|
|
|
|
const ctx = makeMockCtx({ portId: portB.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest('PATCH', `http://localhost/api/v1/companies/${company.id}`, {
|
|
|
|
|
body: { name: 'hijack' },
|
|
|
|
|
});
|
|
|
|
|
const res = await patchHandler(req, ctx, { id: company.id });
|
|
|
|
|
expect(res.status).toBe(404);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('DELETE /api/v1/companies/[id]', () => {
|
|
|
|
|
it('archives the company (204)', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
const company = await makeCompany({ portId: port.id });
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest('DELETE', `http://localhost/api/v1/companies/${company.id}`);
|
|
|
|
|
const res = await deleteHandler(req, ctx, { id: company.id });
|
|
|
|
|
expect(res.status).toBe(204);
|
|
|
|
|
const [row] = await db.select().from(companies).where(eq(companies.id, company.id));
|
|
|
|
|
expect(row?.archivedAt).not.toBeNull();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('GET /api/v1/companies/autocomplete', () => {
|
|
|
|
|
it('returns matching companies by name', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
await makeCompany({
|
|
|
|
|
portId: port.id,
|
|
|
|
|
overrides: { name: 'AutoCompleteCoMatch One' },
|
|
|
|
|
});
|
|
|
|
|
await makeCompany({
|
|
|
|
|
portId: port.id,
|
|
|
|
|
overrides: { name: 'OtherCompanyName' },
|
|
|
|
|
});
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest(
|
|
|
|
|
'GET',
|
|
|
|
|
'http://localhost/api/v1/companies/autocomplete?q=AutoCompleteCoMatch',
|
|
|
|
|
);
|
|
|
|
|
const res = await autocompleteHandler(req, ctx, {});
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(body.data.length).toBeGreaterThanOrEqual(1);
|
|
|
|
|
expect(body.data.every((c: { name: string }) => c.name.includes('AutoCompleteCoMatch'))).toBe(
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns empty array when q missing', async () => {
|
|
|
|
|
const port = await makePort();
|
|
|
|
|
const ctx = makeMockCtx({ portId: port.id, permissions: makeFullPermissions() });
|
|
|
|
|
const req = makeMockRequest('GET', 'http://localhost/api/v1/companies/autocomplete');
|
|
|
|
|
const res = await autocompleteHandler(req, ctx, {});
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(body.data).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
});
|