Feat: event-types and tests
This commit is contained in:
1 parent
2e2f6a5e57
commit
a2817a2fee
16 files changed
+15892
-205
No files matched your search
@@ -1,19 +1,18 @@
|
||||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { withValidation } from "next-validations";
|
||||
|
||||
import schema from "@lib/validation/queryIdTransformParseInt";
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
type ResponseData = {
|
||||
message?: string;
|
||||
error?: any;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schema.safeParse(query);
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "DELETE") {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
@@ -25,19 +24,13 @@ export async function eventType(req: NextApiRequest, res: NextApiResponse<Respon
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.eventType.delete() if the resource is not found.
|
||||
res.status(400).json({ error: error });
|
||||
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({ error: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" });
|
||||
res.status(405).json({ message: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "query",
|
||||
});
|
||||
|
||||
export default validate(eventType);
|
||||
export default withValidQueryId(eventType);
|
||||
@@ -1,41 +1,37 @@
|
||||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { withValidation } from "next-validations";
|
||||
|
||||
import schema from "@lib/validation/eventType";
|
||||
import schemaQuery from "@lib/validation/queryIdTransformParseInt";
|
||||
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
error?: any;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function editEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQuery.safeParse(query);
|
||||
const safeBody = await schema.safeParse(body);
|
||||
const safeQuery = await schemaQueryId.safeParse(query);
|
||||
const safeBody = await schemaEventType.safeParse(body);
|
||||
|
||||
if (safeQuery.success && safeBody.success) {
|
||||
if (method === "PATCH") {
|
||||
const event = await prisma.eventType.update({
|
||||
if (method === "PATCH") {
|
||||
if (safeQuery.success && safeBody.success) {
|
||||
await prisma.eventType.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: { ...safeBody.data },
|
||||
data: safeBody.data,
|
||||
}).then(event => {
|
||||
res.status(200).json({ data: event });
|
||||
}).catch(error => {
|
||||
res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
||||
});
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ error: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only GET Method allowed" });
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only PATCH Method allowed for updating event-types" });
|
||||
}
|
||||
}
|
||||
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "body",
|
||||
});
|
||||
|
||||
export default validate(editEventType);
|
||||
export default withValidQueryId(withValidEventType(editEventType));
|
||||
@@ -1,43 +1,31 @@
|
||||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { withValidation } from "next-validations";
|
||||
|
||||
import schema from "@lib/validation/queryIdTransformParseInt";
|
||||
import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
error?: any;
|
||||
message?: string;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schema.safeParse(query);
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "GET") {
|
||||
const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ error: "Event type not found" });
|
||||
} else if (method === "PATCH") {
|
||||
const event = await prisma.eventType.update({
|
||||
where: { id: safe.data.id },
|
||||
data: { title: "Updated title" },
|
||||
});
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ error: "Event type not found" });
|
||||
if (!event) res.status(404).json({ message: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only GET Method allowed" });
|
||||
res.status(405).json({ message: "Only GET Method allowed" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "query",
|
||||
});
|
||||
|
||||
export default validate(eventType);
|
||||
export default withValidQueryId(eventType);
|
||||
@@ -4,7 +4,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||
const prisma = new PrismaClient();
|
||||
type ResponseData = {
|
||||
data?: EventType[];
|
||||
error?: any;
|
||||
error?: unknown;
|
||||
};
|
||||
|
||||
export default async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { withValidation } from "next-validations";
|
||||
|
||||
import schema from "@lib/validation/eventType";
|
||||
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "body",
|
||||
});
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
message?: string;
|
||||
@@ -21,7 +13,7 @@ type ResponseData = {
|
||||
async function createEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { body, method } = req;
|
||||
if (method === "POST") {
|
||||
const safe = schema.safeParse(body);
|
||||
const safe = schemaEventType.safeParse(body);
|
||||
if (safe.success && safe.data) {
|
||||
await prisma.eventType
|
||||
.create({ data: safe.data })
|
||||
@@ -34,4 +26,4 @@ async function createEventType(req: NextApiRequest, res: NextApiResponse<Respons
|
||||
}
|
||||
}
|
||||
|
||||
export default validate(createEventType);
|
||||
export default withValidEventType(createEventType);
|
||||
@@ -1,19 +0,0 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
import { User } from "@calcom/prisma/client";
|
||||
|
||||
type Data = {
|
||||
message: string;
|
||||
};
|
||||
|
||||
export default async function userId(req: NextApiRequest, res: NextApiResponse<Data>) {
|
||||
const { id }: { id?: string } = req.query;
|
||||
if (!id) return res.status(404).json({ message: `User not found` });
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: parseInt(id),
|
||||
},
|
||||
});
|
||||
return res.json({ message: `Hello ${user?.name}` });
|
||||
}
|
||||
Reference in new issue
Block a user