2026-04-27 21:57:13 +02:00
|
|
|
import { and, count, eq, ilike, inArray, isNull, or, sql } from 'drizzle-orm';
|
2026-04-24 12:02:08 +02:00
|
|
|
import { db } from '@/lib/db';
|
2026-04-27 21:57:13 +02:00
|
|
|
import { companies, companyMemberships, companyTags } from '@/lib/db/schema/companies';
|
2026-04-24 12:02:08 +02:00
|
|
|
import type { Company } from '@/lib/db/schema/companies';
|
2026-04-27 21:57:13 +02:00
|
|
|
import { yachts } from '@/lib/db/schema/yachts';
|
2026-04-24 12:02:08 +02:00
|
|
|
import { withTransaction } from '@/lib/db/utils';
|
|
|
|
|
import { buildListQuery } from '@/lib/db/query-builder';
|
|
|
|
|
import { createAuditLog } from '@/lib/audit';
|
|
|
|
|
import { NotFoundError, ConflictError } from '@/lib/errors';
|
|
|
|
|
import { emitToRoom } from '@/lib/socket/server';
|
|
|
|
|
import { diffEntity } from '@/lib/entity-diff';
|
|
|
|
|
import type { z } from 'zod';
|
|
|
|
|
import type {
|
|
|
|
|
createCompanySchema,
|
|
|
|
|
UpdateCompanyInput,
|
|
|
|
|
ListCompaniesInput,
|
|
|
|
|
} from '@/lib/validators/companies';
|
|
|
|
|
|
|
|
|
|
type CreateCompanyInput = z.input<typeof createCompanySchema>;
|
|
|
|
|
|
|
|
|
|
interface AuditMeta {
|
|
|
|
|
userId: string;
|
|
|
|
|
portId: string;
|
|
|
|
|
ipAddress: string;
|
|
|
|
|
userAgent: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type { Company };
|
|
|
|
|
|
|
|
|
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if the error is a Postgres unique-violation (SQLSTATE 23505).
|
|
|
|
|
* We check a few shapes because the exact object depends on the driver.
|
|
|
|
|
*/
|
|
|
|
|
function isUniqueViolation(err: unknown): boolean {
|
|
|
|
|
if (!err || typeof err !== 'object') return false;
|
|
|
|
|
const e = err as { code?: unknown; cause?: { code?: unknown } };
|
|
|
|
|
if (e.code === '23505') return true;
|
|
|
|
|
if (e.cause && typeof e.cause === 'object' && e.cause.code === '23505') return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Create ──────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function createCompany(portId: string, data: CreateCompanyInput, meta: AuditMeta) {
|
|
|
|
|
// Pre-check (case-insensitive) for friendlier ConflictError; the partial unique
|
|
|
|
|
// index `idx_companies_name_unique ON companies(portId, lower(name))` is the
|
|
|
|
|
// authoritative guard and caught below as defense-in-depth.
|
|
|
|
|
const existing = await db.query.companies.findFirst({
|
|
|
|
|
where: and(eq(companies.portId, portId), sql`lower(${companies.name}) = lower(${data.name})`),
|
|
|
|
|
});
|
|
|
|
|
if (existing) {
|
|
|
|
|
throw new ConflictError('company name already exists');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return await withTransaction(async (tx) => {
|
|
|
|
|
const [company] = await tx
|
|
|
|
|
.insert(companies)
|
|
|
|
|
.values({
|
|
|
|
|
portId,
|
|
|
|
|
name: data.name,
|
|
|
|
|
legalName: data.legalName ?? null,
|
|
|
|
|
taxId: data.taxId ?? null,
|
|
|
|
|
registrationNumber: data.registrationNumber ?? null,
|
chore(i18n): drop legacy free-text country/nationality columns
Test-data only — no production migration needed (per earlier decision).
Schema is now ISO-only; readers convert ISO codes to localized names where
human-readable output is required (EOI documents, invoices, portal).
Migration 0016 drops:
- clients.nationality
- companies.incorporation_country
- client_addresses.{state_province, country}
- company_addresses.{state_province, country}
Code paths that previously read free-text values now read the ISO column
and pass through `getCountryName()` / `getSubdivisionName()` for rendering.
Document templates ({{client.nationality}}), portal client view, EOI/
reservation-agreement contexts, and invoice billing addresses all updated.
Public yacht-interest endpoint (/api/public/interests) drops the legacy
fields from its insert path and writes ISO codes only. The Zod validators
no longer accept the legacy fields — older website builds posting raw
'incorporationCountry' / 'country' / 'stateProvince' will get 400s.
Server-side phone normalization is unchanged.
Seed data updated to use ISO codes (GB/FR/ES/GR/SE/IT/GH/MC/PA), spread
across continents to keep test fixtures realistic.
Test assertions updated to match the new render shape (e.g.
'United States' not 'US', 'California' not 'CA').
Vitest: 741 -> 741 (unchanged count; assertions updated, no new tests).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 19:00:57 +02:00
|
|
|
incorporationCountryIso: data.incorporationCountryIso ?? null,
|
|
|
|
|
incorporationSubdivisionIso: data.incorporationSubdivisionIso ?? null,
|
2026-04-24 12:02:08 +02:00
|
|
|
incorporationDate: data.incorporationDate ?? null,
|
|
|
|
|
status: data.status ?? 'active',
|
|
|
|
|
billingEmail: data.billingEmail ?? null,
|
|
|
|
|
notes: data.notes ?? null,
|
|
|
|
|
})
|
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
|
|
const tagIds = data.tagIds ?? [];
|
|
|
|
|
if (tagIds.length > 0) {
|
|
|
|
|
await tx
|
|
|
|
|
.insert(companyTags)
|
|
|
|
|
.values(tagIds.map((tagId) => ({ companyId: company!.id, tagId })));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void createAuditLog({
|
|
|
|
|
userId: meta.userId,
|
|
|
|
|
portId,
|
|
|
|
|
action: 'create',
|
|
|
|
|
entityType: 'company',
|
|
|
|
|
entityId: company!.id,
|
|
|
|
|
newValue: { name: company!.name, status: company!.status },
|
|
|
|
|
ipAddress: meta.ipAddress,
|
|
|
|
|
userAgent: meta.userAgent,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
emitToRoom(`port:${portId}`, 'company:created', { companyId: company!.id });
|
|
|
|
|
|
|
|
|
|
return company!;
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (isUniqueViolation(err)) {
|
|
|
|
|
throw new ConflictError('company name already exists');
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Get ─────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function getCompanyById(id: string, portId: string) {
|
|
|
|
|
const company = await db.query.companies.findFirst({
|
|
|
|
|
where: and(eq(companies.id, id), eq(companies.portId, portId)),
|
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
|
|
|
with: {
|
|
|
|
|
tags: { with: { tag: true } },
|
|
|
|
|
},
|
2026-04-24 12:02:08 +02:00
|
|
|
});
|
|
|
|
|
if (!company) throw new NotFoundError('Company');
|
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
|
|
|
const { tags: tagJoins, ...rest } = company as typeof company & {
|
|
|
|
|
tags: Array<{ tag: { id: string; name: string; color: string } }>;
|
|
|
|
|
};
|
|
|
|
|
return {
|
|
|
|
|
...rest,
|
|
|
|
|
tags: tagJoins.map((t) => t.tag),
|
|
|
|
|
};
|
2026-04-24 12:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Update ──────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function updateCompany(
|
|
|
|
|
id: string,
|
|
|
|
|
portId: string,
|
|
|
|
|
data: UpdateCompanyInput,
|
|
|
|
|
meta: AuditMeta,
|
|
|
|
|
) {
|
|
|
|
|
const existing = await db.query.companies.findFirst({
|
|
|
|
|
where: eq(companies.id, id),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existing || existing.portId !== portId) {
|
|
|
|
|
throw new NotFoundError('Company');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { diff } = diffEntity(
|
|
|
|
|
existing as unknown as Record<string, unknown>,
|
|
|
|
|
data as Record<string, unknown>,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let updated: Company | undefined;
|
|
|
|
|
try {
|
|
|
|
|
const rows = await db
|
|
|
|
|
.update(companies)
|
|
|
|
|
.set({ ...data, updatedAt: new Date() })
|
|
|
|
|
.where(and(eq(companies.id, id), eq(companies.portId, portId)))
|
|
|
|
|
.returning();
|
|
|
|
|
updated = rows[0];
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (isUniqueViolation(err)) {
|
|
|
|
|
throw new ConflictError('company name already exists');
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void createAuditLog({
|
|
|
|
|
userId: meta.userId,
|
|
|
|
|
portId,
|
|
|
|
|
action: 'update',
|
|
|
|
|
entityType: 'company',
|
|
|
|
|
entityId: id,
|
|
|
|
|
oldValue: diff as Record<string, unknown>,
|
|
|
|
|
newValue: data as Record<string, unknown>,
|
|
|
|
|
ipAddress: meta.ipAddress,
|
|
|
|
|
userAgent: meta.userAgent,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
emitToRoom(`port:${portId}`, 'company:updated', {
|
|
|
|
|
companyId: id,
|
|
|
|
|
changedFields: Object.keys(diff),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return updated!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Archive ─────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function archiveCompany(id: string, portId: string, meta: AuditMeta) {
|
|
|
|
|
const existing = await db.query.companies.findFirst({
|
|
|
|
|
where: eq(companies.id, id),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existing || existing.portId !== portId) {
|
|
|
|
|
throw new NotFoundError('Company');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: bypassing the shared `softDelete(...)` util: it sets the raw column key
|
|
|
|
|
// `archived_at`, which Drizzle does not recognise (the JS key is `archivedAt`)
|
|
|
|
|
// and therefore emits an empty SET clause. Until the utility is fixed, do the
|
|
|
|
|
// update inline. (See Task 2.3 for context.)
|
|
|
|
|
await db
|
|
|
|
|
.update(companies)
|
|
|
|
|
.set({ archivedAt: new Date() })
|
|
|
|
|
.where(and(eq(companies.id, id), eq(companies.portId, portId)));
|
|
|
|
|
|
|
|
|
|
void createAuditLog({
|
|
|
|
|
userId: meta.userId,
|
|
|
|
|
portId,
|
|
|
|
|
action: 'archive',
|
|
|
|
|
entityType: 'company',
|
|
|
|
|
entityId: id,
|
|
|
|
|
ipAddress: meta.ipAddress,
|
|
|
|
|
userAgent: meta.userAgent,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
emitToRoom(`port:${portId}`, 'company:archived', { companyId: id });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── List ────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function listCompanies(portId: string, query: ListCompaniesInput) {
|
|
|
|
|
const { page, limit, sort, order, search, includeArchived, status } = query;
|
|
|
|
|
|
|
|
|
|
const filters = [];
|
|
|
|
|
if (status) filters.push(eq(companies.status, status));
|
|
|
|
|
|
|
|
|
|
let sortColumn: typeof companies.name | typeof companies.createdAt | typeof companies.updatedAt =
|
|
|
|
|
companies.updatedAt;
|
|
|
|
|
if (sort === 'name') sortColumn = companies.name;
|
|
|
|
|
else if (sort === 'createdAt') sortColumn = companies.createdAt;
|
|
|
|
|
|
|
|
|
|
const result = await buildListQuery<Company>({
|
|
|
|
|
table: companies,
|
|
|
|
|
portIdColumn: companies.portId,
|
|
|
|
|
portId,
|
|
|
|
|
idColumn: companies.id,
|
|
|
|
|
updatedAtColumn: companies.updatedAt,
|
|
|
|
|
searchColumns: [companies.name, companies.legalName, companies.taxId],
|
|
|
|
|
searchTerm: search,
|
|
|
|
|
filters,
|
|
|
|
|
sort: sort ? { column: sortColumn, direction: order } : undefined,
|
|
|
|
|
page,
|
|
|
|
|
pageSize: limit,
|
|
|
|
|
includeArchived,
|
|
|
|
|
archivedAtColumn: companies.archivedAt,
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-27 21:57:13 +02:00
|
|
|
if (result.data.length === 0) return result;
|
|
|
|
|
|
|
|
|
|
const ids = result.data.map((r) => r.id);
|
|
|
|
|
|
|
|
|
|
const [memberCounts, yachtCounts] = await Promise.all([
|
|
|
|
|
db
|
|
|
|
|
.select({ companyId: companyMemberships.companyId, count: count() })
|
|
|
|
|
.from(companyMemberships)
|
|
|
|
|
.where(and(inArray(companyMemberships.companyId, ids), isNull(companyMemberships.endDate)))
|
|
|
|
|
.groupBy(companyMemberships.companyId),
|
|
|
|
|
db
|
|
|
|
|
.select({ ownerId: yachts.currentOwnerId, count: count() })
|
|
|
|
|
.from(yachts)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(yachts.portId, portId),
|
|
|
|
|
eq(yachts.currentOwnerType, 'company'),
|
|
|
|
|
inArray(yachts.currentOwnerId, ids),
|
|
|
|
|
isNull(yachts.archivedAt),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.groupBy(yachts.currentOwnerId),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const memberCountMap = new Map(memberCounts.map((r) => [r.companyId, r.count]));
|
|
|
|
|
const yachtCountMap = new Map(yachtCounts.map((r) => [r.ownerId, r.count]));
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...result,
|
|
|
|
|
data: result.data.map((row) => ({
|
|
|
|
|
...row,
|
|
|
|
|
memberCount: memberCountMap.get(row.id) ?? 0,
|
|
|
|
|
yachtCount: yachtCountMap.get(row.id) ?? 0,
|
|
|
|
|
})),
|
|
|
|
|
};
|
2026-04-24 12:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Autocomplete ────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function autocomplete(portId: string, q: string) {
|
|
|
|
|
const pattern = `%${q}%`;
|
|
|
|
|
return await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(companies)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(companies.portId, portId),
|
|
|
|
|
or(ilike(companies.name, pattern), ilike(companies.legalName, pattern)),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.limit(10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Upsert by name (find-or-create) ─────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find-or-create a company by (portId, lower(name)). NOT a Postgres UPSERT.
|
|
|
|
|
*
|
|
|
|
|
* Runs a case-insensitive SELECT scoped by portId; if found, returns it.
|
|
|
|
|
* Otherwise inserts a new row with the provided `name` verbatim. A concurrent
|
|
|
|
|
* insert that hits the partial unique index (23505) is re-raised as
|
|
|
|
|
* ConflictError for the caller to retry if desired.
|
|
|
|
|
*/
|
|
|
|
|
export async function upsertByName(portId: string, name: string, meta: AuditMeta) {
|
|
|
|
|
return await withTransaction(async (tx) => {
|
|
|
|
|
const existing = await tx.query.companies.findFirst({
|
|
|
|
|
where: and(eq(companies.portId, portId), sql`lower(${companies.name}) = lower(${name})`),
|
|
|
|
|
});
|
|
|
|
|
if (existing) return existing;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const [company] = await tx
|
|
|
|
|
.insert(companies)
|
|
|
|
|
.values({
|
|
|
|
|
portId,
|
|
|
|
|
name,
|
|
|
|
|
status: 'active',
|
|
|
|
|
})
|
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
|
|
void createAuditLog({
|
|
|
|
|
userId: meta.userId,
|
|
|
|
|
portId,
|
|
|
|
|
action: 'create',
|
|
|
|
|
entityType: 'company',
|
|
|
|
|
entityId: company!.id,
|
|
|
|
|
newValue: { name: company!.name, status: company!.status },
|
|
|
|
|
ipAddress: meta.ipAddress,
|
|
|
|
|
userAgent: meta.userAgent,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
emitToRoom(`port:${portId}`, 'company:created', { companyId: company!.id });
|
|
|
|
|
|
|
|
|
|
return company!;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (isUniqueViolation(err)) {
|
|
|
|
|
throw new ConflictError('company name already exists');
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
export async function setCompanyTags(
|
|
|
|
|
companyId: string,
|
|
|
|
|
portId: string,
|
|
|
|
|
tagIds: string[],
|
|
|
|
|
meta: AuditMeta,
|
|
|
|
|
) {
|
|
|
|
|
const company = await db.query.companies.findFirst({ where: eq(companies.id, companyId) });
|
|
|
|
|
if (!company || company.portId !== portId) throw new NotFoundError('Company');
|
|
|
|
|
|
|
|
|
|
await db.delete(companyTags).where(eq(companyTags.companyId, companyId));
|
|
|
|
|
|
|
|
|
|
if (tagIds.length > 0) {
|
|
|
|
|
await db.insert(companyTags).values(tagIds.map((tagId) => ({ companyId, tagId })));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void createAuditLog({
|
|
|
|
|
userId: meta.userId,
|
|
|
|
|
portId,
|
|
|
|
|
action: 'update',
|
|
|
|
|
entityType: 'company',
|
|
|
|
|
entityId: companyId,
|
|
|
|
|
newValue: { tagIds },
|
|
|
|
|
ipAddress: meta.ipAddress,
|
|
|
|
|
userAgent: meta.userAgent,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
emitToRoom(`port:${portId}`, 'company:updated', { companyId, changedFields: ['tags'] });
|
|
|
|
|
}
|