cal.pub0.org/pages/api/memberships/index.ts

74 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-03-30 12:17:55 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
2022-03-30 12:17:55 +00:00
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { MembershipResponse, MembershipsResponse } from "@lib/types";
import { schemaMembershipBodyParams, schemaMembershipPublic } from "@lib/validations/membership";
async function createOrlistAllMemberships(
2022-04-29 15:29:57 +00:00
{ method, body, userId }: NextApiRequest,
res: NextApiResponse<MembershipsResponse | MembershipResponse>
) {
if (method === "GET") {
2022-04-29 15:29:57 +00:00
/**
* @swagger
* /memberships:
* get:
* summary: Find all memberships
* tags:
* - memberships
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No memberships were found
*/
2022-04-20 22:55:22 +00:00
const data = await prisma.membership.findMany({ where: { userId } });
const memberships = data.map((membership) => schemaMembershipPublic.parse(membership));
if (memberships) res.status(200).json({ memberships });
else
(error: Error) =>
res.status(404).json({
message: "No Memberships were found",
error,
});
} else if (method === "POST") {
2022-04-29 15:29:57 +00:00
/**
* @swagger
* /memberships:
* post:
* summary: Creates a new membership
* tags:
* - memberships
* responses:
* 201:
* description: OK, membership created
* 400:
* description: Bad request. Membership body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
const safe = schemaMembershipBodyParams.safeParse(body);
2022-05-17 17:33:18 +00:00
if (!safe.success) {
res.status(400).json({ message: "Invalid request body" });
return;
}
2022-04-20 22:55:22 +00:00
const data = await prisma.membership.create({ data: { ...safe.data, userId } });
const membership = schemaMembershipPublic.parse(data);
if (membership) res.status(201).json({ membership, message: "Membership created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new membership",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllMemberships);