(inviteId: string)
| 73 | |
| 74 | // 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 |
| 75 | export const redeemInvite = async (inviteId: string): Promise<{ success: boolean; } | ServiceError> => sew(async () => { |
| 76 | const authResult = await getAuthenticatedUser(); |
| 77 | if (!authResult) { |
| 78 | return notAuthenticated(); |
| 79 | } |
| 80 | |
| 81 | const { user } = authResult; |
| 82 | |
| 83 | const invite = await __unsafePrisma.invite.findUnique({ |
| 84 | where: { |
| 85 | id: inviteId, |
| 86 | }, |
| 87 | include: { |
| 88 | org: true, |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | if (!invite) { |
| 93 | return notFound(); |
| 94 | } |
| 95 | |
| 96 | const failAuditCallback = async (error: string) => { |
| 97 | await createAudit({ |
| 98 | action: "user.invite_accept_failed", |
| 99 | actor: { |
| 100 | id: user.id, |
| 101 | type: "user" |
| 102 | }, |
| 103 | target: { |
| 104 | id: inviteId, |
| 105 | type: "invite" |
| 106 | }, |
| 107 | orgId: invite.org.id, |
| 108 | metadata: { |
| 109 | message: error |
| 110 | } |
| 111 | }); |
| 112 | }; |
| 113 | |
| 114 | const hasAvailability = await orgHasAvailability(invite.org.id); |
| 115 | if (!hasAvailability) { |
| 116 | await failAuditCallback("Organization is at max capacity"); |
| 117 | return { |
| 118 | statusCode: StatusCodes.BAD_REQUEST, |
| 119 | errorCode: ErrorCode.ORG_SEAT_COUNT_REACHED, |
| 120 | message: "Organization is at max capacity", |
| 121 | } satisfies ServiceError; |
| 122 | } |
| 123 | |
| 124 | // Check if the user is the recipient of the invite |
| 125 | if (user.email !== invite.recipientEmail) { |
| 126 | await failAuditCallback("User is not the recipient of the invite"); |
| 127 | return notFound(); |
| 128 | } |
| 129 | |
| 130 | const addUserToOrgRes = await addUserToOrganization(user.id, invite.orgId); |
| 131 | if (isServiceError(addUserToOrgRes)) { |
| 132 | await failAuditCallback(addUserToOrgRes.message); |
no test coverage detected