import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import { switchMap , take } from 'rxjs/operators' ; import { AngularFirestore } from '@angular/fire/firestore' ; import * as firebase from 'firebase' ; import { UserModel , UserStatsModel } from '@shared/models/index.model' ; import { ActivityService } from './activity.service' ; import { AuthenticationService } from './authentication.service' ; /**
 * User service
 */ @ Injectable ( { providedIn : 'root' , } ) export class UserService { param afs param activityService constructor ( private afs , private activityService , private authService ) { } /**
     * Find all the user stats information
     */ public findAllUserStats ( ) { return this . activityService . start ( ) . pipe ( switchMap ( ( ) => this . afs . collection < UserStatsModel > ( 'userStats' , ( ref ) => ref . orderBy ( 'lastUpdated' , 'desc' ) . limit ( 4 ) ) . valueChanges ( ) ) ) ; } /**
     * Find all the user information
     */ public findAllUserList ( ) { return this . activityService . start ( ) . pipe ( switchMap ( ( ) => this . afs . collection < UserModel > ( 'users' , ( ref ) => ref . orderBy ( 'lastSignInTime' , 'desc' ) ) . valueChanges ( ) ) ) ; } param userId public findUserStatsById ( userId ) { return this . activityService . start ( ) . pipe ( switchMap ( ( ) => this . afs . doc < UserModel > ( `userStats/${ userId }` ) . valueChanges ( ) ) ) ; } param projectUid param isUpdate public updateFollowing ( projectUid , isUpdate ) { return this . activityService . start ( ) . pipe ( switchMap ( ( ) => this . afs . collection < UserModel > ( 'users' ) . doc < any > ( this . authService . profile . uid ) . set ( { following : isUpdate ? firebase . firestore . FieldValue . arrayUnion ( projectUid ) : firebase . firestore . FieldValue . arrayRemove ( projectUid ) , } , { merge : true } ) ) , take ( 1 ) ) ; } }