import * as firebase from 'firebase-admin' ; import { DocumentReference , FirebaseAdmin , WriteBatch } from './../client/firebase-admin' ; import { GitHubClient } from './../client/github' ; import { Logger } from './../client/logger' ; import { GitHubRepositoryInput , GitHubRepositoryMapper , GitHubRepositoryModel } from './../mappers/github/repository.mapper' ; export interface ReposInput { token : string ; } export const getUserRepos = async ( token , uid ) => { const batch = FirebaseAdmin . firestore ( ) . batch ( ) ; const userRef = FirebaseAdmin . firestore ( ) . collection ( 'users' ) . doc ( uid ) ; let repositories = [ ] ; try { repositories = await GitHubClient < GitHubRepositoryInput [ ] > ( '/user/repos?visibility=public&affiliation=owner&sort=updated&per_page=100' , token ) ; } catch ( error ) { Logger . error ( error ) ; throw new Error ( error ) ; } const mappedRepos = repositories . map ( ( repository ) => GitHubRepositoryMapper . import ( repository ) ) ; Logger . info ( { user : uid , imported : { repos : mappedRepos . length || 0 , } , } ) ; await batch . update ( userRef , { repositories : { lastUpdated : firebase . firestore . Timestamp . fromDate ( new Date ( ) ) , data : mappedRepos , } , } ) ; await batch . commit ( ) ; return mappedRepos ; } ;