refactor(services): centralize AuditMeta + transactional setEntityTags helper
The same `interface AuditMeta { userId; portId; ipAddress; userAgent }`
was duplicated in 26 service files. Move the canonical definition into
`@/lib/audit` next to the related types and update every service to
import it. `ServiceAuditMeta` (the alias used in invoices.ts and
expenses.ts) collapses into the same name.
Tag CRUD across clients/companies/yachts/interests/berths followed an
identical wipe-then-rewrite recipe with two latent issues: the delete
and insert weren't wrapped in a transaction (a partial failure left
the entity with zero tags) and the audit-log payload shape diverged
(`newValue: { tagIds }` for clients/yachts/companies but
`metadata: { type: 'tags_updated', tagIds }` for interests/berths).
Extract `setEntityTags` in `entity-tags.helper.ts` that performs the
delete+insert inside a single transaction, normalizes the audit payload
to `newValue: { tagIds }`, and dispatches the per-entity socket event
through a switch so `ServerToClientEvents` typing stays intact.
The five `setXTags(...)` service functions now do parent-row tenant
verification and delegate the join-table work + side effects.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,9 +3,10 @@ import { db } from '@/lib/db';
|
||||
import { yachts, yachtOwnershipHistory, yachtTags, clients } from '@/lib/db/schema';
|
||||
import type { Yacht } from '@/lib/db/schema/yachts';
|
||||
import { companies } from '@/lib/db/schema/companies';
|
||||
import { createAuditLog } from '@/lib/audit';
|
||||
import { createAuditLog, type AuditMeta } from '@/lib/audit';
|
||||
import { NotFoundError, ValidationError } from '@/lib/errors';
|
||||
import { emitToRoom } from '@/lib/socket/server';
|
||||
import { setEntityTags } from '@/lib/services/entity-tags.helper';
|
||||
import { diffEntity } from '@/lib/entity-diff';
|
||||
import { buildListQuery } from '@/lib/db/query-builder';
|
||||
import { withTransaction } from '@/lib/db/utils';
|
||||
@@ -19,13 +20,6 @@ import type {
|
||||
|
||||
type CreateYachtInput = z.input<typeof createYachtSchema>;
|
||||
|
||||
interface AuditMeta {
|
||||
userId: string;
|
||||
portId: string;
|
||||
ipAddress: string;
|
||||
userAgent: string;
|
||||
}
|
||||
|
||||
async function assertOwnerExists(
|
||||
portId: string,
|
||||
owner: { type: 'client' | 'company'; id: string },
|
||||
@@ -408,22 +402,14 @@ export async function setYachtTags(
|
||||
const yacht = await db.query.yachts.findFirst({ where: eq(yachts.id, yachtId) });
|
||||
if (!yacht || yacht.portId !== portId) throw new NotFoundError('Yacht');
|
||||
|
||||
await db.delete(yachtTags).where(eq(yachtTags.yachtId, yachtId));
|
||||
|
||||
if (tagIds.length > 0) {
|
||||
await db.insert(yachtTags).values(tagIds.map((tagId) => ({ yachtId, tagId })));
|
||||
}
|
||||
|
||||
void createAuditLog({
|
||||
userId: meta.userId,
|
||||
portId,
|
||||
action: 'update',
|
||||
entityType: 'yacht',
|
||||
await setEntityTags({
|
||||
joinTable: yachtTags,
|
||||
entityColumn: yachtTags.yachtId,
|
||||
tagColumn: yachtTags.tagId,
|
||||
entityId: yachtId,
|
||||
newValue: { tagIds },
|
||||
ipAddress: meta.ipAddress,
|
||||
userAgent: meta.userAgent,
|
||||
portId,
|
||||
tagIds,
|
||||
meta,
|
||||
entityType: 'yacht',
|
||||
});
|
||||
|
||||
emitToRoom(`port:${portId}`, 'yacht:updated', { yachtId, changedFields: ['tags'] });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user