-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add number of users in Slack counter
- Loading branch information
1 parent
24b3fe9
commit 3e8d38d
Showing
3 changed files
with
109 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
const SLACK_BOT_TOKEN = process.env.SLACK_BOT_TOKEN; | ||
|
||
if (!SLACK_BOT_TOKEN) { | ||
throw new Error('SLACK_BOT_TOKEN is not defined'); | ||
} | ||
|
||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
export async function GET(request: NextRequest) { | ||
try { | ||
let userCount = 0; | ||
let cursor = ''; | ||
|
||
do { | ||
const fetchResponse = await fetch(`https://slack.com/api/users.list?cursor=${cursor}`, { | ||
method: 'GET', | ||
headers: { | ||
'Authorization': `Bearer ${SLACK_BOT_TOKEN}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
const data = await fetchResponse.json(); | ||
|
||
if (!data.ok) { | ||
return NextResponse.json({ error: 'Failed to fetch users from Slack' }, { status: 500 }); | ||
} | ||
|
||
interface SlackUser { | ||
id: string; | ||
team_id: string; | ||
name: string; | ||
deleted: boolean; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[key: string]: any; | ||
} | ||
|
||
interface SlackResponseMetadata { | ||
next_cursor: string; | ||
} | ||
|
||
interface SlackUsersListResponse { | ||
ok: boolean; | ||
members: SlackUser[]; | ||
response_metadata?: SlackResponseMetadata; | ||
} | ||
|
||
userCount += (data as SlackUsersListResponse).members.filter((member: SlackUser) => !member.deleted).length; | ||
cursor = data.response_metadata?.next_cursor || ''; | ||
} while (cursor); | ||
|
||
return NextResponse.json({ userCount }, { status: 200 }); | ||
} catch (error) { | ||
console.error(error); | ||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export async function getUserCount(): Promise<number> { | ||
const getHost = (): string => { | ||
if (typeof window !== 'undefined') { | ||
return `${window.location.protocol}://${window.location.hostname}`; | ||
} | ||
return process.env.NEXT_PUBLIC_URL || 'http://localhost:3000'; | ||
}; | ||
|
||
const fetchUserCount = async (host: string): Promise<number> => { | ||
try { | ||
const response = await fetch(`${host}/api/slack/userCount`); | ||
if (!response.ok) { | ||
console.error('Failed to fetch user count:', response.statusText); | ||
return 0; | ||
} | ||
const data = await response.json(); | ||
if (data.error) { | ||
console.error('Error in response data:', data.error); | ||
return 0; | ||
} | ||
return data.userCount; | ||
} catch (error) { | ||
console.error('Error fetching user count:', error); | ||
return 0; | ||
} | ||
}; | ||
|
||
const host = getHost(); | ||
return fetchUserCount(host); | ||
} |