feat: adds api keys endpoints, new QueryIdAsString as apiKey has a UUID string-like ID unlike the numbers of event-type or others
parent
c2234593db
commit
d9c34f67e6
|
@ -0,0 +1,20 @@
|
|||
import { withValidation } from "next-validations";
|
||||
import { z } from "zod";
|
||||
|
||||
// Extracted out as utility function so can be reused
|
||||
// at different endpoints that require this validation.
|
||||
const schemaQueryId = z
|
||||
.object({
|
||||
// since nextjs parses query params as strings,
|
||||
// we need to cast them to numbers using z.transform() and parseInt()
|
||||
id: z.string().uuid()
|
||||
})
|
||||
.strict();
|
||||
|
||||
const withValidQueryIdString = withValidation({
|
||||
schema: schemaQueryId,
|
||||
type: "Zod",
|
||||
mode: "query",
|
||||
});
|
||||
|
||||
export { schemaQueryId, withValidQueryIdString };
|
|
@ -14,10 +14,10 @@ const schemaQueryId = z
|
|||
})
|
||||
.strict();
|
||||
|
||||
const withValidQueryId = withValidation({
|
||||
const withValidQueryIdTransformParseInt = withValidation({
|
||||
schema: schemaQueryId,
|
||||
type: "Zod",
|
||||
mode: "query",
|
||||
});
|
||||
|
||||
export { schemaQueryId, withValidQueryId };
|
||||
export { schemaQueryId, withValidQueryIdTransformParseInt };
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
// https://www.npmjs.com/package/next-transpile-modules
|
||||
// This makes our @calcom/prisma package from the monorepo to be transpiled and usable by API
|
||||
const withTM = require("next-transpile-modules")([
|
||||
"@calcom/prisma",
|
||||
]);
|
||||
|
||||
// use something like withPlugins([withTM], {}) if more plugins added later.
|
||||
|
||||
module.exports = withTM({
|
||||
async rewrites() {
|
||||
|
|
|
@ -1,36 +1,34 @@
|
|||
import { PrismaClient } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
import { schemaQueryId, withValidQueryIdString } from "@lib/validations/queryIdString";
|
||||
|
||||
type ResponseData = {
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "DELETE") {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.eventType
|
||||
prisma.apiKey
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the event type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `event-type with id: ${safe.data.id} deleted successfully` });
|
||||
// We only remove the api key from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `api-key with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.eventType.delete() if the resource is not found.
|
||||
// This catches the error thrown by prisma.apiKey.delete() if the resource is not found.
|
||||
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" });
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /api-keys/[id]/delete endpoint" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default withValidQueryId(eventType);
|
||||
export default withValidQueryIdString(apiKey);
|
||||
|
|
|
@ -1,37 +1,37 @@
|
|||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { ApiKey } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
|
||||
import { schemaQueryId, withValidQueryIdString } from "@lib/validations/queryIdString";
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
data?: ApiKey;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function editEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
export async function editApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryId.safeParse(query);
|
||||
const safeBody = await schemaEventType.safeParse(body);
|
||||
const safeBody = await schemaApiKey.safeParse(body);
|
||||
|
||||
if (method === "PATCH") {
|
||||
if (safeQuery.success && safeBody.success) {
|
||||
await prisma.eventType.update({
|
||||
await prisma.apiKey.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
}).then(event => {
|
||||
res.status(200).json({ data: event });
|
||||
}).then(apiKey => {
|
||||
res.status(200).json({ data: apiKey });
|
||||
}).catch(error => {
|
||||
res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only PATCH Method allowed for updating event-types" });
|
||||
res.status(405).json({ message: "Only PATCH Method allowed for updating apiKey-types" });
|
||||
}
|
||||
}
|
||||
|
||||
export default withValidQueryId(withValidEventType(editEventType));
|
||||
export default withValidQueryIdString(withValidApiKey(editApiKey));
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { ApiKey } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
import { schemaQueryId, withValidQueryIdString } from "@lib/validations/queryIdString";
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
data?: ApiKey;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "GET") {
|
||||
const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
|
||||
const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ message: "Event type not found" });
|
||||
if (apiKey) res.status(200).json({ data: apiKey });
|
||||
if (!apiKey) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
|
@ -28,4 +28,4 @@ export async function eventType(req: NextApiRequest, res: NextApiResponse<Respon
|
|||
}
|
||||
|
||||
|
||||
export default withValidQueryId(eventType);
|
||||
export default withValidQueryIdString(apiKey);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { PrismaClient, ApiKey } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { ApiKey } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
type ResponseData = {
|
||||
data?: ApiKey[];
|
||||
error?: unknown;
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { PrismaClient, ApiKey } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { ApiKey } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
type ResponseData = {
|
||||
data?: ApiKey;
|
||||
message?: string;
|
||||
|
@ -18,8 +18,8 @@ async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseDa
|
|||
await prisma.apiKey
|
||||
.create({ data: { ...safe.data, user: { connect: { id: 1 } }
|
||||
}})
|
||||
.then((event) => res.status(201).json({ data: event }))
|
||||
.catch((error) => res.status(400).json({ message: "Could not create event type", error: error }));
|
||||
.then((apiKey) => res.status(201).json({ data: apiKey }))
|
||||
.catch((error) => res.status(400).json({ message: "Could not create apiKey", error: error }));
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { PrismaClient } from "@prisma/client";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
type ResponseData = {
|
||||
message?: string;
|
||||
|
@ -33,4 +33,4 @@ export async function eventType(req: NextApiRequest, res: NextApiResponse<Respon
|
|||
}
|
||||
}
|
||||
|
||||
export default withValidQueryId(eventType);
|
||||
export default withValidQueryIdTransformParseInt(eventType);
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { EventType } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
|
@ -34,4 +34,4 @@ export async function editEventType(req: NextApiRequest, res: NextApiResponse<Re
|
|||
}
|
||||
}
|
||||
|
||||
export default withValidQueryId(withValidEventType(editEventType));
|
||||
export default withValidQueryIdTransformParseInt(withValidEventType(editEventType));
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { EventType } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
|
@ -28,4 +28,4 @@ export async function eventType(req: NextApiRequest, res: NextApiResponse<Respon
|
|||
}
|
||||
|
||||
|
||||
export default withValidQueryId(eventType);
|
||||
export default withValidQueryIdTransformParseInt(eventType);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { EventType } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
type ResponseData = {
|
||||
data?: EventType[];
|
||||
error?: unknown;
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { EventType } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
message?: string;
|
||||
|
|
Loading…
Reference in New Issue