2023-02-16 21:01:40 +00:00
|
|
|
import type { Prisma } from "@prisma/client";
|
2022-10-11 02:25:47 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2022-06-29 22:01:14 +00:00
|
|
|
|
2022-11-22 20:24:25 +00:00
|
|
|
import { purchaseTeamSubscription } from "@calcom/features/ee/teams/lib/payments";
|
|
|
|
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
|
2022-06-29 22:01:14 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
2022-11-22 20:24:25 +00:00
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
2022-11-25 13:56:58 +00:00
|
|
|
import { schemaQueryTeamId } from "~/lib/validations/shared/queryTeamId";
|
|
|
|
import { schemaTeamReadPublic, schemaTeamUpdateBodyParams } from "~/lib/validations/team";
|
2022-11-22 20:24:25 +00:00
|
|
|
|
2022-06-29 22:01:14 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /teams/{teamId}:
|
|
|
|
* patch:
|
|
|
|
* operationId: editTeamById
|
|
|
|
* summary: Edit an existing team
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: teamId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: ID of the team to edit
|
2023-03-10 17:04:14 +00:00
|
|
|
* - in: query
|
|
|
|
* name: apiKey
|
|
|
|
* required: true
|
|
|
|
* schema:
|
|
|
|
* type: string
|
|
|
|
* description: Your API key
|
|
|
|
* requestBody:
|
|
|
|
* description: Create a new custom input for an event type
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* name:
|
|
|
|
* type: string
|
|
|
|
* description: Name of the team
|
|
|
|
* slug:
|
|
|
|
* type: string
|
|
|
|
* description: A unique slug that works as path for the team public page
|
2022-06-29 22:01:14 +00:00
|
|
|
* tags:
|
|
|
|
* - teams
|
|
|
|
* responses:
|
|
|
|
* 201:
|
2022-10-11 02:25:47 +00:00
|
|
|
* description: OK, team edited successfully
|
2022-06-29 22:01:14 +00:00
|
|
|
* 400:
|
|
|
|
* description: Bad request. Team body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-10-11 02:25:47 +00:00
|
|
|
export async function patchHandler(req: NextApiRequest) {
|
|
|
|
const { prisma, body, userId } = req;
|
|
|
|
const data = schemaTeamUpdateBodyParams.parse(body);
|
|
|
|
const { teamId } = schemaQueryTeamId.parse(req.query);
|
|
|
|
/** Only OWNERS and ADMINS can edit teams */
|
|
|
|
const _team = await prisma.team.findFirst({
|
2022-11-22 20:24:25 +00:00
|
|
|
include: { members: true },
|
2022-10-11 02:25:47 +00:00
|
|
|
where: { id: teamId, members: { some: { userId, role: { in: ["OWNER", "ADMIN"] } } } },
|
2022-06-29 22:01:14 +00:00
|
|
|
});
|
2022-10-11 02:25:47 +00:00
|
|
|
if (!_team) throw new HttpError({ statusCode: 401, message: "Unauthorized: OWNER or ADMIN required" });
|
2022-11-22 20:24:25 +00:00
|
|
|
let paymentUrl;
|
|
|
|
if (_team.slug === null && data.slug) {
|
|
|
|
data.metadata = {
|
|
|
|
...(_team.metadata as Prisma.JsonObject),
|
|
|
|
requestedSlug: data.slug,
|
|
|
|
};
|
|
|
|
delete data.slug;
|
|
|
|
if (IS_TEAM_BILLING_ENABLED) {
|
|
|
|
const checkoutSession = await purchaseTeamSubscription({
|
|
|
|
teamId: _team.id,
|
|
|
|
seats: _team.members.length,
|
|
|
|
userId,
|
|
|
|
});
|
|
|
|
if (!checkoutSession.url)
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
|
|
message: "Failed retrieving a checkout session URL.",
|
|
|
|
});
|
|
|
|
paymentUrl = checkoutSession.url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 11:38:49 +00:00
|
|
|
// TODO: Perhaps there is a better fix for this?
|
|
|
|
const cloneData: typeof data & {
|
|
|
|
metadata: NonNullable<typeof data.metadata> | undefined;
|
|
|
|
} = {
|
|
|
|
...data,
|
|
|
|
metadata: data.metadata === null ? {} : data.metadata || undefined,
|
|
|
|
};
|
|
|
|
const team = await prisma.team.update({ where: { id: teamId }, data: cloneData });
|
2022-11-22 20:24:25 +00:00
|
|
|
const result = {
|
|
|
|
team: schemaTeamReadPublic.parse(team),
|
|
|
|
paymentUrl,
|
|
|
|
};
|
|
|
|
if (!paymentUrl) {
|
|
|
|
delete result.paymentUrl;
|
|
|
|
}
|
|
|
|
return result;
|
2022-06-29 22:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(patchHandler);
|