({
query,
getSlackAccessToken = api.getSlackAccessToken,
fetchTeamInfo = api.fetchTeamInfo,
eventNewIntegration = integrationEvents.eventNewIntegration,
}: {
query: any;
getSlackAccessToken?: (
code: string,
clientId: string,
clientSecret: string
) => Promise<any>;
fetchTeamInfo?(token: string): Promise<any>;
eventNewIntegration?(event: integrationEvents.NewMessageEvent): Promise<void>;
})
| 6 | import { prisma } from '@linen/database'; |
| 7 | |
| 8 | export async function newSlackIntegration({ |
| 9 | query, |
| 10 | getSlackAccessToken = api.getSlackAccessToken, |
| 11 | fetchTeamInfo = api.fetchTeamInfo, |
| 12 | eventNewIntegration = integrationEvents.eventNewIntegration, |
| 13 | }: { |
| 14 | query: any; |
| 15 | getSlackAccessToken?: ( |
| 16 | code: string, |
| 17 | clientId: string, |
| 18 | clientSecret: string |
| 19 | ) => Promise<any>; |
| 20 | fetchTeamInfo?(token: string): Promise<any>; |
| 21 | eventNewIntegration?(event: integrationEvents.NewMessageEvent): Promise<void>; |
| 22 | }) { |
| 23 | const accountId = query.state; |
| 24 | |
| 25 | const account = await prisma.accounts.findUnique({ |
| 26 | where: { id: accountId }, |
| 27 | }); |
| 28 | |
| 29 | if (!account) { |
| 30 | return 'https://www.linen.dev/500?error=account-not-found'; |
| 31 | } |
| 32 | |
| 33 | const url = getHomeUrl(serializeAccount(account) as SerializedAccount); |
| 34 | |
| 35 | try { |
| 36 | if (query.error) { |
| 37 | throw query.error; |
| 38 | } |
| 39 | |
| 40 | const code = query.code; |
| 41 | const accountId = query.state as string; |
| 42 | const clientId = process.env.NEXT_PUBLIC_SLACK_CLIENT_ID; |
| 43 | const clientSecret = process.env.SLACK_CLIENT_SECRET; |
| 44 | |
| 45 | const resp = await getSlackAccessToken( |
| 46 | code as string, |
| 47 | clientId as string, |
| 48 | clientSecret as string |
| 49 | ); |
| 50 | |
| 51 | const body: SlackAuthorizationResponse = resp.body; |
| 52 | if (!body.ok) { |
| 53 | throw body; |
| 54 | } |
| 55 | |
| 56 | const teamInfoResponse = await fetchTeamInfo(body.access_token); |
| 57 | const communityUrl = teamInfoResponse?.body?.team?.url; |
| 58 | |
| 59 | const account = await prisma.accounts.update({ |
| 60 | where: { |
| 61 | id: accountId, |
| 62 | }, |
| 63 | data: { |
| 64 | slackTeamId: body.team.id, |
| 65 | communityUrl, |
no test coverage detected