Files
pn-new-crm/src/app/api/v1/invoices/[id]/route.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import { NextResponse } from 'next/server';
import { withAuth, withPermission } from '@/lib/api/helpers';
import { parseBody } from '@/lib/api/route-helpers';
import { errorResponse } from '@/lib/errors';
import { getInvoiceById, updateInvoice, deleteInvoice } from '@/lib/services/invoices';
import { updateInvoiceSchema } from '@/lib/validators/invoices';
export const GET = withAuth(
withPermission('invoices', 'view', async (_req, ctx, params) => {
try {
const invoice = await getInvoiceById(params.id!, ctx.portId);
return NextResponse.json({ data: invoice });
} catch (error) {
return errorResponse(error);
}
}),
);
export const PATCH = withAuth(
withPermission('invoices', 'edit', async (req, ctx, params) => {
try {
const body = await parseBody(req, updateInvoiceSchema);
const invoice = await updateInvoice(params.id!, ctx.portId, body, {
userId: ctx.userId,
portId: ctx.portId,
ipAddress: ctx.ipAddress,
userAgent: ctx.userAgent,
});
return NextResponse.json({ data: invoice });
} catch (error) {
return errorResponse(error);
}
}),
);
export const DELETE = withAuth(
withPermission('invoices', 'delete', async (_req, ctx, params) => {
try {
await deleteInvoice(params.id!, ctx.portId, {
userId: ctx.userId,
portId: ctx.portId,
ipAddress: ctx.ipAddress,
userAgent: ctx.userAgent,
});
return new NextResponse(null, { status: 204 });
} catch (error) {
return errorResponse(error);
}
}),
);