import { Inject , Injectable } from '@nestjs/common' ; import { InjectModel } from '@nestjs/mongoose' ; import { concatMap , map , Observable , of } from 'rxjs' ; import { Model } from 'mongoose' ; import { CronProcessType , EntityAdmin , EntityOrders , EntityType , EntityUpdate , EntityWithGroupType , EntityWithoutGroupType , } from '@dark-rush-photography/shared/types' ; import { getGoogleDrive } from '@dark-rush-photography/api/util' ; import { Document , DocumentModel } from '../schema/document.schema' ; import { createTestEntity$ , findEntityByIdAndSoftDelete$ , findEntityById$ , updateEntity$ , findEntityByIdAndDelete$ , } from '../entities/entity-repository.functions' ; import { loadEntityAdmin } from '../entities/entity-load-admin.functions' ; import { startCronProcessType } from '../cron-processes/cron-process-start.functions' ; import { validatePublishEntity } from '../entities/entity-publish-validation.functions' ; import { validateEntityFound } from '../entities/entity-validation.functions' ; import { ConfigProvider } from '../providers/config.provider' ; import { EntityGroupProvider } from '../providers/entity-group.provider' ; import { EntityCreateProvider } from '../providers/entity-create.provider' ; import { EntityFindAllProvider } from '../providers/entity-find-all.provider' ; import { EntityOrderProvider } from '../providers/entity-order.provider' ; import { CronProcessRepositoryProvider } from '../providers/cron-process-repository.provider' ; @ Injectable ( ) export class AdminEntitiesService { constructor ( private readonly configProvider , @ InjectModel ( Document . name ) private readonly entityModel , private readonly entityGroupProvider , private readonly entityCreateProvider , private readonly entityFindAllProvider , private readonly entityOrderProvider , @ Inject ( CronProcessRepositoryProvider . name ) private readonly cronProcessRepositoryProvider ) { } createTest$ ( ) { return createTestEntity$ ( this . entityModel ) . pipe ( map ( loadEntityAdmin ) ) ; } order$ ( entityOrders ) { return this . entityOrderProvider . order$ ( entityOrders ) ; } update$ ( entityId , entityUpdate ) { return findEntityById$ ( entityId , this . entityModel ) . pipe ( map ( validateEntityFound ) , concatMap ( ( ) => updateEntity$ ( entityId , entityUpdate , this . entityModel ) ) , map ( ( ) => undefined ) ) ; } publish$ ( entityId , postSocialMedia ) { return findEntityById$ ( entityId , this . entityModel ) . pipe ( map ( validateEntityFound ) , map ( validatePublishEntity ) , concatMap ( ( documentModel ) => { if ( documentModel . type === EntityType . Test ) return of ( undefined ) ; return this . cronProcessRepositoryProvider . create$ ( startCronProcessType ( CronProcessType . PublishEntity , documentModel . type , documentModel . _id , documentModel . group , documentModel . slug , postSocialMedia ) ) ; } ) ) ; } findGroups$ ( entityWithGroupType ) { const googleDrive = getGoogleDrive ( this . configProvider . googleDriveClientEmail , this . configProvider . googleDrivePrivateKey ) ; return this . entityGroupProvider . findGroups$ ( googleDrive , entityWithGroupType ) ; } findAll$ ( entityWithoutGroupType ) { const googleDrive = getGoogleDrive ( this . configProvider . googleDriveClientEmail , this . configProvider . googleDrivePrivateKey ) ; return this . entityCreateProvider . create$ ( googleDrive , entityWithoutGroupType ) . pipe ( concatMap ( ( ) => this . entityFindAllProvider . findAllEntities$ ( entityWithoutGroupType ) ) , map ( ( entities ) => entities . sort ( ( entityA , entityB ) => entityA . order - entityB . order ) ) ) ; } findAllForGroup$ ( entityWithGroupType , group ) { const googleDrive = getGoogleDrive ( this . configProvider . googleDriveClientEmail , this . configProvider . googleDrivePrivateKey ) ; return this . entityCreateProvider . createForGroup$ ( googleDrive , entityWithGroupType , group ) . pipe ( concatMap ( ( ) => this . entityFindAllProvider . findAllEntitiesForGroup$ ( entityWithGroupType , group ) ) , map ( ( entities ) => entities . sort ( ( entityA , entityB ) => entityA . order - entityB . order ) ) ) ; } findOne$ ( entityId ) { return findEntityById$ ( entityId , this . entityModel ) . pipe ( map ( validateEntityFound ) , map ( loadEntityAdmin ) ) ; } delete$ ( entityId ) { return findEntityByIdAndSoftDelete$ ( entityId , this . entityModel ) . pipe ( concatMap ( ( documentModel ) => { if ( ! documentModel ) { return of ( undefined ) ; } if ( documentModel . type === EntityType . Test ) { return findEntityByIdAndDelete$ ( documentModel . _id , this . entityModel ) . pipe ( map ( ( ) => undefined ) ) ; } return this . cronProcessRepositoryProvider . create$ ( startCronProcessType ( CronProcessType . DeleteEntity , documentModel . type , documentModel . _id , documentModel . group , documentModel . slug ) ) ; } ) ) ; } }