feat: adds bookings and more tests

This commit is contained in:
Agusti Fernandez Pardo committed 2022-03-26 22:29:30 +01:00
1 parent c0d7623beb
commit ac307f7161
38 files changed
+515 -285

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
import prisma from "@calcom/prisma";
import { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/queryIdString";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = {
message?: string;
+2 -4
View File
@@ -4,7 +4,7 @@ import { ApiKey } from "@prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/queryIdString";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = {
data?: ApiKey;
@@ -17,8 +17,7 @@ export async function editApiKey(req: NextApiRequest, res: NextApiResponse<Respo
const safeQuery = await schemaQueryIdAsString.safeParse(query);
const safeBody = await schemaApiKey.safeParse(body);
if (method === "PATCH") {
if (safeQuery.success && safeBody.success) {
if (method === "PATCH" && safeQuery.success && safeBody.success) {
await prisma.apiKey.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
@@ -27,7 +26,6 @@ export async function editApiKey(req: NextApiRequest, res: NextApiResponse<Respo
}).catch(error => {
res.status(404).json({ message: `apiKey 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 API keys" });
+3 -6
View File
@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { ApiKey } from "@prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/queryIdString";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = {
data?: ApiKey;
@@ -13,14 +13,11 @@ type ResponseData = {
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
if (method === "GET") {
const safe = await schemaQueryIdAsString.safeParse(query);
if (safe.success) {
if (method === "GET" && safe.success) {
const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
if (apiKey) res.status(200).json({ data: apiKey });
if (!apiKey) res.status(404).json({ message: "API key was not found" });
}
else res.status(200).json({ data: apiKey });
} else {
// Reject any other HTTP method than POST
res.status(405).json({ message: "Only GET Method allowed" });
+5 -14
View File
@@ -13,9 +13,8 @@ type ResponseData = {
async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
if (method === "POST") {
const safe = schemaApiKey.safeParse(body);
if (safe.success && safe.data) {
const safe = schemaApiKey.safeParse(body);
if (method === "POST" && safe.success) {
const apiKey = await prisma.apiKey
.create({
data: {
@@ -24,17 +23,9 @@ async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseDa
})
if (apiKey) {
res.status(201).json({ data: apiKey });
} else {
// Reject any other HTTP method than POST
res.status(405).json({ error: "Only POST Method allowed" });
}
// .then((apiKey) => res.status(201).json({ data: apiKey }))
// .catch((error) => {
// res.status(400).json({ message: "Could not create apiKey", error: error })
// }
// )
}
} else {
res.status(404).json({message: "API Key not created"});
}
} else {
// Reject any other HTTP method than POST
res.status(405).json({ error: "Only POST Method allowed" });
+36
View File
@@ -0,0 +1,36 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
export async function booking(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.booking
.delete({ where: { id: safe.data.id } })
.then(() => {
// We only remove the booking type from the database if there's an existing resource.
res.status(200).json({ message: `booking-type with id: ${safe.data.id} deleted successfully` });
})
.catch((error) => {
// This catches the error thrown by prisma.booking.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 /booking-types/[id]/delete endpoint" });
}
}
}
export default withValidQueryIdTransformParseInt(booking);
+37
View File
@@ -0,0 +1,37 @@
import prisma from "@calcom/prisma";
import { Booking } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaBooking, withValidBooking } from "@lib/validations/booking";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Booking;
message?: string;
error?: unknown;
};
export async function editBooking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryId.safeParse(query);
const safeBody = await schemaBooking.safeParse(body);
if (method === "PATCH") {
if (safeQuery.success && safeBody.success) {
await prisma.booking.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
}).then(booking => {
res.status(200).json({ data: booking });
}).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 bookings" });
}
}
export default withValidQueryIdTransformParseInt(withValidBooking(editBooking));
+31
View File
@@ -0,0 +1,31 @@
import prisma from "@calcom/prisma";
import { Booking } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Booking;
message?: string;
error?: unknown;
};
export async function booking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryId.safeParse(query);
if (safe.success) {
if (method === "GET") {
const booking = await prisma.booking.findUnique({ where: { id: safe.data.id } });
if (booking) res.status(200).json({ data: booking });
if (!booking) 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" });
}
}
}
export default withValidQueryIdTransformParseInt(booking);
+19
View File
@@ -0,0 +1,19 @@
import prisma from "@calcom/prisma";
import { Booking } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: Booking[];
error?: unknown;
};
export default async function booking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
try {
const bookings = await prisma.booking.findMany();
res.status(200).json({ data: { ...bookings } });
} catch (error) {
// FIXME: Add zod for validation/error handling
res.status(400).json({ error: error });
}
}
+30
View File
@@ -0,0 +1,30 @@
import prisma from "@calcom/prisma";
import { Booking } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaBooking, withValidBooking } from "@lib/validations/booking";
type ResponseData = {
data?: Booking;
message?: string;
error?: string;
};
async function createBooking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
if (method === "POST") {
const safe = schemaBooking.safeParse(body);
if (safe.success && safe.data) {
await prisma.booking
.create({ data: safe.data })
.then((booking) => res.status(201).json({ data: booking }))
.catch((error) => res.status(400).json({ message: "Could not create booking type", error: error }));
}
} else {
// Reject any other HTTP method than POST
res.status(405).json({ error: "Only POST Method allowed" });
}
}
export default withValidBooking(createBooking);
+1 -1
View File
@@ -2,7 +2,7 @@ import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
+1 -1
View File
@@ -4,7 +4,7 @@ import { EventType } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaEventType, withValidEventType } from "@lib/validations/eventType";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: EventType;
+1 -1
View File
@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { EventType } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: EventType;
+7 -5
View File
@@ -5,15 +5,17 @@ import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: EventType[];
message?: string;
error?: unknown;
};
export default async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
try {
const { method } = req;
if (method === "GET") {
const eventTypes = await prisma.eventType.findMany();
res.status(200).json({ data: { ...eventTypes } });
} catch (error) {
// FIXME: Add zod for validation/error handling
res.status(400).json({ error: error });
}
} else {
// Reject any other HTTP method than POST
res.status(405).json({ message: "Only GET Method allowed" });
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
+1 -1
View File
@@ -4,7 +4,7 @@ import { Team } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaTeam, withValidTeam } from "@lib/validations/team";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Team;
+1 -1
View File
@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { Team } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Team;
+1 -1
View File
@@ -2,7 +2,7 @@ import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
+1 -1
View File
@@ -4,7 +4,7 @@ import { User } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaUser, withValidUser } from "@lib/validations/user";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: User;
+1 -1
View File
@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { User } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/queryIdTransformParseInt";
import { schemaQueryId, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: User;