2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { MembershipResponse } from "@lib/types";
|
|
|
|
import {
|
|
|
|
schemaMembershipBodyParams,
|
|
|
|
schemaMembershipPublic,
|
|
|
|
withValidMembership,
|
|
|
|
} from "@lib/validations/membership";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /api/memberships/new:
|
|
|
|
* post:
|
|
|
|
* summary: Creates a new membership
|
|
|
|
* tags:
|
|
|
|
* - memberships
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, membership created
|
|
|
|
* model: Membership
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Membership body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
async function createMembership(req: NextApiRequest, res: NextApiResponse<MembershipResponse>) {
|
|
|
|
const safe = schemaMembershipBodyParams.safeParse(req.body);
|
|
|
|
if (!safe.success) throw new Error("Invalid request body", safe.error);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
const membership = await prisma.membership.create({ data: safe.data });
|
|
|
|
const data = schemaMembershipPublic.parse(membership);
|
|
|
|
|
|
|
|
if (data) res.status(201).json({ data, message: "Membership created successfully" });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new membership",
|
|
|
|
error,
|
|
|
|
});
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
export default withMiddleware("HTTP_POST")(withValidMembership(createMembership));
|