import { Inject , Injectable } from '@nestjs/common' ; import { InjectModel } from '@nestjs/mongoose' ; import { concatMap , map , Observable , of } from 'rxjs' ; import { Model } from 'mongoose' ; import { CronProcessType , EntityType , ImageAdmin , ImageOrders , ImageSelections , ImageState , ImageStates , ImageUpdate , } from '@dark-rush-photography/shared/types' ; import { Document , DocumentModel } from '../schema/document.schema' ; import { findEntityById$ } from '../entities/entity-repository.functions' ; import { validateEntityFound } from '../entities/entity-validation.functions' ; import { updateImage$ } from '../images/image-repository.functions' ; import { loadImageAdmin } from '../images/image-load.functions' ; import { validateImageFound , validatePublishImage , } from '../images/image-validation.functions' ; import { startCronProcessType } from '../cron-processes/cron-process-start.functions' ; import { ImageAddProvider } from '../providers/image-add.provider' ; import { ImageOrderProvider } from '../providers/image-order.provider' ; import { ImageStateChangeProvider } from '../providers/image-state-change.provider' ; import { ImageRemoveOneProvider } from '../providers/image-remove-one.provider' ; import { CronProcessRepositoryProvider } from '../providers/cron-process-repository.provider' ; @ Injectable ( ) export class ImagesService { constructor ( @ InjectModel ( Document . name ) private readonly entityModel , private readonly imageAddProvider , private readonly imageOrderProvider , private readonly imageStateChangeProvider , private readonly imageRemoveOneProvider , @ Inject ( CronProcessRepositoryProvider . name ) private readonly cronProcessRepositoryProvider ) { } addTestImage$ ( entityId ) { return this . imageAddProvider . addTestImage$ ( entityId ) . pipe ( map ( ( image ) => loadImageAdmin ( image ) ) ) ; } loadImages$ ( entityId , imageStates ) { return findEntityById$ ( entityId , this . entityModel ) . pipe ( map ( validateEntityFound ) , map ( ( documentModel ) => documentModel . images . filter ( ( image ) => imageStates . imageStates . includes ( image . state ) ) . sort ( ( imageA , imageB ) => imageA . order - imageB . order ) . map ( ( image ) => loadImageAdmin ( image ) ) ) ) ; } updateNewImages$ ( entityId ) { return findEntityById$ ( entityId , this . entityModel ) . pipe ( map ( validateEntityFound ) , concatMap ( ( documentModel ) => { if ( documentModel . type === EntityType . Test ) return of ( undefined ) ; return this . cronProcessRepositoryProvider . create$ ( startCronProcessType ( CronProcessType . UpdateNewImages , documentModel . type , documentModel . _id , documentModel . group , documentModel . slug ) ) ; } ) , map ( ( ) => undefined ) ) ; } orderImages$ ( entityId , imageOrders ) { return this . imageOrderProvider . orderImages$ ( entityId , imageOrders ) ; } selectNewImages$ ( entityId , imageSelections ) { return this . imageStateChangeProvider . selectNewImages$ ( entityId , imageSelections ) ; } updatePublishImage$ ( imageId , entityId , imageUpdate ) { return findEntityById$ ( entityId , this . entityModel ) . pipe ( map ( validateEntityFound ) , concatMap ( ( documentModel ) => { const image = validateImageFound ( imageId , documentModel . images ) ; return updateImage$ ( validatePublishImage ( image ) , imageUpdate , documentModel , this . entityModel ) ; } ) , map ( ( ) => undefined ) ) ; } archiveImage$ ( imageId , entityId ) { return this . imageStateChangeProvider . archiveImage$ ( imageId , entityId ) ; } unarchiveImage$ ( imageId , entityId ) { return this . imageStateChangeProvider . unarchiveImage$ ( imageId , entityId ) ; } removePublishImage$ ( imageId , entityId ) { return findEntityById$ ( entityId , this . entityModel ) . pipe ( concatMap ( ( documentModel ) => { if ( ! documentModel ) return of ( undefined ) ; const image = documentModel . images . find ( ( image ) => image . id === imageId ) ; if ( ! image || ( image . state !== ImageState . Selected && image . state !== ImageState . Public ) ) { return of ( undefined ) ; } return this . imageRemoveOneProvider . removeImage$ ( image ) ; } ) ) ; } }