forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.handler.ts
More file actions
177 lines (153 loc) · 5.15 KB
/
update.handler.ts
File metadata and controls
177 lines (153 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import type { Prisma } from "@prisma/client";
import { getOrgFullOrigin } from "@calcom/ee/organizations/lib/orgDomains";
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
import type { IntervalLimit } from "@calcom/lib/intervalLimits/intervalLimitSchema";
import { validateIntervalLimitOrder } from "@calcom/lib/intervalLimits/validateIntervalLimitOrder";
import { uploadLogo } from "@calcom/lib/server/avatar";
import { isTeamAdmin } from "@calcom/lib/server/queries/teams";
import { prisma } from "@calcom/prisma";
import { RedirectType, RRTimestampBasis } from "@calcom/prisma/enums";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../types";
import type { TUpdateInputSchema } from "./update.schema";
type UpdateOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TUpdateInputSchema;
};
export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
const isOrgAdmin = ctx.user?.organization?.isOrgAdmin;
if (!isOrgAdmin) {
if (!(await isTeamAdmin(ctx.user?.id, input.id))) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
}
if (input.slug) {
const userConflict = await prisma.team.findMany({
where: {
slug: input.slug,
},
});
if (userConflict.some((t) => t.id !== input.id)) return;
}
const prevTeam = await prisma.team.findUnique({
where: {
id: input.id,
},
});
if (!prevTeam) throw new TRPCError({ code: "NOT_FOUND", message: "Team not found." });
if (input.bookingLimits) {
const isValid = validateIntervalLimitOrder(input.bookingLimits);
if (!isValid)
throw new TRPCError({ code: "BAD_REQUEST", message: "Booking limits must be in ascending order." });
}
const data: Prisma.TeamUpdateArgs["data"] = {
name: input.name,
bio: input.bio,
hideBranding: input.hideBranding,
isPrivate: input.isPrivate,
hideBookATeamMember: input.hideBookATeamMember,
hideTeamProfileLink: input.hideTeamProfileLink,
brandColor: input.brandColor,
darkBrandColor: input.darkBrandColor,
theme: input.theme,
bookingLimits: input.bookingLimits ?? undefined,
includeManagedEventsInLimits: input.includeManagedEventsInLimits ?? undefined,
rrResetInterval: input.rrResetInterval,
rrTimestampBasis: input.rrTimestampBasis,
};
if (
input.logo &&
(input.logo.startsWith("data:image/png;base64,") ||
input.logo.startsWith("data:image/jpeg;base64,") ||
input.logo.startsWith("data:image/jpg;base64,"))
) {
data.logoUrl = await uploadLogo({ teamId: input.id, logo: input.logo });
} else if (typeof input.logo !== "undefined" && !input.logo) {
data.logoUrl = null;
}
if (
input.slug &&
IS_TEAM_BILLING_ENABLED &&
/** If the team doesn't have a slug we can assume that it hasn't been published yet. */
!prevTeam.slug
) {
// Save it on the metadata so we can use it later
data.metadata = {
requestedSlug: input.slug,
};
} else {
data.slug = input.slug;
// If we save slug, we don't need the requestedSlug anymore
const metadataParse = teamMetadataSchema.safeParse(prevTeam.metadata);
if (metadataParse.success) {
const { requestedSlug: _, ...cleanMetadata } = metadataParse.data || {};
data.metadata = {
...cleanMetadata,
};
}
}
const updatedTeam = await prisma.team.update({
where: { id: input.id },
data,
});
if (
data.rrTimestampBasis &&
data.rrTimestampBasis !== RRTimestampBasis.CREATED_AT &&
prevTeam.rrTimestampBasis === RRTimestampBasis.CREATED_AT
) {
// disable load balancing for all event types
await prisma.eventType.updateMany({
where: {
teamId: input.id,
},
data: {
maxLeadThreshold: null,
},
});
}
if (updatedTeam.parentId && prevTeam.slug) {
// No changes made lets skip this logic
if (updatedTeam.slug === prevTeam.slug) return;
// Fetch parent team slug to construct toUrl
const parentTeam = await prisma.team.findUnique({
where: {
id: updatedTeam.parentId,
},
select: {
slug: true,
},
});
if (!parentTeam?.slug) {
throw new Error(`Parent team with slug: ${parentTeam?.slug} not found`);
}
const orgUrlPrefix = getOrgFullOrigin(parentTeam.slug);
const toUrlOld = `${orgUrlPrefix}/${prevTeam.slug}`;
const toUrlNew = `${orgUrlPrefix}/${updatedTeam.slug}`;
await prisma.tempOrgRedirect.updateMany({
where: {
type: RedirectType.Team,
toUrl: toUrlOld,
},
data: {
toUrl: toUrlNew,
},
});
}
return {
logoUrl: updatedTeam.logoUrl,
name: updatedTeam.name,
bio: updatedTeam.bio,
slug: updatedTeam.slug,
theme: updatedTeam.theme,
brandColor: updatedTeam.brandColor,
darkBrandColor: updatedTeam.darkBrandColor,
bookingLimits: updatedTeam.bookingLimits as IntervalLimit,
includeManagedEventsInLimits: updatedTeam.includeManagedEventsInLimits,
rrResetInterval: updatedTeam.rrResetInterval,
rrTimestampBasis: updatedTeam.rrTimestampBasis,
};
};
export default updateHandler;