MCPcopy Create free account
hub / github.com/Linen-dev/linen.dev / handler

Function handler

apps/web/pages/api/invites/index.ts:21–84  ·  view source on GitHub ↗
(
  request: NextApiRequest,
  response: NextApiResponse
)

Source from the content-addressed store, hash-verified

19};
20
21export 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,

Callers

nothing calls this directly

Calls 8

preflightFunction · 0.90
corsFunction · 0.90
addHttpsToUrlFunction · 0.90
getHostFromHeadersFunction · 0.90
createInvitationFunction · 0.90
updateInvitationFunction · 0.90
mapMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected