import { Injectable } from '@angular/core' ; import { Effect , Actions , ofType } from '@ngrx/effects' ; import { switchMap , map } from 'rxjs/operators' ; import { empty } from 'rxjs' ; import { BulkUploadFileInfo , Question } from 'shared-library/shared/model' ; import { BulkActionTypes } from '../actions' ; import * as bulkActions from '../actions/bulk.actions' ; import { BulkService , QuestionService } from 'shared-library/core/services' ; @ Injectable ( ) export class BulkEffects { @ Effect ( ) loadBulkUpload$ = this . actions$ . pipe ( ofType ( BulkActionTypes . LOAD_BULK_UPLOAD ) ) . pipe ( switchMap ( ( action ) => this . bulkService . getBulkUpload ( action . payload . user , action . payload . archive ) . pipe ( map ( ( bulkUploadFileInfo ) => new bulkActions . LoadBulkUploadSuccess ( bulkUploadFileInfo ) ) ) ) ) ; @ Effect ( ) loadUserBulkUpload$ = this . actions$ . pipe ( ofType ( BulkActionTypes . LOAD_USER_BULK_UPLOAD ) ) . pipe ( switchMap ( ( action ) => this . bulkService . getUserBulkUpload ( action . payload . user , action . payload . archive ) . pipe ( map ( ( bulkUploadFileInfo ) => new bulkActions . LoadUserBulkUploadSuccess ( bulkUploadFileInfo ) ) ) ) ) ; @ Effect ( ) loadBulkUploadPublishedQuestions$ = this . actions$ . pipe ( ofType ( BulkActionTypes . LOAD_BULK_UPLOAD_PUBLISHED_QUESTIONS ) ) . pipe ( switchMap ( ( action ) => this . questionService . getQuestionsForBulkUpload ( action . payload . bulkUploadFileInfo , true ) . pipe ( map ( ( questions ) => new bulkActions . LoadBulkUploadPublishedQuestionsSuccess ( questions ) ) ) ) ) ; @ Effect ( ) loadBulkUploadUnpublishedQuestions$ = this . actions$ . pipe ( ofType ( BulkActionTypes . LOAD_BULK_UPLOAD_UNPUBLISHED_QUESTIONS ) ) . pipe ( switchMap ( ( action ) => this . questionService . getQuestionsForBulkUpload ( action . payload . bulkUploadFileInfo , false ) . pipe ( map ( ( questions ) => new bulkActions . LoadBulkUploadUnpublishedQuestionsSuccess ( questions ) ) ) ) ) ; @ Effect ( ) updateQuestion$ = this . actions$ . pipe ( ofType ( BulkActionTypes . UPDATE_QUESTION ) ) . pipe ( switchMap ( ( action ) => { this . questionService . saveQuestion ( action . payload . question ) ; return empty ( ) ; } ) ) ; @ Effect ( ) loadBulkUploadFileUrl$ = this . actions$ . pipe ( ofType ( BulkActionTypes . LOAD_BULK_UPLOAD_FILE_URL ) ) . pipe ( switchMap ( ( action ) => this . bulkService . getFileByBulkUploadFileUrl ( action . payload . bulkUploadFileInfo ) . pipe ( map ( ( url ) => new bulkActions . LoadBulkUploadFileUrlSuccess ( url ) ) ) ) ) ; @ Effect ( ) updateBulkUpload$ = this . actions$ . pipe ( ofType ( BulkActionTypes . UPDATE_BULK_UPLOAD ) ) . pipe ( switchMap ( ( action ) => { this . bulkService . updateBulkUpload ( action . payload . bulkUploadFileInfo ) ; return empty ( ) ; } ) ) ; @ Effect ( ) approveQuestion$ = this . actions$ . pipe ( ofType ( BulkActionTypes . APPROVE_QUESTION ) ) . pipe ( switchMap ( ( action ) => { this . questionService . approveQuestion ( action . payload . question ) ; return empty ( ) ; } ) ) ; @ Effect ( ) addBulkQuestions$ = this . actions$ . pipe ( ofType ( BulkActionTypes . ADD_BULK_QUESTIONS ) ) . pipe ( switchMap ( ( action ) => { this . questionService . saveBulkQuestions ( action . payload . bulkUpload ) ; return empty ( ) ; } ) ) ; @ Effect ( ) archiveUserBulkUpload$ = this . actions$ . pipe ( ofType ( BulkActionTypes . ARCHIVE_BULK_UPLOAD ) ) . pipe ( switchMap ( ( action ) => this . bulkService . archiveBulkUpload ( action . payload . archiveArray , action . payload . user ) . then ( ref => { return new bulkActions . ArchiveBulkUploadSuccess ( ) ; } ) ) ) ; @ Effect ( ) getBulkUpload$ = this . actions$ . pipe ( ofType ( BulkActionTypes . LOAD_BULK_UPLOAD_FILE ) ) . pipe ( switchMap ( ( action ) => this . bulkService . getBulkUploadFile ( action . payload . bulkId ) . pipe ( map ( ( bulkUploadFileInfo ) => new bulkActions . LoadBulkUploadFileSuccess ( bulkUploadFileInfo ) ) ) ) ) ; constructor ( private actions$ , private bulkService , private questionService ) { } }