feat: unitfy new/index of teams and selected calendars

This commit is contained in:
Agusti Fernandez Pardo committed 2022-04-09 01:29:26 +02:00
1 parent fbc83a5cc9
commit af72b1f5b5
9 files changed
+135 -206

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
export default async function CalcomApi(req: NextApiRequest, res: NextApiResponse) {
export default async function CalcomApi(_: NextApiRequest, res: NextApiResponse) {
res.status(201).json({ message: "Welcome to Cal.com API - docs are at https://docs.cal.com/api" });
}
+32 -14
View File
@@ -12,7 +12,7 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/
/**
* @swagger
* /api/selected-calendars/{userId}_{teamId}:
* /v1/selected-calendars/{userId}_{integration}_{externalId}:
* get:
* summary: Get a selected-calendar by userID and teamID
* parameters:
@@ -21,13 +21,19 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/
* schema:
* type: integer
* required: true
* description: Numeric userId of the selected-calendar to get
* description: userId of the selected calendar to get
* - in: path
* name: teamId
* name: externalId
* schema:
* type: integer
* type: string
* required: true
* description: Numeric teamId of the selected-calendar to get
* description: externalId of the selected calendar to get
* - in: path
* name: integration
* schema:
* type: string
* required: true
* description: integration of the selected calendar to get
* tags:
* - selected-calendars
* responses:
@@ -38,7 +44,7 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/
* 404:
* description: SelectedCalendar was not found
* patch:
* summary: Edit an existing selected-calendar
* summary: Edit an existing selected calendar
* consumes:
* - application/json
* parameters:
@@ -54,13 +60,19 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/
* schema:
* type: integer
* required: true
* description: Numeric userId of the selected-calendar to get
* description: userId of the selected calendar to get
* - in: path
* name: teamId
* name: externalId
* schema:
* type: integer
* type: string
* required: true
* description: Numeric teamId of the selected-calendar to get
* description: externalId of the selected calendar to get
* - in: path
* name: integration
* schema:
* type: string
* required: true
* description: integration of the selected calendar to get
* tags:
* - selected-calendars
* responses:
@@ -79,13 +91,19 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/
* schema:
* type: integer
* required: true
* description: Numeric userId of the selected-calendar to get
* description: userId of the selected calendar to get
* - in: path
* name: teamId
* name: externalId
* schema:
* type: integer
* required: true
* description: Numeric teamId of the selected-calendar to get
* description: externalId of the selected-calendar to get
* - in: path
* name: integration
* schema:
* type: string
* required: true
* description: integration of the selected calendar to get
* tags:
* - selected-calendars
* responses:
@@ -105,7 +123,7 @@ export async function selectedCalendarById(
const safeQuery = await schemaQueryIdAsString.safeParse(query);
const safeBody = await schemaSelectedCalendarBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
// This is how we set the userId and teamId in the query for managing compoundId.
// This is how we set the userId and externalId in the query for managing compoundId.
const [userId, integration, externalId] = safeQuery.data.id.split("_");
switch (method) {
+55 -18
View File
@@ -3,14 +3,18 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { SelectedCalendarsResponse } from "@lib/types";
import { schemaSelectedCalendarPublic } from "@lib/validations/selected-calendar";
import { SelectedCalendarResponse, SelectedCalendarsResponse } from "@lib/types";
import {
schemaSelectedCalendarBodyParams,
schemaSelectedCalendarPublic,
withValidSelectedCalendar,
} from "@lib/validations/selected-calendar";
/**
* @swagger
* /api/selected-calendars:
* /v1/selected-calendars:
* get:
* summary: Returns all selected calendars
* summary: Get all selected calendars
* tags:
* - selected-calendars
* responses:
@@ -19,21 +23,54 @@ import { schemaSelectedCalendarPublic } from "@lib/validations/selected-calendar
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No selectedCalendars were found
* description: No selected calendars were found
* post:
* summary: Creates a new selected calendar
* tags:
* - selected-calendars
* responses:
* 201:
* description: OK, selected calendar created
* model: SelectedCalendar
* 400:
* description: Bad request. SelectedCalendar body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function allSelectedCalendars(_: NextApiRequest, res: NextApiResponse<SelectedCalendarsResponse>) {
const selectedCalendars = await prisma.selectedCalendar.findMany();
const data = selectedCalendars.map((selectedCalendar) =>
schemaSelectedCalendarPublic.parse(selectedCalendar)
);
async function createOrlistAllSelectedCalendars(
req: NextApiRequest,
res: NextApiResponse<SelectedCalendarsResponse | SelectedCalendarResponse>
) {
const { method } = req;
if (method === "GET") {
const selectedCalendars = await prisma.selectedCalendar.findMany();
const data = selectedCalendars.map((selectedCalendar) =>
schemaSelectedCalendarPublic.parse(selectedCalendar)
);
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No SelectedCalendars were found",
error,
});
} else if (method === "POST") {
const safe = schemaSelectedCalendarBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No SelectedCalendars were found",
error,
});
const selectedCalendar = await prisma.selectedCalendar.create({ data: safe.data });
const data = schemaSelectedCalendarPublic.parse(selectedCalendar);
if (data) res.status(201).json({ data, message: "SelectedCalendar created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new selectedCalendar",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET")(allSelectedCalendars);
export default withMiddleware("HTTP_GET_OR_POST")(
withValidSelectedCalendar(createOrlistAllSelectedCalendars)
);
-45
View File
@@ -1,45 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { SelectedCalendarResponse } from "@lib/types";
import {
schemaSelectedCalendarBodyParams,
schemaSelectedCalendarPublic,
withValidSelectedCalendar,
} from "@lib/validations/selected-calendar";
/**
* @swagger
* /api/selected-calendars/new:
* post:
* summary: Creates a new selected calendar
* tags:
* - selected-calendars
* responses:
* 201:
* description: OK, selected calendar created
* model: SelectedCalendar
* 400:
* description: Bad request. SelectedCalendar body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createSelectedCalendar(req: NextApiRequest, res: NextApiResponse<SelectedCalendarResponse>) {
const safe = schemaSelectedCalendarBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const selectedCalendar = await prisma.selectedCalendar.create({ data: safe.data });
const data = schemaSelectedCalendarPublic.parse(selectedCalendar);
if (data) res.status(201).json({ data, message: "SelectedCalendar created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new selectedCalendar",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidSelectedCalendar(createSelectedCalendar));
-68
View File
@@ -1,68 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { PaymentResponse, PaymentsResponse } from "@lib/types";
import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";
/**
* @swagger
* /api/payments:
* get:
* summary: Get all payments
* tags:
* - payments
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No payments were found
* post:
* summary: Creates a new payment
* tags:
* - payments
* responses:
* 201:
* description: OK, payment created
* model: Payment
* 400:
* description: Bad request. Payment body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createOrlistAllPayments(
req: NextApiRequest,
res: NextApiResponse<PaymentsResponse | PaymentResponse>
) {
const { method } = req;
if (method === "GET") {
const payments = await prisma.payment.findMany();
const data = payments.map((payment) => schemaPaymentPublic.parse(payment));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Payments were found",
error,
});
} else if (method === "POST") {
const safe = schemaPaymentBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
const payment = await prisma.payment.create({ data: safe.data });
const data = schemaPaymentPublic.parse(payment);
if (data) res.status(201).json({ data, message: "Payment created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new payment",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments));
+3 -3
View File
@@ -12,7 +12,7 @@ import { schemaTeamBodyParams, schemaTeamPublic } from "@lib/validations/team";
/**
* @swagger
* /api/teams/{id}:
* /v1/teams/{id}:
* get:
* summary: Get a team by ID
* parameters:
@@ -81,8 +81,8 @@ import { schemaTeamBodyParams, schemaTeamPublic } from "@lib/validations/team";
*/
export async function teamById(req: NextApiRequest, res: NextApiResponse<TeamResponse>) {
const { method, query, body } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaTeamBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaTeamBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
switch (method) {
+43 -15
View File
@@ -3,14 +3,14 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { TeamsResponse } from "@lib/types";
import { schemaTeamPublic } from "@lib/validations/team";
import { TeamResponse, TeamsResponse } from "@lib/types";
import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/validations/team";
/**
* @swagger
* /api/teams:
* /v1/teams:
* get:
* summary: Returns all teams
* summary: Get all teams
* tags:
* - teams
* responses:
@@ -20,18 +20,46 @@ import { schemaTeamPublic } from "@lib/validations/team";
* description: Authorization information is missing or invalid.
* 404:
* description: No teams were found
* post:
* summary: Creates a new team
* tags:
* - teams
* responses:
* 201:
* description: OK, team created
* model: Team
* 400:
* description: Bad request. Team body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function allTeams(_: NextApiRequest, res: NextApiResponse<TeamsResponse>) {
const teams = await prisma.team.findMany();
const data = teams.map((team) => schemaTeamPublic.parse(team));
async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse<TeamsResponse | TeamResponse>) {
const { method } = req;
if (method === "GET") {
const teams = await prisma.team.findMany();
const data = teams.map((team) => schemaTeamPublic.parse(team));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Teams were found",
error,
});
} else if (method === "POST") {
const safe = schemaTeamBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body");
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Teams were found",
error,
});
const team = await prisma.team.create({ data: safe.data });
const data = schemaTeamPublic.parse(team);
if (data) res.status(201).json({ data, message: "Team created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new team",
error,
});
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET")(allTeams);
export default withMiddleware("HTTP_GET_OR_POST")(withValidTeam(createOrlistAllTeams));
-41
View File
@@ -1,41 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { TeamResponse } from "@lib/types";
import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/validations/team";
/**
* @swagger
* /api/teams/new:
* post:
* summary: Creates a new team
* tags:
* - teams
* responses:
* 201:
* description: OK, team created
* model: Team
* 400:
* description: Bad request. Team body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createTeam(req: NextApiRequest, res: NextApiResponse<TeamResponse>) {
const safe = schemaTeamBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const team = await prisma.team.create({ data: safe.data });
const data = schemaTeamPublic.parse(team);
if (data) res.status(201).json({ data, message: "Team created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new team",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidTeam(createTeam));
+1 -1
View File
@@ -9,7 +9,7 @@ import { schemaUserPublic } from "@lib/validations/user";
/**
* @swagger
* /api/users:
* /v1/users:
* get:
* summary: Get all users (admin only), returns your user if regular user.
* tags: