feat(yachts): list + owner-scoped list + autocomplete

Adds `listYachts`, `listYachtsForOwner`, and `autocomplete` to the
yacht service so UIs can page/filter yachts per port, look up all
yachts tied to a given client/company, and power search-as-you-type.

`listYachts` delegates to the shared port-scoped `buildListQuery`,
supporting search over name/hullNumber/registration plus ownerType,
ownerId and status filters; `autocomplete` caps at 10 results and is
tenant-scoped; `listYachtsForOwner` returns all yachts whose current
owner matches, newest first. Extends `makeYacht` factory to accept
flat `name`, `status`, `hullNumber`, `registration` overrides.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt Ciaccio
2026-04-24 00:03:36 +02:00
parent 8a5cd1ef0e
commit 7c408cf975
3 changed files with 283 additions and 4 deletions

View File

@@ -72,15 +72,22 @@ export async function makeYacht(args: {
portId: string;
ownerType: 'client' | 'company';
ownerId: string;
name?: string;
status?: 'active' | 'retired' | 'sold_away';
hullNumber?: string;
registration?: string;
overrides?: Partial<NewYacht>;
}): Promise<Yacht> {
const [yacht] = await db
.insert(yachts)
.values({
portId: args.portId,
name: args.overrides?.name ?? `Yacht ${Math.random().toString(36).slice(2, 8)}`,
name: args.name ?? args.overrides?.name ?? `Yacht ${Math.random().toString(36).slice(2, 8)}`,
currentOwnerType: args.ownerType,
currentOwnerId: args.ownerId,
...(args.status !== undefined ? { status: args.status } : {}),
...(args.hullNumber !== undefined ? { hullNumber: args.hullNumber } : {}),
...(args.registration !== undefined ? { registration: args.registration } : {}),
...args.overrides,
})
.returning();

View File

@@ -1,6 +1,19 @@
import { describe, it, expect } from 'vitest';
import { createYacht, updateYacht, archiveYacht } from '@/lib/services/yachts.service';
import { makeClient, makePort, makeYacht, makeAuditMeta } from '../../helpers/factories';
import {
createYacht,
updateYacht,
archiveYacht,
listYachts,
listYachtsForOwner,
autocomplete,
} from '@/lib/services/yachts.service';
import {
makeClient,
makeCompany,
makePort,
makeYacht,
makeAuditMeta,
} from '../../helpers/factories';
import { db } from '@/lib/db';
import { yachts, yachtOwnershipHistory } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';
@@ -160,3 +173,188 @@ describe('yachts.service — archiveYacht', () => {
await expect(archiveYacht(yachtInB.id, portA.id, makeAuditMeta())).rejects.toThrow(/yacht/i);
});
});
describe('yachts.service — listYachts', () => {
it('is scoped to port (tenant isolation)', async () => {
const portA = await makePort();
const portB = await makePort();
const clientA = await makeClient({ portId: portA.id });
const clientB = await makeClient({ portId: portB.id });
await makeYacht({ portId: portA.id, ownerType: 'client', ownerId: clientA.id, name: 'In A' });
await makeYacht({ portId: portB.id, ownerType: 'client', ownerId: clientB.id, name: 'In B' });
const result = await listYachts(portA.id, {
page: 1,
limit: 20,
order: 'desc',
includeArchived: false,
});
expect(result.data.some((y) => y.name === 'In A')).toBe(true);
expect(result.data.some((y) => y.name === 'In B')).toBe(false);
});
it('filters by ownerType + ownerId', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
const other = await makeClient({ portId: port.id });
await makeYacht({ portId: port.id, ownerType: 'client', ownerId: client.id, name: 'Mine' });
await makeYacht({ portId: port.id, ownerType: 'client', ownerId: other.id, name: 'Theirs' });
const result = await listYachts(port.id, {
page: 1,
limit: 20,
order: 'desc',
includeArchived: false,
ownerType: 'client',
ownerId: client.id,
});
expect(result.data.map((y) => y.name)).toContain('Mine');
expect(result.data.map((y) => y.name)).not.toContain('Theirs');
});
it('filters by status', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
await makeYacht({
portId: port.id,
ownerType: 'client',
ownerId: client.id,
name: 'Active One',
status: 'active',
});
await makeYacht({
portId: port.id,
ownerType: 'client',
ownerId: client.id,
name: 'Retired One',
status: 'retired',
});
const result = await listYachts(port.id, {
page: 1,
limit: 20,
order: 'desc',
includeArchived: false,
status: 'retired',
});
expect(result.data.map((y) => y.name)).toContain('Retired One');
expect(result.data.map((y) => y.name)).not.toContain('Active One');
});
it('searches by name (ILIKE)', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
await makeYacht({
portId: port.id,
ownerType: 'client',
ownerId: client.id,
name: 'Sea Breeze',
});
await makeYacht({
portId: port.id,
ownerType: 'client',
ownerId: client.id,
name: 'Wind Dancer',
});
const result = await listYachts(port.id, {
page: 1,
limit: 20,
order: 'desc',
includeArchived: false,
search: 'breeze',
});
expect(result.data.map((y) => y.name)).toContain('Sea Breeze');
expect(result.data.map((y) => y.name)).not.toContain('Wind Dancer');
});
});
describe('yachts.service — listYachtsForOwner', () => {
it('returns all yachts owned by a given client', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
await makeYacht({ portId: port.id, ownerType: 'client', ownerId: client.id, name: 'Y1' });
await makeYacht({ portId: port.id, ownerType: 'client', ownerId: client.id, name: 'Y2' });
const result = await listYachtsForOwner(port.id, 'client', client.id);
expect(result.map((y) => y.name).sort()).toEqual(['Y1', 'Y2']);
});
it('returns all yachts owned by a given company', async () => {
const port = await makePort();
const company = await makeCompany({ portId: port.id });
await makeYacht({ portId: port.id, ownerType: 'company', ownerId: company.id, name: 'CY1' });
const result = await listYachtsForOwner(port.id, 'company', company.id);
expect(result.map((y) => y.name)).toEqual(['CY1']);
});
it('is tenant-scoped', async () => {
const portA = await makePort();
const portB = await makePort();
const clientA = await makeClient({ portId: portA.id });
await makeYacht({
portId: portA.id,
ownerType: 'client',
ownerId: clientA.id,
name: 'Only-A',
});
const result = await listYachtsForOwner(portB.id, 'client', clientA.id);
expect(result).toHaveLength(0);
});
});
describe('yachts.service — autocomplete', () => {
it('matches by name (ILIKE)', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
await makeYacht({ portId: port.id, ownerType: 'client', ownerId: client.id, name: 'Phoenix' });
const result = await autocomplete(port.id, 'phoe');
expect(result.some((y) => y.name === 'Phoenix')).toBe(true);
});
it('matches by hullNumber or registration', async () => {
const port = await makePort();
const client = await makeClient({ portId: port.id });
await makeYacht({
portId: port.id,
ownerType: 'client',
ownerId: client.id,
name: 'Something',
hullNumber: 'HULL-ABC-123',
});
const result = await autocomplete(port.id, 'HULL-ABC');
expect(result.some((y) => y.hullNumber === 'HULL-ABC-123')).toBe(true);
});
it('is tenant-scoped and caps at 10 results', async () => {
const port = await makePort();
const other = await makePort();
const client = await makeClient({ portId: port.id });
const otherClient = await makeClient({ portId: other.id });
for (let i = 0; i < 12; i++) {
await makeYacht({
portId: port.id,
ownerType: 'client',
ownerId: client.id,
name: `MatchMe-${i}`,
});
}
await makeYacht({
portId: other.id,
ownerType: 'client',
ownerId: otherClient.id,
name: 'MatchMe-other',
});
const result = await autocomplete(port.id, 'matchme');
expect(result.length).toBe(10);
expect(result.every((y) => y.name.startsWith('MatchMe-') && !y.name.endsWith('-other'))).toBe(
true,
);
});
});