(id: string, email: string)
| 152 | } |
| 153 | |
| 154 | export async function acceptInvite(id: string, email: string) { |
| 155 | const invite = await prisma.invites.findUnique({ where: { id } }); |
| 156 | if (invite?.email !== email) { |
| 157 | throw { error: 'bad request, email mismatch' }; |
| 158 | } |
| 159 | const auth = await prisma.auths.findUnique({ where: { email } }); |
| 160 | if (!auth) { |
| 161 | throw { error: 'bad request, missing signup' }; |
| 162 | } |
| 163 | const user = await prisma.users.findFirst({ |
| 164 | include: { account: true }, |
| 165 | where: { accountsId: invite.accountsId, authsId: auth.id }, |
| 166 | }); |
| 167 | if (user) { |
| 168 | return user; |
| 169 | } |
| 170 | const displayName = email.split('@').shift() || email; |
| 171 | |
| 172 | const newUser = await prisma.users.create({ |
| 173 | include: { account: true }, |
| 174 | data: { |
| 175 | isAdmin: invite.role === Roles.ADMIN, |
| 176 | isBot: false, |
| 177 | account: { connect: { id: invite.accountsId } }, |
| 178 | auth: { connect: { id: auth.id } }, |
| 179 | anonymousAlias: generateRandomWordSlug(), |
| 180 | displayName, |
| 181 | role: invite.role, |
| 182 | }, |
| 183 | }); |
| 184 | // this step will be deprecated soon |
| 185 | await prisma.auths.update({ |
| 186 | where: { email }, |
| 187 | data: { account: { connect: { id: invite.accountsId } } }, |
| 188 | }); |
| 189 | |
| 190 | await prisma.invites.update({ where: { id }, data: { status: 'ACCEPTED' } }); |
| 191 | |
| 192 | await eventUserJoin({ userId: newUser.id }); |
| 193 | return newUser; |
| 194 | } |
| 195 | |
| 196 | export async function updateInvitation({ |
| 197 | inviteId, |
no test coverage detected