-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathactions.ts
More file actions
205 lines (174 loc) · 5.49 KB
/
Copy pathactions.ts
File metadata and controls
205 lines (174 loc) · 5.49 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use server";
import { isServiceError } from "@/lib/utils";
import { notAuthenticated, notFound, orgNotFound, ServiceError } from "@/lib/serviceError";
import { sew } from "@/middleware/sew";
import { addUserToOrganization, orgHasAvailability } from "@/lib/authUtils";
import { StatusCodes } from "http-status-codes";
import { ErrorCode } from "@/lib/errorCodes";
import { getAuthenticatedUser } from "@/middleware/withAuth";
import { __unsafePrisma } from "@/prisma";
import { SINGLE_TENANT_ORG_ID } from "@/lib/constants";
import { getAuditService } from "@/ee/features/audit/factory";
const auditService = getAuditService();
export const joinOrganization = async (inviteLinkId?: string) => sew(async () => {
const authResult = await getAuthenticatedUser();
if (!authResult) {
return notAuthenticated();
}
const { user } = authResult;
const org = await __unsafePrisma.org.findUnique({
where: {
id: SINGLE_TENANT_ORG_ID,
},
});
if (!org) {
return orgNotFound();
}
// If member approval is required we must be using a valid invite link
if (org.memberApprovalRequired) {
if (!org.inviteLinkEnabled) {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.INVITE_LINK_NOT_ENABLED,
message: "Invite link is not enabled.",
} satisfies ServiceError;
}
if (org.inviteLinkId !== inviteLinkId) {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.INVALID_INVITE_LINK,
message: "Invalid invite link.",
} satisfies ServiceError;
}
}
const addUserToOrgRes = await addUserToOrganization(user.id, org.id);
if (isServiceError(addUserToOrgRes)) {
return addUserToOrgRes;
}
await auditService.createAudit({
action: "org.member_added",
actor: { id: user.id, type: "user" },
target: { id: user.id, type: "user" },
orgId: org.id,
metadata: {
message: `${user.id} joined the organization via invite link`,
},
});
return {
success: true,
}
});
export const redeemInvite = async (inviteId: string): Promise<{ success: boolean; } | ServiceError> => sew(async () => {
const authResult = await getAuthenticatedUser();
if (!authResult) {
return notAuthenticated();
}
const { user } = authResult;
const invite = await __unsafePrisma.invite.findUnique({
where: {
id: inviteId,
},
include: {
org: true,
}
});
if (!invite) {
return notFound();
}
const failAuditCallback = async (error: string) => {
await auditService.createAudit({
action: "user.invite_accept_failed",
actor: {
id: user.id,
type: "user"
},
target: {
id: inviteId,
type: "invite"
},
orgId: invite.org.id,
metadata: {
message: error
}
});
};
const hasAvailability = await orgHasAvailability();
if (!hasAvailability) {
await failAuditCallback("Organization is at max capacity");
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.ORG_SEAT_COUNT_REACHED,
message: "Organization is at max capacity",
} satisfies ServiceError;
}
// Check if the user is the recipient of the invite
if (user.email !== invite.recipientEmail) {
await failAuditCallback("User is not the recipient of the invite");
return notFound();
}
const addUserToOrgRes = await addUserToOrganization(user.id, invite.orgId);
if (isServiceError(addUserToOrgRes)) {
await failAuditCallback(addUserToOrgRes.message);
return addUserToOrgRes;
}
await auditService.createAudit({
action: "user.invite_accepted",
actor: {
id: user.id,
type: "user"
},
orgId: invite.org.id,
target: {
id: inviteId,
type: "invite"
}
});
await auditService.createAudit({
action: "org.member_added",
actor: { id: user.id, type: "user" },
target: { id: user.id, type: "user" },
orgId: invite.org.id,
metadata: {
message: `${user.id} joined the organization by accepting invite ${inviteId}`,
},
});
return {
success: true,
};
});
export const getInviteInfo = async (inviteId: string) => sew(async () => {
const authResult = await getAuthenticatedUser();
if (!authResult) {
return notAuthenticated();
}
const { user } = authResult;
const invite = await __unsafePrisma.invite.findUnique({
where: {
id: inviteId,
},
include: {
org: true,
host: true,
}
});
if (!invite) {
return notFound();
}
if (invite.recipientEmail !== user.email) {
return notFound();
}
return {
id: invite.id,
orgName: invite.org.name,
orgImageUrl: invite.org.imageUrl ?? undefined,
host: {
name: invite.host.name ?? undefined,
email: invite.host.email!,
avatarUrl: invite.host.image ?? undefined,
},
recipient: {
name: user.name ?? undefined,
email: user.email!,
}
};
});