30 lines
1023 B
TypeScript
30 lines
1023 B
TypeScript
import { getApiUser } from "@/session.server";
|
|
import prisma from "@/lib/prisma.server";
|
|
import { z } from "zod";
|
|
|
|
const serviceSchema = z.object({
|
|
companyId: z.string().min(1),
|
|
name: z.string().min(1),
|
|
description: z.string().optional(),
|
|
unit: z.string().optional(),
|
|
unitPrice: z.number(),
|
|
taxRate: z.number(),
|
|
});
|
|
|
|
export async function action({ request }: { request: Request }) {
|
|
const user = await getApiUser(request);
|
|
if (!user) return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
const body = await request.json();
|
|
const parsed = serviceSchema.safeParse(body);
|
|
if (!parsed.success) return Response.json({ error: parsed.error.issues }, { status: 400 });
|
|
|
|
const company = await prisma.company.findFirst({
|
|
where: { id: parsed.data.companyId, userId: user.id },
|
|
});
|
|
if (!company) return Response.json({ error: "Company not found" }, { status: 404 });
|
|
|
|
const service = await prisma.service.create({ data: parsed.data });
|
|
return Response.json(service, { status: 201 });
|
|
}
|