73-file atomic rename per docs/tenancies-design.md:
- Migration 0085: rename table + indexes + FK constraints; rename
documents.reservation_id → tenancy_id; migrate jsonb permission maps
(reservations resource → tenancies; collapse create+activate → manage);
rewrite historical audit_logs.entity_type='berth_reservation' →
'berth_tenancy'. FK renames wrapped in DO blocks so dev DBs that pre-date
the FK additions don't abort.
- Schema: berthReservations → berthTenancies; BerthReservation type →
BerthTenancy; indexes idx_br_* / idx_brr_* → idx_bt_*.
- RolePermissions: resource { view, create, activate, cancel } collapses to
{ view, manage, cancel }; all 8 default seed bundles + role-form + matrix
updated.
- Service: berth-reservations.service.ts → berth-tenancies.service.ts;
endReservation → endTenancy; listReservations → listTenancies.
- API: /api/v1/berth-reservations → /api/v1/tenancies (+ nested [id]);
/api/v1/berths/[id]/reservations → /api/v1/berths/[id]/tenancies.
- Validators: reservations.ts → tenancies.ts; RESERVATION_STATUSES →
TENANCY_STATUSES; endReservationSchema → endTenancySchema.
- Routes: /{portSlug}/berth-reservations → /{portSlug}/tenancies;
/portal/my-reservations → /portal/my-tenancies.
- Components: src/components/reservations/* → src/components/tenancies/*;
BerthReservationsTab → BerthTenanciesTab; ClientReservationsTab →
ClientTenanciesTab; ReservationList → TenancyList.
- Socket events: berth_reservation:* → berth_tenancy:*; payload
reservationId → tenancyId.
- Webhook events: berth_reservation.* → berth_tenancy.*.
- Portal: getPortalUserReservations → getPortalUserTenancies;
PortalReservation → PortalTenancy; PortalDashboard.counts.activeReservations
→ activeTenancies; PortalNav label "Reservations" → "Tenancies".
- Dossier: DossierReservation → DossierTenancy; reservationDecisions →
tenancyDecisions across smart-archive-dialog + bulk-archive routes.
- Documents schema: documents.reservationId → documents.tenancyId
(TS + DB column + index + FK constraint).
- Activity feed label berth_reservation → berth_tenancy (matched against
migrated historical audit rows).
KEPT (separate concepts):
- Reservation Agreement document type (the contract sent to clients).
- "Reservation" pipeline stage name.
- {{reservation.*}} merge tokens in template authoring.
- interest.reservationStatus / reservationDocStatus / dateReservationSent
fields (track agreement signing on the deal).
- reservation-agreement-context.ts service (builds merge context for the
Reservation Agreement doc; only its DB imports were renamed).
Verified: tsc clean, 1480/1480 vitest passing, migration applied.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
375 lines
12 KiB
TypeScript
375 lines
12 KiB
TypeScript
import { describe, it, expect, beforeAll } from 'vitest';
|
|
|
|
describe('portal.service - getPortalUserYachts', () => {
|
|
let getPortalUserYachts: (clientId: string, portId: string) => Promise<Array<any>>;
|
|
|
|
let makeClient: typeof import('../../helpers/factories').makeClient;
|
|
|
|
let makePort: typeof import('../../helpers/factories').makePort;
|
|
|
|
let makeYacht: typeof import('../../helpers/factories').makeYacht;
|
|
|
|
let makeCompany: typeof import('../../helpers/factories').makeCompany;
|
|
|
|
let makeMembership: typeof import('../../helpers/factories').makeMembership;
|
|
|
|
let db: typeof import('@/lib/db').db;
|
|
|
|
let yachts: typeof import('@/lib/db/schema').yachts;
|
|
|
|
let eq: typeof import('drizzle-orm').eq;
|
|
|
|
beforeAll(async () => {
|
|
const portalMod = await import('@/lib/services/portal.service');
|
|
getPortalUserYachts = portalMod.getPortalUserYachts;
|
|
const factoriesMod = await import('../../helpers/factories');
|
|
makeClient = factoriesMod.makeClient;
|
|
makePort = factoriesMod.makePort;
|
|
makeYacht = factoriesMod.makeYacht;
|
|
makeCompany = factoriesMod.makeCompany;
|
|
makeMembership = factoriesMod.makeMembership;
|
|
const dbMod = await import('@/lib/db');
|
|
db = dbMod.db;
|
|
const schemaMod = await import('@/lib/db/schema');
|
|
yachts = schemaMod.yachts;
|
|
const ormMod = await import('drizzle-orm');
|
|
eq = ormMod.eq;
|
|
});
|
|
it('returns client-owned yachts only when client has no memberships', async () => {
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
const yacht = await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'client',
|
|
ownerId: client.id,
|
|
name: 'Solo Sail',
|
|
});
|
|
|
|
const result = await getPortalUserYachts(client.id, port.id);
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0]!.id).toBe(yacht.id);
|
|
expect(result[0]!.ownerContext).toBe('direct');
|
|
expect(result[0]!.ownerCompanyId).toBeNull();
|
|
expect(result[0]!.ownerCompanyName).toBeNull();
|
|
});
|
|
|
|
it('returns company-owned yachts when client is an active member', async () => {
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
const company = await makeCompany({
|
|
portId: port.id,
|
|
overrides: { name: `Acme Holdings ${Math.random().toString(36).slice(2, 6)}` },
|
|
});
|
|
await makeMembership({
|
|
companyId: company.id,
|
|
clientId: client.id,
|
|
role: 'director',
|
|
endDate: null,
|
|
});
|
|
const yacht = await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'company',
|
|
ownerId: company.id,
|
|
name: 'Corporate Cruise',
|
|
});
|
|
|
|
const result = await getPortalUserYachts(client.id, port.id);
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0]!.id).toBe(yacht.id);
|
|
expect(result[0]!.ownerContext).toBe('company');
|
|
expect(result[0]!.ownerCompanyId).toBe(company.id);
|
|
expect(result[0]!.ownerCompanyName).toBe(company.name);
|
|
});
|
|
|
|
it('de-dupes if same yacht id appears in both paths (defensive)', async () => {
|
|
// A yacht cannot legitimately be owned by both, but we verify dedup is
|
|
// defensive by forcing the yacht's current owner to company after the
|
|
// direct query path has already cached it. We simulate the case by
|
|
// creating a client-owned yacht, then manually flipping owner to a
|
|
// company the client is a member of - if both queries ran they'd both
|
|
// match, but dedup by id ensures only one entry.
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
const company = await makeCompany({ portId: port.id });
|
|
await makeMembership({
|
|
companyId: company.id,
|
|
clientId: client.id,
|
|
role: 'director',
|
|
endDate: null,
|
|
});
|
|
const yacht = await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'client',
|
|
ownerId: client.id,
|
|
name: 'Ambiguous',
|
|
});
|
|
// Flip the denormalized owner to the company (without updating history) -
|
|
// this is artificial but exercises the dedup branch.
|
|
await db
|
|
.update(yachts)
|
|
.set({ currentOwnerType: 'company', currentOwnerId: company.id })
|
|
.where(eq(yachts.id, yacht.id));
|
|
|
|
const result = await getPortalUserYachts(client.id, port.id);
|
|
const matches = result.filter((y) => y.id === yacht.id);
|
|
expect(matches).toHaveLength(1);
|
|
});
|
|
|
|
it('excludes yachts from companies where membership ended', async () => {
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
const company = await makeCompany({ portId: port.id });
|
|
await makeMembership({
|
|
companyId: company.id,
|
|
clientId: client.id,
|
|
role: 'director',
|
|
startDate: new Date('2024-01-01'),
|
|
endDate: new Date('2025-06-01'),
|
|
});
|
|
await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'company',
|
|
ownerId: company.id,
|
|
name: 'Past Company Yacht',
|
|
});
|
|
|
|
const result = await getPortalUserYachts(client.id, port.id);
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
it('is tenant-scoped', async () => {
|
|
const portA = await makePort();
|
|
const portB = await makePort();
|
|
const clientInA = await makeClient({ portId: portA.id });
|
|
// Directly-owned yacht in portB with the SAME client id - must not leak
|
|
// because getPortalUserYachts filters on portId.
|
|
// We insert a yacht row in portB with ownerId=clientInA.id. The FK on
|
|
// yachts.currentOwnerId isn't to clients, so this is valid.
|
|
await makeYacht({
|
|
portId: portB.id,
|
|
ownerType: 'client',
|
|
ownerId: clientInA.id,
|
|
name: 'Other Port Yacht',
|
|
});
|
|
|
|
const result = await getPortalUserYachts(clientInA.id, portA.id);
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
it('excludes archived yachts', async () => {
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'client',
|
|
ownerId: client.id,
|
|
overrides: { archivedAt: new Date() },
|
|
});
|
|
|
|
const result = await getPortalUserYachts(client.id, port.id);
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('portal.service - getPortalUserMemberships', () => {
|
|
let getPortalUserMemberships: (clientId: string, portId: string) => Promise<Array<any>>;
|
|
|
|
let makeClient: typeof import('../../helpers/factories').makeClient;
|
|
|
|
let makePort: typeof import('../../helpers/factories').makePort;
|
|
|
|
let makeCompany: typeof import('../../helpers/factories').makeCompany;
|
|
|
|
let makeMembership: typeof import('../../helpers/factories').makeMembership;
|
|
|
|
beforeAll(async () => {
|
|
const portalMod = await import('@/lib/services/portal.service');
|
|
getPortalUserMemberships = portalMod.getPortalUserMemberships;
|
|
const factoriesMod = await import('../../helpers/factories');
|
|
makeClient = factoriesMod.makeClient;
|
|
makePort = factoriesMod.makePort;
|
|
makeCompany = factoriesMod.makeCompany;
|
|
makeMembership = factoriesMod.makeMembership;
|
|
});
|
|
|
|
it('returns only active memberships', async () => {
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
const activeCompany = await makeCompany({ portId: port.id });
|
|
const endedCompany = await makeCompany({ portId: port.id });
|
|
await makeMembership({
|
|
companyId: activeCompany.id,
|
|
clientId: client.id,
|
|
role: 'director',
|
|
endDate: null,
|
|
});
|
|
await makeMembership({
|
|
companyId: endedCompany.id,
|
|
clientId: client.id,
|
|
role: 'officer',
|
|
startDate: new Date('2024-01-01'),
|
|
endDate: new Date('2025-01-01'),
|
|
});
|
|
|
|
const result = await getPortalUserMemberships(client.id, port.id);
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0]!.company.id).toBe(activeCompany.id);
|
|
expect(result[0]!.role).toBe('director');
|
|
});
|
|
|
|
it('is tenant-scoped (memberships on companies in different port are excluded)', async () => {
|
|
const portA = await makePort();
|
|
const portB = await makePort();
|
|
const client = await makeClient({ portId: portA.id });
|
|
// Company in portB - but membership references clientId on portA.
|
|
const companyInB = await makeCompany({ portId: portB.id });
|
|
await makeMembership({
|
|
companyId: companyInB.id,
|
|
clientId: client.id,
|
|
role: 'director',
|
|
endDate: null,
|
|
});
|
|
|
|
const resultA = await getPortalUserMemberships(client.id, portA.id);
|
|
expect(resultA).toHaveLength(0);
|
|
|
|
const resultB = await getPortalUserMemberships(client.id, portB.id);
|
|
expect(resultB).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe('portal.service - getPortalUserTenancies', () => {
|
|
let getPortalUserTenancies: (clientId: string, portId: string) => Promise<Array<any>>;
|
|
|
|
let makeClient: typeof import('../../helpers/factories').makeClient;
|
|
|
|
let makePort: typeof import('../../helpers/factories').makePort;
|
|
|
|
let makeYacht: typeof import('../../helpers/factories').makeYacht;
|
|
|
|
let makeBerth: typeof import('../../helpers/factories').makeBerth;
|
|
|
|
let makeTenancy: typeof import('../../helpers/factories').makeTenancy;
|
|
|
|
beforeAll(async () => {
|
|
const portalMod = await import('@/lib/services/portal.service');
|
|
getPortalUserTenancies = portalMod.getPortalUserTenancies;
|
|
const factoriesMod = await import('../../helpers/factories');
|
|
makeClient = factoriesMod.makeClient;
|
|
makePort = factoriesMod.makePort;
|
|
makeYacht = factoriesMod.makeYacht;
|
|
makeBerth = factoriesMod.makeBerth;
|
|
makeTenancy = factoriesMod.makeTenancy;
|
|
});
|
|
|
|
it('returns active + pending tenancies', async () => {
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
const yacht = await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'client',
|
|
ownerId: client.id,
|
|
});
|
|
const berth1 = await makeBerth({ portId: port.id });
|
|
const berth2 = await makeBerth({ portId: port.id });
|
|
await makeTenancy({
|
|
berthId: berth1.id,
|
|
portId: port.id,
|
|
clientId: client.id,
|
|
yachtId: yacht.id,
|
|
status: 'active',
|
|
});
|
|
await makeTenancy({
|
|
berthId: berth2.id,
|
|
portId: port.id,
|
|
clientId: client.id,
|
|
yachtId: yacht.id,
|
|
status: 'pending',
|
|
});
|
|
|
|
const result = await getPortalUserTenancies(client.id, port.id);
|
|
expect(result).toHaveLength(2);
|
|
const statuses = result.map((r) => r.status).sort();
|
|
expect(statuses).toEqual(['active', 'pending']);
|
|
});
|
|
|
|
it('excludes ended and cancelled', async () => {
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
const yacht = await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'client',
|
|
ownerId: client.id,
|
|
});
|
|
const berth1 = await makeBerth({ portId: port.id });
|
|
const berth2 = await makeBerth({ portId: port.id });
|
|
await makeTenancy({
|
|
berthId: berth1.id,
|
|
portId: port.id,
|
|
clientId: client.id,
|
|
yachtId: yacht.id,
|
|
status: 'ended',
|
|
});
|
|
await makeTenancy({
|
|
berthId: berth2.id,
|
|
portId: port.id,
|
|
clientId: client.id,
|
|
yachtId: yacht.id,
|
|
status: 'cancelled',
|
|
});
|
|
|
|
const result = await getPortalUserTenancies(client.id, port.id);
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
it('is tenant-scoped', async () => {
|
|
const portA = await makePort();
|
|
const portB = await makePort();
|
|
const client = await makeClient({ portId: portA.id });
|
|
const yachtB = await makeYacht({
|
|
portId: portB.id,
|
|
ownerType: 'client',
|
|
ownerId: client.id,
|
|
});
|
|
const berthB = await makeBerth({ portId: portB.id });
|
|
await makeTenancy({
|
|
berthId: berthB.id,
|
|
portId: portB.id,
|
|
clientId: client.id,
|
|
yachtId: yachtB.id,
|
|
status: 'active',
|
|
});
|
|
|
|
const resultA = await getPortalUserTenancies(client.id, portA.id);
|
|
expect(resultA).toHaveLength(0);
|
|
});
|
|
|
|
it('includes joined yacht name + berth mooring number', async () => {
|
|
const port = await makePort();
|
|
const client = await makeClient({ portId: port.id });
|
|
const yacht = await makeYacht({
|
|
portId: port.id,
|
|
ownerType: 'client',
|
|
ownerId: client.id,
|
|
name: 'Test Vessel',
|
|
});
|
|
const berth = await makeBerth({
|
|
portId: port.id,
|
|
overrides: { mooringNumber: 'M-42' },
|
|
});
|
|
await makeTenancy({
|
|
berthId: berth.id,
|
|
portId: port.id,
|
|
clientId: client.id,
|
|
yachtId: yacht.id,
|
|
status: 'active',
|
|
});
|
|
|
|
const result = await getPortalUserTenancies(client.id, port.id);
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0]!.yachtName).toBe('Test Vessel');
|
|
expect(result[0]!.berthMooringNumber).toBe('M-42');
|
|
expect(result[0]!.tenureType).toBe('permanent');
|
|
});
|
|
});
|