import * as admin from 'firebase-admin' ; import { firestore , Change , EventContext } from 'firebase-functions' ; import { DocumentData , DocumentSnapshot , FirebaseAdmin } from './../client/firebase-admin' ; import { Logger } from './../client/logger' ; import { GitHubUserStatsModel } from './../mappers/github/user.mapper' ; export const onUpdateUserStats = firestore . document ( 'users/{userId}' ) . onWrite ( ( change , context ) => { const user = change . after . data ( ) ; if ( ! user ) { return null ; } const data = { name : user . name , username : user . username , avatarUrl : user . avatarUrl , github : { repository : { total : user . repositories && user . repositories . data ? user . repositories . data . length : 0 , } , activity : { latest : user . activity ? user . activity [ 0 ] : { } , } , } , lastUpdated : admin . firestore . Timestamp . fromDate ( new Date ( ) ) , } ; Logger . info ( { user : user . username , imported : { } , } ) ; return FirebaseAdmin . firestore ( ) . collection ( 'userStats' ) . doc ( user . uid ) . set ( data , { merge : true } ) ; } ) ;