46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from 'next/server';
|
||
|
|
|
||
|
|
import { withAuth } from '@/lib/api/helpers';
|
||
|
|
import { errorResponse, NotFoundError } from '@/lib/errors';
|
||
|
|
import { setValuesSchema } from '@/lib/validators/custom-fields';
|
||
|
|
import { getValues, setValues } from '@/lib/services/custom-fields.service';
|
||
|
|
|
||
|
|
export const GET = withAuth(async (_req: NextRequest, ctx, params) => {
|
||
|
|
try {
|
||
|
|
const { entityId } = params;
|
||
|
|
if (!entityId) throw new NotFoundError('Entity');
|
||
|
|
|
||
|
|
const data = await getValues(entityId, ctx.portId);
|
||
|
|
return NextResponse.json({ data });
|
||
|
|
} catch (error) {
|
||
|
|
return errorResponse(error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
export const PUT = withAuth(async (req: NextRequest, ctx, params) => {
|
||
|
|
try {
|
||
|
|
const { entityId } = params;
|
||
|
|
if (!entityId) throw new NotFoundError('Entity');
|
||
|
|
|
||
|
|
const body = await req.json();
|
||
|
|
const { values } = setValuesSchema.parse(body);
|
||
|
|
|
||
|
|
const result = await setValues(
|
||
|
|
entityId,
|
||
|
|
ctx.portId,
|
||
|
|
ctx.userId,
|
||
|
|
values as Array<{ fieldId: string; value: unknown }>,
|
||
|
|
{
|
||
|
|
userId: ctx.userId,
|
||
|
|
portId: ctx.portId,
|
||
|
|
ipAddress: ctx.ipAddress,
|
||
|
|
userAgent: ctx.userAgent,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
return NextResponse.json({ data: result });
|
||
|
|
} catch (error) {
|
||
|
|
return errorResponse(error);
|
||
|
|
}
|
||
|
|
});
|