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

75 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 { CredentialResponse, CredentialsResponse } from "@lib/types";
2022-04-13 00:12:16 +00:00
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
import { schemaCredentialBodyParams, schemaCredentialPublic } from "@lib/validations/credential";
/**
* @swagger
* /api/credentials:
* get:
* summary: Get all credentials
* tags:
* - credentials
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No credentials were found
* post:
* summary: Creates a new credential
* tags:
* - credentials
* responses:
* 201:
* description: OK, credential created
* model: Credential
* 400:
* description: Bad request. Credential body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createOrlistAllCredentials(
req: NextApiRequest,
res: NextApiResponse<CredentialsResponse | CredentialResponse>
) {
const { method } = req;
2022-04-13 00:12:16 +00:00
const userId = getCalcomUserId(res);
if (method === "GET") {
2022-04-13 00:12:16 +00:00
const data = await prisma.credential.findMany({ where: { userId } });
const credentials = data.map((credential) => schemaCredentialPublic.parse(credential));
if (credentials) res.status(200).json({ credentials });
else
(error: Error) =>
res.status(404).json({
message: "No Credentials were found",
error,
});
} else if (method === "POST") {
const safe = schemaCredentialBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
const data = await prisma.credential.create({ data: safe.data });
const credential = schemaCredentialPublic.parse(data);
2022-04-13 00:12:16 +00:00
if (credential)
res
.status(201)
.json({ credential: { ...credential, userId }, message: "Credential created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new credential",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllCredentials);