1+ import pLimit from 'p-limit' ;
2+
13import { Member } from '../entities/Member' ;
24
35export const implementMemberSlackRepository = ( {
@@ -14,30 +16,30 @@ export const implementMemberSlackRepository = ({
1416
1517 if ( ! users . ok ) throw new Error ( 'Failed to fetch users' ) ;
1618
19+ const limit = pLimit ( 10 ) ;
20+
21+ const fetchMemberProfile = async ( member : { id : string } ) => {
22+ const profile = await fetch ( `https://slack.com/api/users.profile.get?user=${ member . id } ` , {
23+ method : 'GET' ,
24+ headers : { 'Content-Type' : 'application/json' , Authorization : `Bearer ${ slackAuthToken } ` } ,
25+ } ) . then (
26+ ( res ) =>
27+ res . json ( ) as Promise <
28+ { ok : true ; profile : { fields : { [ githubField ] ?: { value : string } } } } | { ok : false }
29+ > ,
30+ ) ;
31+
32+ if ( ! profile . ok ) throw new Error ( 'Failed to fetch profile: ' + JSON . stringify ( profile ) ) ;
33+
34+ const githubUsername = profile . profile . fields [ githubField ] ?. value ;
35+
36+ if ( ! githubUsername ) return [ ] ;
37+
38+ return [ { githubUsername : githubUsername . replace ( 'https://github.com/' , '' ) , slackUserId : member . id } ] ;
39+ } ;
40+
1741 return {
18- members : (
19- await Promise . all (
20- users . members . flatMap ( async ( member ) => {
21- const profile = await fetch ( `https://slack.com/api/users.profile.get?user=${ member . id } ` , {
22- method : 'GET' ,
23- headers : { 'Content-Type' : 'application/json' , Authorization : `Bearer ${ slackAuthToken } ` } ,
24- } ) . then (
25- ( res ) =>
26- res . json ( ) as Promise <
27- { ok : true ; profile : { fields : { [ githubField ] ?: { value : string } } } } | { ok : false }
28- > ,
29- ) ;
30-
31- if ( ! profile . ok ) throw new Error ( 'Failed to fetch profile: ' + JSON . stringify ( profile ) ) ;
32-
33- const githubUsername = profile . profile . fields [ githubField ] ?. value ;
34-
35- if ( ! githubUsername ) return [ ] ;
36-
37- return [ { githubUsername : githubUsername . replace ( 'https://github.com/' , '' ) , slackUserId : member . id } ] ;
38- } ) ,
39- )
40- ) . flat ( ) ,
42+ members : ( await Promise . all ( users . members . map ( ( member ) => limit ( ( ) => fetchMemberProfile ( member ) ) ) ) ) . flat ( ) ,
4143 } ;
4244 } ,
4345 } ;
0 commit comments