import { Injectable } from '@angular/core' ; import { Actions , Effect , ofType } from '@ngrx/effects' ; import * as fromAdmin from './../store/admin.actions' ; import { switchMap , map , catchError , mergeMap } from 'rxjs/operators' ; import { AdminService } from '../services/admin.service' ; import { Project } from '../../projects/models/project.model' ; import { of } from 'rxjs' ; import { Customer } from '../../customers/models/customer.model' ; @ Injectable ( ) export class AdminEffects { constructor ( private actions$ , private adminService ) { } @ Effect ( ) getUsersList$ = this . actions$ . pipe ( ofType ( fromAdmin . AdminActionTypes . GET_USERS_LIST ) , switchMap ( ( ) => this . adminService . getUsersList ( ) . pipe ( map ( ( users ) => { const usersList = users . map ( ( res ) => { const key = res . payload . key ; const user = res . payload . val ( ) ; return { key : key , uid : user . uid , displayName : user . displayName , email : user . email , providerId : user . providerId , photoUrl : user . photoUrl , isNewUser : user . isNewUser , isAdmin : user . isAdmin , isOnline : user . isOnline } ; } ) ; return ( new fromAdmin . UsersListFetched ( { usersList } ) ) ; } ) , catchError ( ( error ) => of ( new fromAdmin . AdminError ( { error } ) ) ) ) ) ) ; @ Effect ( ) getUserProjects$ = this . actions$ . pipe ( ofType ( fromAdmin . AdminActionTypes . GET_USER_PROJECTS ) , map ( ( action ) => action . payload ) , mergeMap ( ( payload ) => this . adminService . getUserProjects ( payload . uid ) . pipe ( map ( ( data ) => { const projectsData = data . map ( ( res ) => { const key = res . payload . key ; const project = res . payload . val ( ) ; return { key : key || null , title : project . title || null , description : project . description || null , photoUrl : project . photoUrl || null } ; } ) ; return ( new fromAdmin . UserProjectsLoaded ( { uid : payload . uid , userProjects : projectsData } ) ) ; } ) , catchError ( error => of ( new fromAdmin . AdminError ( { error } ) ) ) ) ) ) ; @ Effect ( { dispatch : false } ) deleteUserProject$ = this . actions$ . pipe ( ofType ( fromAdmin . AdminActionTypes . DELETE_USER_PROJECT ) , map ( ( action ) => action . payload ) , switchMap ( ( payload ) => this . adminService . deleteUserProject ( payload . userId , payload . projectId ) . pipe ( catchError ( ( error ) => of ( new fromAdmin . AdminError ( { error } ) ) ) ) ) ) ; @ Effect ( ) getUserCustomers$ = this . actions$ . pipe ( ofType ( fromAdmin . AdminActionTypes . GET_USER_CUSTOMERS ) , map ( ( action ) => action . payload ) , mergeMap ( ( payload ) => this . adminService . getUserCustomers ( payload . uid ) . pipe ( map ( ( data ) => { const customersData = data . map ( ( res ) => { const key = res . payload . key ; const customer = res . payload . val ( ) ; return { key : key , id : customer . id , name : customer . name , description : customer . description } ; } ) ; return ( new fromAdmin . UserCustomersLoaded ( { uid : payload . uid , userCustomers : customersData } ) ) ; } ) , catchError ( error => of ( new fromAdmin . AdminError ( { error } ) ) ) ) ) ) ; @ Effect ( { dispatch : false } ) deleteUserCustomer$ = this . actions$ . pipe ( ofType ( fromAdmin . AdminActionTypes . DELETE_USER_CUSTOMER ) , map ( ( action ) => action . payload ) , switchMap ( ( payload ) => this . adminService . deleteUserCustomer ( payload . userId , payload . customerId ) . pipe ( catchError ( ( error ) => of ( new fromAdmin . AdminError ( { error } ) ) ) ) ) ) ; @ Effect ( { dispatch : false } ) addAdminPrivileges$ = this . actions$ . pipe ( ofType ( fromAdmin . AdminActionTypes . ADD_ADMIN_PRIVILEGES ) , map ( ( action ) => action . payload ) , switchMap ( ( payload ) => this . adminService . addAdminPrivileges ( payload . userId ) . pipe ( catchError ( ( error ) => of ( new fromAdmin . AdminError ( { error } ) ) ) ) ) ) ; @ Effect ( { dispatch : false } ) removeAdminPrivileges$ = this . actions$ . pipe ( ofType ( fromAdmin . AdminActionTypes . REMOVE_ADMIN_PRIVILEGES ) , map ( ( action ) => action . payload ) , switchMap ( ( payload ) => this . adminService . removeAdminPrivileges ( payload . userId ) . pipe ( catchError ( ( error ) => of ( new fromAdmin . AdminError ( { error } ) ) ) ) ) ) ; }