import { from , Observable } from 'rxjs' ; import { Model } from 'mongoose' ; import { Image , ImageState , ImageUpdate , Dimension , ImageVideo , } from '@dark-rush-photography/shared/types' ; import { DocumentModel } from '../schema/document.schema' ; import { loadUpdateImage , loadUpdateImageCreatedDate , loadUpdateImageOrder , loadUpdateImageSmallDimension , loadUpdateImageState , } from './image-load-document-model.functions' ; export const addImage$ = ( image , entityId , documentModel , entityModel ) : Observable < DocumentModel | null > => from ( entityModel . findByIdAndUpdate ( entityId , { images : [ ... documentModel . images , { ... image } ] , } ) ) ; export const updateImage$ = ( image , imageUpdate , documentModel , entityModel ) : Observable < DocumentModel | null > => { const imageId = image . id ; return from ( entityModel . findByIdAndUpdate ( image . entityId , { images : [ ... documentModel . images . filter ( ( image ) => image . id !== imageId ) , { ... loadUpdateImage ( image , imageUpdate ) , } , ] , } ) ) ; } ; export const updateImageOrder$ = ( image , order , documentModel , entityModel ) : Observable < DocumentModel | null > => { const imageId = image . id ; return from ( entityModel . findByIdAndUpdate ( image . entityId , { images : [ ... documentModel . images . filter ( ( image ) => image . id !== imageId ) , { ... loadUpdateImageOrder ( image , order ) , } , ] , } ) ) ; } ; export const updateImageState$ = ( image , state , documentModel , entityModel ) : Observable < DocumentModel | null > => { const imageId = image . id ; return from ( entityModel . findByIdAndUpdate ( image . entityId , { images : [ ... documentModel . images . filter ( ( image ) => image . id !== imageId ) , { ... loadUpdateImageState ( image , state ) , } , ] , } ) ) ; } ; export const updateImageCreatedDate$ = ( image , createdDate , documentModel , entityModel ) : Observable < DocumentModel | null > => { const imageId = image . id ; return from ( entityModel . findByIdAndUpdate ( image . entityId , { images : [ ... documentModel . images . filter ( ( image ) => image . id !== imageId ) , { ... loadUpdateImageCreatedDate ( image , createdDate ) , } , ] , } ) ) ; } ; export const updateImageSmallDimension$ = ( image , resolution , documentModel , entityModel ) : Observable < DocumentModel | null > => { const imageId = image . id ; return from ( entityModel . findByIdAndUpdate ( image . entityId , { images : [ ... documentModel . images . filter ( ( image ) => image . id !== imageId ) , { ... loadUpdateImageSmallDimension ( image , resolution ) , } , ] , } ) ) ; } ; export const removeImage$ = ( imageId , entityId , documentModel , entityModel ) : Observable < DocumentModel | null > => from ( entityModel . findByIdAndUpdate ( entityId , { images : [ ... documentModel . images . filter ( ( image ) => image . id !== imageId ) ] , } ) ) ; export const addImageVideo$ = ( imageVideo , entityId , entityModel ) : Observable < DocumentModel | null > => from ( entityModel . findByIdAndUpdate ( entityId , { imageVideo : { ... imageVideo } , } ) ) ; export const removeImageVideo$ = ( entityId , entityModel ) : Observable < DocumentModel | null > => from ( entityModel . findByIdAndUpdate ( entityId , { imageVideo : undefined , } ) ) ;