2023-11-06 02:57:00 +01:00
|
|
|
import { defineEventHandler, readBody } from "h3";
|
2023-05-24 09:40:45 +02:00
|
|
|
|
|
|
|
import { checkIsOrder } from "../orders.post";
|
2023-11-08 05:35:48 +01:00
|
|
|
import { getOrder } from "./[id].get";
|
|
|
|
import { database } from "~/server/utils/database";
|
|
|
|
import { prismaToWeb } from "~/server/utils/prismaToWeb";
|
2023-05-24 09:40:45 +02:00
|
|
|
|
2023-11-06 02:57:00 +01:00
|
|
|
import { createError } from "#imports";
|
|
|
|
|
2023-05-24 09:40:45 +02:00
|
|
|
export default defineEventHandler(async (e) => {
|
|
|
|
const body = await readBody(e);
|
2023-11-08 05:35:48 +01:00
|
|
|
const id = e.context.params?.id as string;
|
2023-05-24 09:40:45 +02:00
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
if (!checkIsOrder(body, true)) throw createError({ message: "Invalid body", statusCode: 400 });
|
2023-05-24 09:40:45 +02:00
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
await database.order.update({
|
|
|
|
where: {
|
|
|
|
id: BigInt(id),
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
clientId: body.clientId ? BigInt(body.clientId) : undefined,
|
|
|
|
draft: body.draft,
|
|
|
|
},
|
|
|
|
});
|
2023-05-24 09:40:45 +02:00
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
return getOrder(BigInt(id)).then(prismaToWeb);
|
2023-05-24 09:40:45 +02:00
|
|
|
});
|