import { DocumentReference , FirebaseAdmin , WriteBatch } from './../client/firebase-admin' ; import { GitHubEventInput , GitHubEventMapper , GitHubEventModel } from '../mappers/github/index.mapper' ; import { GitHubClient } from './../client/github' ; import { Logger } from './../client/logger' ; export interface EventsInput { token : string ; username : string ; } export const getUserEvents = async ( token , uid , username ) => { const batch = FirebaseAdmin . firestore ( ) . batch ( ) ; const userRef = FirebaseAdmin . firestore ( ) . collection ( 'users' ) . doc ( uid ) ; let events = [ ] ; try { events = await GitHubClient < GitHubEventInput [ ] > ( `/users/${ username }/events` , token ) ; } catch ( error ) { Logger . error ( error ) ; throw new Error ( error ) ; } const mappedEvents = events . map ( ( event ) => GitHubEventMapper . import ( event ) ) ; await batch . update ( userRef , { activity : mappedEvents , } ) ; await batch . commit ( ) ; return mappedEvents ; } ;