2022-03-26 04:28:53 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-25 22:26:22 +00:00
|
|
|
|
2022-03-29 01:23:22 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { User } from "@calcom/prisma/client";
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-03-25 22:26:22 +00:00
|
|
|
import { schemaUser, withValidUser } from "@lib/validations/user";
|
|
|
|
|
2022-03-29 01:23:22 +00:00
|
|
|
|
2022-03-25 22:26:22 +00:00
|
|
|
type ResponseData = {
|
|
|
|
data?: User;
|
2022-03-29 01:23:22 +00:00
|
|
|
error?: object;
|
2022-03-25 22:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function createUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
2022-03-29 01:59:57 +00:00
|
|
|
const safe = schemaUser.safeParse(req.body);
|
|
|
|
if (safe.success) {
|
2022-03-29 01:23:22 +00:00
|
|
|
const data = await prisma.user
|
|
|
|
.create({ data: safe.data })
|
|
|
|
if (data) res.status(201).json({ data })
|
|
|
|
else (error: unknown) => res.status(400).json({ error: { message: "Could not create user type", error: error } });
|
2022-03-29 01:59:57 +00:00
|
|
|
}
|
2022-03-25 22:26:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
export default withMiddleware("addRequestId","postOnly")(
|
|
|
|
withValidUser(
|
|
|
|
createUser
|
|
|
|
)
|
|
|
|
);
|