2023-11-08 05:35:48 +01:00
|
|
|
import { defineEventHandler, readBody, setResponseStatus } from "h3";
|
|
|
|
import { type Client } from "@prisma/client";
|
2023-05-11 06:03:22 +02:00
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
import getRequestingUser from "../utils/getRequestingUser";
|
|
|
|
import { database } from "../utils/database";
|
|
|
|
import { prismaToWeb } from "~/server/utils/prismaToWeb";
|
|
|
|
import Snowflake from "~/utils/snowflake";
|
2023-11-06 02:57:00 +01:00
|
|
|
|
|
|
|
import { createError } from "#imports";
|
2023-05-11 06:03:22 +02:00
|
|
|
|
2023-05-11 09:11:20 +02:00
|
|
|
const clientKeys: Array<string> = [
|
2023-05-11 06:03:22 +02:00
|
|
|
"name",
|
|
|
|
"address",
|
|
|
|
"phone",
|
|
|
|
"email",
|
|
|
|
];
|
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
export function checkIsClient<Patch extends boolean = boolean>(
|
2023-05-11 06:03:22 +02:00
|
|
|
value: any,
|
2023-11-08 05:35:48 +01:00
|
|
|
patch: Patch,
|
|
|
|
): value is Patch extends true ? Partial<Omit<Client, "id">> : Omit<Client, "id"> {
|
2023-05-11 06:03:22 +02:00
|
|
|
const errors = new Map<string, string>();
|
|
|
|
|
|
|
|
if (typeof value !== "object") {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
message: "Invalid body",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-09 11:38:36 +01:00
|
|
|
if (!(typeof value.name === "string" || value.name === null || (patch && value.name === undefined))) errors.set("name", "is not string or null");
|
|
|
|
if (!(typeof value.address === "string" || value.address === null || (patch && value.address === undefined))) errors.set("address", "is not string or null");
|
|
|
|
if (!(typeof value.phone === "string" || value.phone === null || (patch && value.phone === undefined))) errors.set("phone", "is not string or null");
|
|
|
|
if (!(typeof value.email === "string" || value.email === null || (patch && value.email === undefined))) errors.set("email", "is not string or null");
|
2023-05-11 06:03:22 +02:00
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
for (const i in value as Partial<Omit<Client, "id">>)
|
2023-05-11 06:03:22 +02:00
|
|
|
if (!clientKeys.includes(i)) errors.set(i, `excessive property`);
|
|
|
|
|
|
|
|
if (errors.size !== 0) {
|
|
|
|
let message = "Invalid Parameters: ";
|
|
|
|
for (const i in errors)
|
|
|
|
message += i + ", ";
|
|
|
|
message = message.slice(0, -2);
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
message,
|
|
|
|
data: {
|
|
|
|
errors: Object.fromEntries(errors),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
export default defineEventHandler(async (e) => {
|
|
|
|
const body = await readBody(e);
|
|
|
|
const id = new Snowflake().state;
|
|
|
|
const user = await getRequestingUser(e);
|
|
|
|
|
|
|
|
if (!checkIsClient(body, false)) throw createError({ message: "Invalid body", statusCode: 400 });
|
|
|
|
|
|
|
|
const rvalue = await database.client.create({
|
|
|
|
data: {
|
|
|
|
...body,
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
setResponseStatus(e, 201);
|
|
|
|
return prismaToWeb(rvalue);
|
2023-05-11 06:03:22 +02:00
|
|
|
});
|