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" });