48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
|
|
import { NextResponse } from 'next/server';
|
||
|
|
import { z } from 'zod';
|
||
|
|
|
||
|
|
import { withAuth, withPermission } from '@/lib/api/helpers';
|
||
|
|
import { parseBody } from '@/lib/api/route-helpers';
|
||
|
|
import { errorResponse } from '@/lib/errors';
|
||
|
|
import { listRelationships, createRelationship } from '@/lib/services/clients.service';
|
||
|
|
|
||
|
|
const createRelationshipSchema = z.object({
|
||
|
|
clientBId: z.string().min(1),
|
||
|
|
relationshipType: z.enum([
|
||
|
|
'referred_by',
|
||
|
|
'broker_for',
|
||
|
|
'family_member',
|
||
|
|
'same_vessel',
|
||
|
|
'custom',
|
||
|
|
]),
|
||
|
|
description: z.string().optional(),
|
||
|
|
});
|
||
|
|
|
||
|
|
export const GET = withAuth(
|
||
|
|
withPermission('clients', 'view', async (req, ctx, params) => {
|
||
|
|
try {
|
||
|
|
const relationships = await listRelationships(params.id!, ctx.portId);
|
||
|
|
return NextResponse.json({ data: relationships });
|
||
|
|
} catch (error) {
|
||
|
|
return errorResponse(error);
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
export const POST = withAuth(
|
||
|
|
withPermission('clients', 'edit', async (req, ctx, params) => {
|
||
|
|
try {
|
||
|
|
const body = await parseBody(req, createRelationshipSchema);
|
||
|
|
const rel = await createRelationship(params.id!, ctx.portId, body, {
|
||
|
|
userId: ctx.userId,
|
||
|
|
portId: ctx.portId,
|
||
|
|
ipAddress: ctx.ipAddress,
|
||
|
|
userAgent: ctx.userAgent,
|
||
|
|
});
|
||
|
|
return NextResponse.json({ data: rel }, { status: 201 });
|
||
|
|
} catch (error) {
|
||
|
|
return errorResponse(error);
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|