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

Function newSlackIntegration

apps/web/services/accounts/slack.ts:8–108  ·  view source on GitHub ↗
({
  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>;
})

Source from the content-addressed store, hash-verified

6import { prisma } from '@linen/database';
7
8export 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,

Callers 2

handlerFunction · 0.90
slack.test.tsFile · 0.90

Calls 8

getHomeUrlFunction · 0.90
serializeAccountFunction · 0.90
getSlackAccessTokenFunction · 0.70
fetchTeamInfoFunction · 0.70
eventNewIntegrationFunction · 0.70
errorMethod · 0.65
updateMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected