import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable , of } from 'rxjs' ; import { catchError , map } from 'rxjs/operators' ; import { BulkUploadFileInfo , User } from '../../shared/model' ; import { DbService } from './../db-service' ; @ Injectable ( ) export class BulkService { constructor ( private dbService ) { } getBulkUpload ( user , archive ) { if ( ! archive ) { const queryParams = { condition : [ { name : 'isAdminArchived' , comparator : '==' , value : archive } ] } ; return this . dbService . valueChanges ( 'bulk_uploads' , '' , queryParams ) . pipe ( catchError ( error => { console . log ( error ) ; return of ( null ) ; } ) ) ; } else { return this . dbService . valueChanges ( 'bulk_uploads' ) . pipe ( catchError ( error => { console . log ( error ) ; return of ( null ) ; } ) ) ; } } getUserBulkUpload ( user , archive ) { const adminArchive = 'isAdminArchived' ; const userArchive = 'isUserArchived' ; let queryParams ; if ( ! archive ) { if ( user . roles . admin && user . roles . bulkuploader ) { queryParams = { condition : [ { name : 'created_uid' , comparator : '==' , value : user . userId } , { name : userArchive , comparator : '==' , value : archive } , { name : adminArchive , comparator : '==' , value : archive } ] } ; } else if ( user . roles . admin ) { queryParams = { condition : [ { name : 'created_uid' , comparator : '==' , value : user . userId } , { name : adminArchive , comparator : '==' , value : archive } ] } ; } else { queryParams = { condition : [ { name : 'created_uid' , comparator : '==' , value : user . userId } , { name : userArchive , comparator : '==' , value : archive } ] } ; } } else { queryParams = { condition : [ { name : 'created_uid' , comparator : '==' , value : user . userId } ] } ; } return this . dbService . valueChanges ( 'bulk_uploads' , '' , queryParams ) . pipe ( catchError ( error => { console . log ( error ) ; return of ( null ) ; } ) ) ; } getBulkUploadById ( bulkUploadFileInfo ) { return this . dbService . valueChanges ( 'bulk_uploads' , bulkUploadFileInfo . id ) . pipe ( catchError ( error => { console . log ( error ) ; return of ( null ) ; } ) ) ; } updateBulkUpload ( bulkUploadFileInfo ) { const dbBulkUploadFileInfo = Object . assign ( { } , bulkUploadFileInfo ) ; delete dbBulkUploadFileInfo . downloadUrl ; this . dbService . setDoc ( 'bulk_uploads' , dbBulkUploadFileInfo . id , dbBulkUploadFileInfo ) ; } getFileByBulkUploadFileUrl ( bulkUploadFileInfo ) { const filePath = `bulk_upload/${ bulkUploadFileInfo . created_uid }/${ bulkUploadFileInfo . id }-${ bulkUploadFileInfo . fileName }` ; const ref = this . dbService . getFireStorageReference ( filePath ) ; return ref . getDownloadURL ( ) . pipe ( map ( url => url ) ) ; } archiveBulkUpload ( archiveArray , user ) { const isAdmin = user . roles . admin ; let obj = { } ; if ( user . roles . admin && user . roles . bulkuploader ) { obj = { 'isUserArchived' : true , 'isAdminArchived' : true } ; } else if ( user . roles . admin ) { obj = { 'isAdminArchived' : true } ; } else { obj = { 'isUserArchived' : true } ; } const fireStoreInstance = this . dbService . getFireStore ( ) . firestore ; const upload = fireStoreInstance . batch ( ) ; for ( const bulkInfo of archiveArray ) { const itemDoc = fireStoreInstance . collection ( 'bulk_uploads' ) . doc ( bulkInfo . id ) ; upload . update ( itemDoc , obj ) ; } return upload . commit ( ) ; } getBulkUploadFile ( bulkId ) { return this . dbService . valueChanges ( 'bulk_uploads' , bulkId ) . pipe ( catchError ( error => { console . log ( error ) ; return of ( null ) ; } ) ) ; } }