forked from calcom/cal.diy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveMember.handler.ts
More file actions
77 lines (65 loc) · 1.88 KB
/
removeMember.handler.ts
File metadata and controls
77 lines (65 loc) · 1.88 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
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
import { TRPCError } from "@trpc/server";
import type { TRemoveMemberInputSchema } from "./removeMember.schema";
import { RemoveMemberServiceFactory } from "./removeMember/RemoveMemberServiceFactory";
type RemoveMemberOptions = {
ctx: {
user: {
id: number;
organizationId: number | null;
organization?: {
id: number | null;
isOrgAdmin: boolean;
};
};
};
input: TRemoveMemberInputSchema;
};
export const removeMemberHandler = async ({
ctx: {
user: { id: userId, organizationId, organization },
},
input,
}: RemoveMemberOptions): Promise<void> => {
await checkRateLimitAndThrowError({
identifier: `removeMember.${userId}`,
});
const { memberIds, teamIds, isOrg } = input;
const isOrgAdmin = organization?.isOrgAdmin ?? false;
const userOrgId = organizationId ?? organization?.id ?? null;
// Note: This assumes that all teams in the request have the same PBAC setting 9999% chance they do.
const primaryTeamId = teamIds[0];
if (!primaryTeamId) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "At least one team ID must be provided",
});
}
// Get the appropriate service based on feature flag
const service = await RemoveMemberServiceFactory.create(primaryTeamId);
const { hasPermission } = await service.checkRemovePermissions({
userId,
isOrgAdmin,
organizationId: userOrgId,
memberIds,
teamIds,
isOrg,
});
if (!hasPermission) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
await service.validateRemoval(
{
userId,
isOrgAdmin,
organizationId: userOrgId,
memberIds,
teamIds,
isOrg,
},
hasPermission
);
// Perform the removal
await service.removeMembers(memberIds, teamIds, isOrg);
};
export default removeMemberHandler;