( request: NextApiRequest, response: NextApiResponse )
| 59 | } |
| 60 | |
| 61 | export default async function handler( |
| 62 | request: NextApiRequest, |
| 63 | response: NextApiResponse |
| 64 | ) { |
| 65 | if (request.method === 'OPTIONS') { |
| 66 | return preflight(request, response, ['GET']); |
| 67 | } |
| 68 | cors(request, response); |
| 69 | |
| 70 | const schema = z.object({ |
| 71 | community: z.enum(['discord', 'slack']), |
| 72 | accountId: z.string().uuid(), |
| 73 | syncOpt: z.enum(['since-all', 'since-date']).optional(), |
| 74 | dateFrom: z.string().optional(), |
| 75 | }); |
| 76 | |
| 77 | const body = schema.parse(request.query); |
| 78 | |
| 79 | const permissions = await PermissionsService.get({ |
| 80 | request, |
| 81 | response, |
| 82 | params: { |
| 83 | communityId: body.accountId, |
| 84 | }, |
| 85 | }); |
| 86 | if (!permissions.manage) { |
| 87 | throw new Error('Unauthorized'); |
| 88 | } |
| 89 | |
| 90 | if (body.syncOpt === 'since-date' && body.dateFrom) { |
| 91 | const syncFrom = new Date(body.dateFrom); |
| 92 | |
| 93 | if (body.community === 'slack') { |
| 94 | await handleSlack({ accountId: body.accountId, syncFrom }); |
| 95 | } |
| 96 | if (body.community === 'discord') { |
| 97 | await handleDiscord({ accountId: body.accountId, syncFrom }); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return response.json({ |
| 102 | url: integrationAuthorizer(body.community, body.accountId), |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | async function handleSlack({ |
| 107 | accountId, |
nothing calls this directly
no test coverage detected