( request: NextApiRequest, response: NextApiResponse )
| 19 | }; |
| 20 | |
| 21 | export default async function handler( |
| 22 | request: NextApiRequest, |
| 23 | response: NextApiResponse |
| 24 | ) { |
| 25 | if (request.method === 'OPTIONS') { |
| 26 | return preflight(request, response, ['POST', 'PUT']); |
| 27 | } |
| 28 | cors(request, response); |
| 29 | |
| 30 | const body = request.body; |
| 31 | const permissions = await PermissionsService.get({ |
| 32 | request, |
| 33 | response, |
| 34 | params: { |
| 35 | communityId: body.communityId, |
| 36 | }, |
| 37 | }); |
| 38 | if (!permissions.manage) { |
| 39 | return response.status(401).json({}); |
| 40 | } |
| 41 | |
| 42 | const host = addHttpsToUrl(getHostFromHeaders(request.headers)); |
| 43 | |
| 44 | if (request.method === 'POST') { |
| 45 | const { communityId, email, role }: PostProps = body; |
| 46 | const emails = email.split(',').filter(Boolean); |
| 47 | |
| 48 | if (emails.length === 0) { |
| 49 | return response.status(400).json({ error: 'Email is invalid' }); |
| 50 | } |
| 51 | for (let i = 0, ilen = emails.length; i < ilen; i += 1) { |
| 52 | const temp = emails[i]; |
| 53 | if (!temp.includes('@') || !temp.includes('.')) { |
| 54 | return response.status(400).json({ error: 'Email is invalid' }); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | await Promise.all( |
| 59 | emails.map((email) => { |
| 60 | return createInvitation({ |
| 61 | accountId: communityId, |
| 62 | email, |
| 63 | host, |
| 64 | createdByUserId: permissions.user?.id!, |
| 65 | role, |
| 66 | }); |
| 67 | }) |
| 68 | ); |
| 69 | return response |
| 70 | .status(200) |
| 71 | .json({ message: `Invitation${emails.length === 1 ? '' : 's'} sent.` }); |
| 72 | } |
| 73 | if (request.method === 'PUT') { |
| 74 | const { communityId, role, inviteId }: PutProps = body; |
| 75 | const { status, message } = await updateInvitation({ |
| 76 | accountId: communityId, |
| 77 | inviteId, |
| 78 | role, |
nothing calls this directly
no test coverage detected