Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updates slack user service 🚚 #20

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/core/src/modules/slack/services/slack-user.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { slack } from '@/modules/slack/instances';
import { ENV } from '@/shared/env';
import { Member } from '@slack/web-api/dist/response/UsersListResponse';
import { Profile } from '@slack/web-api/dist/response/UsersProfileGetResponse';

/**
* @see https://api.slack.com/methods/users.lookupByEmail
Expand Down Expand Up @@ -35,3 +37,53 @@ export async function updateSlackEmail(id: string, email: string) {
user: id,
});
}
/**
* @see https://api.slack.com/methods/users.list
*/
export async function getAllSlackUsers() {
let allUsers: Member[] = [];
let cursor; // Slack uses cursor-based pagination.

try {
// Continue calling the API until there's no more pages (cursor is empty).
do {
const response = await slack.users.list({ cursor: cursor });
const users: Member[] | undefined = response.members;

if (!users) {
break;
}

allUsers.push(...users);

// Slack recommends waiting for a short period of time before next pagination request
await new Promise((resolve) => setTimeout(resolve, 200));

// Update cursor to the next cursor value, if any.
cursor = response.response_metadata?.next_cursor;
} while (cursor);

return allUsers;
} catch (e) {
console.error('Failed to fetch all Slack users:', e);
return null;
}
}

export async function getUserProfile(user: Member) {
let userId = user.id;
try {
// Continue calling the API until there's no more pages (cursor is empty).
const response = await slack.users.profile.get({ id: userId });
const userProfile: Profile | undefined = response.profile;

return userProfile;
} catch (e) {
console.error(
'Failed to fetch all User Profile for userId= %d:',
userId,
e
);
return null;
}
}