chore: bring up to date booking references, bookings, and credentials
This commit is contained in:
1 parent
55f93cded6
commit
1c19032884
20 files changed
+593
-258
No files matched your search
@@ -2,28 +2,49 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { BaseResponse } from "@lib/types";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
withValidQueryIdTransformParseInt,
|
||||
} from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
/**
|
||||
* @swagger
|
||||
* /api/credentials/{id}/delete:
|
||||
* delete:
|
||||
* summary: Remove an existing credential
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* schema:
|
||||
* type: integer
|
||||
* required: true
|
||||
* description: Numeric ID of the credential to delete
|
||||
* tags:
|
||||
* - credentials
|
||||
* responses:
|
||||
* 201:
|
||||
* description: OK, credential removed successfuly
|
||||
* model: Credential
|
||||
* 400:
|
||||
* description: Bad request. Credential id is invalid.
|
||||
* 401:
|
||||
* description: Authorization information is missing or invalid.
|
||||
*/
|
||||
export async function deleteCredential(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
|
||||
const safe = await schemaQueryIdParseInt.safeParse(req.query);
|
||||
if (!safe.success) throw new Error("Invalid request query", safe.error);
|
||||
|
||||
export async function deleteCredential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
if (method === "DELETE" && safe.success && safe.data) {
|
||||
const credential = await prisma.credential.delete({ where: { id: safe.data.id } });
|
||||
// We only remove the credential type from the database if there's an existing resource.
|
||||
if (credential)
|
||||
res.status(200).json({ message: `credential with id: ${safe.data.id} deleted successfully` });
|
||||
// This catches the error thrown by prisma.credential.delete() if the resource is not found.
|
||||
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only DELETE Method allowed" });
|
||||
const data = await prisma.credential.delete({ where: { id: safe.data.id } });
|
||||
|
||||
if (data) res.status(200).json({ message: `Credential with id: ${safe.data.id} deleted successfully` });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(400).json({
|
||||
message: `Credential with id: ${safe.data.id} was not able to be processed`,
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(deleteCredential);
|
||||
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteCredential));
|
||||
@@ -1,38 +1,62 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
import { Credential } from "@calcom/prisma/client";
|
||||
|
||||
import { schemaCredential, withValidCredential } from "@lib/validations/credential";
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { CredentialResponse } from "@lib/types";
|
||||
import {
|
||||
schemaCredentialBodyParams,
|
||||
schemaCredentialPublic,
|
||||
withValidCredential,
|
||||
} from "@lib/validations/credential";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
withValidQueryIdTransformParseInt,
|
||||
} from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Credential;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
/**
|
||||
* @swagger
|
||||
* /api/credentials/{id}/edit:
|
||||
* patch:
|
||||
* summary: Edit an existing credential
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* schema:
|
||||
* type: integer
|
||||
* required: true
|
||||
* description: Numeric ID of the credential to edit
|
||||
* tags:
|
||||
* - credentials
|
||||
* responses:
|
||||
* 201:
|
||||
* description: OK, credential edited successfuly
|
||||
* model: Credential
|
||||
* 400:
|
||||
* description: Bad request. Credential body is invalid.
|
||||
* 401:
|
||||
* description: Authorization information is missing or invalid.
|
||||
*/
|
||||
export async function editCredential(req: NextApiRequest, res: NextApiResponse<CredentialResponse>) {
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
|
||||
const safeBody = await schemaCredentialBodyParams.safeParse(req.body);
|
||||
|
||||
export async function editCredential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = await schemaCredential.safeParse(body);
|
||||
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
|
||||
const credential = await prisma.credential.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
});
|
||||
const data = schemaCredentialPublic.parse(credential);
|
||||
|
||||
if (method === "PATCH" && safeQuery.success && safeBody.success) {
|
||||
const data = await prisma.credential.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
});
|
||||
if (data) res.status(200).json({ data });
|
||||
else
|
||||
res
|
||||
.status(404)
|
||||
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
|
||||
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only PATCH Method allowed for updating credentials" });
|
||||
if (data) res.status(200).json({ data });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(404).json({
|
||||
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(withValidCredential(editCredential));
|
||||
export default withMiddleware("HTTP_PATCH")(
|
||||
withValidQueryIdTransformParseInt(withValidCredential(editCredential))
|
||||
);
|
||||
@@ -1,29 +1,51 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
import { Credential } from "@calcom/prisma/client";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { CredentialResponse } from "@lib/types";
|
||||
import { schemaCredentialPublic } from "@lib/validations/credential";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
withValidQueryIdTransformParseInt,
|
||||
} from "@lib/validations/shared/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Credential;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
/**
|
||||
* @swagger
|
||||
* /api/credentials/{id}:
|
||||
* get:
|
||||
* summary: Get a credential by ID
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* schema:
|
||||
* type: integer
|
||||
* required: true
|
||||
* description: Numeric ID of the credential to get
|
||||
* tags:
|
||||
* - credentials
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* 401:
|
||||
* description: Authorization information is missing or invalid.
|
||||
* 404:
|
||||
* description: Credential was not found
|
||||
*/
|
||||
export async function credentialById(req: NextApiRequest, res: NextApiResponse<CredentialResponse>) {
|
||||
const safe = await schemaQueryIdParseInt.safeParse(req.query);
|
||||
if (!safe.success) throw new Error("Invalid request query");
|
||||
|
||||
export async function credential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||
if (method === "GET" && safe.success) {
|
||||
const data = await prisma.credential.findUnique({ where: { id: safe.data.id } });
|
||||
const credential = await prisma.credential.findUnique({ where: { id: safe.data.id } });
|
||||
const data = schemaCredentialPublic.parse(credential);
|
||||
|
||||
if (data) res.status(200).json({ data });
|
||||
else res.status(404).json({ message: "Event type not found" });
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ message: "Only GET Method allowed" });
|
||||
if (credential) res.status(200).json({ data });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(404).json({
|
||||
message: "Credential was not found",
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withValidQueryIdTransformParseInt(credential);
|
||||
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(credentialById));
|
||||
@@ -1,15 +1,37 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
import { Credential } from "@calcom/prisma/client";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Credential[];
|
||||
error?: unknown;
|
||||
};
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { CredentialsResponse } from "@lib/types";
|
||||
import { 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
|
||||
*/
|
||||
async function allCredentials(_: NextApiRequest, res: NextApiResponse<CredentialsResponse>) {
|
||||
const credentials = await prisma.credential.findMany();
|
||||
const data = credentials.map((credential) => schemaCredentialPublic.parse(credential));
|
||||
|
||||
export default async function credential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const data = await prisma.credential.findMany();
|
||||
if (data) res.status(200).json({ data });
|
||||
else res.status(400).json({ error: "No data found" });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(404).json({
|
||||
message: "No Credentials were found",
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withMiddleware("HTTP_GET")(allCredentials);
|
||||
@@ -1,26 +1,52 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
import { Credential } from "@calcom/prisma/client";
|
||||
|
||||
import { schemaCredential, withValidCredential } from "@lib/validations/credential";
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { CredentialResponse } from "@lib/types";
|
||||
import {
|
||||
schemaCredentialBodyParams,
|
||||
schemaCredentialPublic,
|
||||
withValidCredential,
|
||||
} from "@lib/validations/credential";
|
||||
|
||||
type ResponseData = {
|
||||
data?: Credential;
|
||||
message?: string;
|
||||
error?: string;
|
||||
};
|
||||
/**
|
||||
* @swagger
|
||||
* /api/credentials/new:
|
||||
* post:
|
||||
* summary: Creates a new credential
|
||||
* requestBody:
|
||||
* description: Optional description in *Markdown*
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/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 createCredential(req: NextApiRequest, res: NextApiResponse<CredentialResponse>) {
|
||||
const safe = schemaCredentialBodyParams.safeParse(req.body);
|
||||
if (!safe.success) throw new Error("Invalid request body", safe.error);
|
||||
|
||||
async function createCredential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { body, method } = req;
|
||||
const safe = schemaCredential.safeParse(body);
|
||||
if (method === "POST" && safe.success) {
|
||||
await prisma.credential
|
||||
.create({ data: safe.data })
|
||||
.then((data) => res.status(201).json({ data }))
|
||||
.catch((error) => res.status(400).json({ message: "Could not create credential type", error: error }));
|
||||
// Reject any other HTTP method than POST
|
||||
} else res.status(405).json({ error: "Only POST Method allowed" });
|
||||
const credential = await prisma.credential.create({ data: safe.data });
|
||||
const data = schemaCredentialPublic.parse(credential);
|
||||
|
||||
if (data) res.status(201).json({ data, message: "Credential created successfully" });
|
||||
else
|
||||
(error: Error) =>
|
||||
res.status(400).json({
|
||||
message: "Could not create new credential",
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
export default withValidCredential(createCredential);
|
||||
export default withMiddleware("HTTP_POST")(withValidCredential(createCredential));
|
||||
Reference in new issue
Block a user