(inviteLinkId?: string)
| 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 |
| 16 | export 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 |
no test coverage detected