(request: NextApiRequest, response: NextApiResponse)
| 8 | import { acceptInvite, findInvitesByEmail } from 'services/invites'; |
| 9 | |
| 10 | async function handler(request: NextApiRequest, response: NextApiResponse) { |
| 11 | try { |
| 12 | const session = await Session.find(request, response); |
| 13 | if (!session?.user?.email) { |
| 14 | throw 'missing session'; |
| 15 | } |
| 16 | |
| 17 | // accept invites |
| 18 | const invites = await findInvitesByEmail(session.user.email); |
| 19 | for (const invite of invites) { |
| 20 | await acceptInvite(invite.id, session.user.email).catch(console.error); |
| 21 | } |
| 22 | |
| 23 | const auth = await findAuthByEmail(session.user.email); |
| 24 | if (!auth) { |
| 25 | throw 'missing auth'; |
| 26 | } |
| 27 | |
| 28 | if (!auth?.users?.length) { |
| 29 | if (process.env.NODE_ENV === 'development') { |
| 30 | return response.redirect('http://localhost:3000/getting-started'); |
| 31 | } |
| 32 | return response.redirect('https://linen.dev/getting-started'); |
| 33 | } |
| 34 | |
| 35 | // these only works if the user browse the community before sign-in |
| 36 | const { communityId, channelId, page } = request.query as { |
| 37 | communityId: string; |
| 38 | channelId?: string; |
| 39 | page: string; |
| 40 | }; |
| 41 | |
| 42 | const user = auth.users.find( |
| 43 | (u) => u.accountsId === (communityId || auth.accountId) |
| 44 | ); |
| 45 | |
| 46 | if (!user?.account) { |
| 47 | return response.redirect('/'); |
| 48 | } |
| 49 | |
| 50 | const account = serializeAccount(user.account); |
| 51 | |
| 52 | const url = getHomeUrl(account); |
| 53 | |
| 54 | if (account.id === communityId) { |
| 55 | if (channelId) { |
| 56 | const channel = await prisma.channels.findFirst({ |
| 57 | where: { |
| 58 | id: channelId, |
| 59 | accountId: account.id, |
| 60 | }, |
| 61 | }); |
| 62 | if (channel) { |
| 63 | return response.redirect(`${url}/c/${channel.channelName}`); |
| 64 | } |
| 65 | } |
| 66 | if (page === 'inbox' || page === 'metrics') { |
| 67 | return response.redirect(`${url}/${page}`); |
nothing calls this directly
no test coverage detected