Files
pn-new-crm/src/app/api/v1/berths/[id]/activity/route.ts
Matt Ciaccio 8cdee99310 feat(activity): per-entity Activity timeline (clients/yachts/companies/berths)
Until now only the global /admin/audit page surfaced audit_logs. Each
entity detail page either lacked the Activity tab entirely or rendered
"Activity log coming soon" text.

- entity-activity.service.loadEntityActivity wraps searchAuditLogs
  with actor-email resolution; reused by all 5 endpoints.
- New endpoints: /api/v1/{clients,yachts,companies,berths,interests}/[id]/activity,
  each gated on the per-entity .view permission and tenant-checked
  against ctx.portId.
- EntityActivityFeed renders a timeline with action verb ("Updated",
  "Archived"), actor name, relative time, and field old→new diff.
- client-tabs, yacht-tabs, company-tabs, berth-tabs now mount the feed
  on their Activity tab. Interest already has the richer
  InterestTimeline component.
- yacht-tabs YachtInterestsTab also gets a friendlier empty state with
  guidance copy.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 14:57:51 +02:00

32 lines
1.0 KiB
TypeScript

import { NextResponse } from 'next/server';
import { eq, and } from 'drizzle-orm';
import { withAuth, withPermission } from '@/lib/api/helpers';
import { db } from '@/lib/db';
import { berths } from '@/lib/db/schema/berths';
import { loadEntityActivity } from '@/lib/services/entity-activity.service';
import { errorResponse, NotFoundError } from '@/lib/errors';
export const GET = withAuth(
withPermission('berths', 'view', async (_req, ctx, params) => {
try {
const id = params.id;
if (!id) throw new NotFoundError('berth');
const exists = await db
.select({ id: berths.id })
.from(berths)
.where(and(eq(berths.id, id), eq(berths.portId, ctx.portId)))
.limit(1);
if (exists.length === 0) throw new NotFoundError('berth');
const data = await loadEntityActivity({
portId: ctx.portId,
entityType: 'berth',
entityId: id,
});
return NextResponse.json({ data });
} catch (error) {
return errorResponse(error);
}
}),
);