MCPcopy Create free account
hub / github.com/sourcebot-dev/sourcebot / joinOrganization

Function joinOrganization

packages/web/src/app/invite/actions.ts:16–72  ·  view source on GitHub ↗
(inviteLinkId?: string)

Source from the content-addressed store, hash-verified

14
15// eslint-disable-next-line authz/require-auth-wrapper -- runs pre-org-membership; uses getAuthenticatedUser() directly since withAuth requires a user-to-org link this call is establishing
16export const joinOrganization = async (inviteLinkId?: string) => sew(async () => {
17 const authResult = await getAuthenticatedUser();
18 if (!authResult) {
19 return notAuthenticated();
20 }
21
22 const { user } = authResult;
23
24 const org = await __unsafePrisma.org.findUnique({
25 where: {
26 id: SINGLE_TENANT_ORG_ID,
27 },
28 });
29
30 if (!org) {
31 return orgNotFound();
32 }
33
34
35 // If member approval is required we must be using a valid invite link
36 if (isMemberApprovalRequired(org)) {
37 if (!org.inviteLinkEnabled) {
38 return {
39 statusCode: StatusCodes.BAD_REQUEST,
40 errorCode: ErrorCode.INVITE_LINK_NOT_ENABLED,
41 message: "Invite link is not enabled.",
42 } satisfies ServiceError;
43 }
44
45 if (org.inviteLinkId !== inviteLinkId) {
46 return {
47 statusCode: StatusCodes.BAD_REQUEST,
48 errorCode: ErrorCode.INVALID_INVITE_LINK,
49 message: "Invalid invite link.",
50 } satisfies ServiceError;
51 }
52 }
53
54 const addUserToOrgRes = await addUserToOrganization(user.id, org.id);
55 if (isServiceError(addUserToOrgRes)) {
56 return addUserToOrgRes;
57 }
58
59 await createAudit({
60 action: "org.member_added",
61 actor: { id: user.id, type: "user" },
62 target: { id: user.id, type: "user" },
63 orgId: org.id,
64 metadata: {
65 message: `${user.id} joined the organization via invite link`,
66 },
67 });
68
69 return {
70 success: true,
71 }
72});
73

Callers 1

handleJoinOrganizationFunction · 0.90

Calls 8

sewFunction · 0.90
getAuthenticatedUserFunction · 0.90
notAuthenticatedFunction · 0.90
orgNotFoundFunction · 0.90
isMemberApprovalRequiredFunction · 0.90
addUserToOrganizationFunction · 0.90
isServiceErrorFunction · 0.90
createAuditFunction · 0.90

Tested by

no test coverage detected