11import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction'
22import { AdLibPiece } from '@sofie-automation/corelib/dist/dataModel/AdLibPiece'
33import { ExpectedMediaItem } from '@sofie-automation/corelib/dist/dataModel/ExpectedMediaItem'
4- import { ExpectedPackageDB , ExpectedPackageDBType } from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages'
4+ import {
5+ ExpectedPackageDB ,
6+ ExpectedPackageDBType ,
7+ getExpectedPackageIdNew ,
8+ } from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages'
59import { ExpectedPlayoutItem } from '@sofie-automation/corelib/dist/dataModel/ExpectedPlayoutItem'
610import { DBPart } from '@sofie-automation/corelib/dist/dataModel/Part'
711import { Piece } from '@sofie-automation/corelib/dist/dataModel/Piece'
@@ -15,7 +19,7 @@ import { ProtectedString } from '@sofie-automation/corelib/dist/protectedString'
1519import { IngestExpectedPackage } from '../IngestExpectedPackage'
1620import { AnyBulkWriteOperation } from 'mongodb'
1721import { ExpectedPackageId , RundownId } from '@sofie-automation/corelib/dist/dataModel/Ids'
18- import { Complete , normalizeArrayToMap } from '@sofie-automation/corelib/dist/lib'
22+ import { normalizeArrayToMap } from '@sofie-automation/corelib/dist/lib'
1923
2024export class SaveIngestModelHelper {
2125 readonly #rundownId: RundownId
@@ -87,6 +91,8 @@ export class SaveIngestModelHelper {
8791 writeExpectedPackagesChangesForRundown (
8892 context ,
8993 this . #rundownId,
94+ // This isn't using the expectedPackages store fully, because we know the content of the documents is immutable
95+ // But it is easier to keep using the store for the sake of consistency
9096 Array . from ( this . #expectedPackages. getDocumentsToSave ( ) . values ( ) )
9197 ) ,
9298 context . directCollections . ExpectedPlayoutItems . bulkWrite ( this . #expectedPlayoutItems. generateWriteOps ( ) ) ,
@@ -121,53 +127,62 @@ async function writeExpectedPackagesChangesForRundown(
121127 ) ) as Pick < ExpectedPackageDB , '_id' | 'created' > [ ]
122128 const existingDocsMap = normalizeArrayToMap ( existingDocs , '_id' )
123129
124- // Generate any insert and update operations
125- const ops : AnyBulkWriteOperation < ExpectedPackageDB > [ ] = [ ]
130+ const packagesToSave = new Map < ExpectedPackageId , ExpectedPackageDB > ( )
126131 for ( const doc of documentsToSave ) {
127- const newDbDoc : Complete < Omit < ExpectedPackageDB , '_id' > > = {
132+ const packageId = getExpectedPackageIdNew ( context . studioId , doc . package )
133+ const partialDoc = packagesToSave . get ( packageId )
134+
135+ if ( partialDoc ) {
136+ // Add the source to the existing document
137+ partialDoc . ingestSources . push ( doc . source )
138+
139+ // TODO - should we check for duplicates entries?
140+ } else {
141+ // Add a new document
128142 // Future: omit 'playoutSources from this doc
129- studioId : context . studioId ,
130- rundownId : rundownId ,
131- bucketId : null ,
132- created : Date . now ( ) ,
133- package : doc . package ,
134- ingestSources : doc . ingestSources ,
143+ packagesToSave . set ( packageId , {
144+ _id : packageId ,
145+ studioId : context . studioId ,
146+ rundownId : rundownId ,
147+ bucketId : null ,
148+ created : Date . now ( ) ,
149+ package : doc . package ,
150+ ingestSources : [ doc . source ] ,
151+ } )
135152 }
153+ }
136154
155+ // Generate any insert and update operations
156+ const ops : AnyBulkWriteOperation < ExpectedPackageDB > [ ] = [ ]
157+ for ( const doc of packagesToSave . values ( ) ) {
137158 const existingDoc = existingDocsMap . get ( doc . _id )
138159 if ( existingDoc ) {
139160 // Document already exists, perform an update to preserve other fields
140161 ops . push ( {
141162 updateOne : {
142163 filter : { _id : doc . _id } ,
143164 update : {
144- $set : {
145- // Update every field that we want to define
146- ...newDbDoc ,
147- } ,
165+ // Update every field that we want to define
166+ $set : doc ,
148167 } ,
149168 } ,
150169 } )
151170 } else {
152171 // Insert this new document
153172 ops . push ( {
154173 insertOne : {
155- document : {
156- ...newDbDoc ,
157- _id : doc . _id ,
158- } ,
174+ document : doc ,
159175 } ,
160176 } )
161177 }
162178 }
163179
164180 // Look over the existing documents, and see is no longer referenced
165- const documentsToSaveMap = normalizeArrayToMap ( documentsToSave , '_id' )
166181 const idsToDelete : ExpectedPackageId [ ] = [ ]
167182
168183 for ( const doc of existingDocs ) {
169184 // Skip if this document is in the list of documents to save
170- if ( documentsToSaveMap . has ( doc . _id ) ) continue
185+ if ( packagesToSave . has ( doc . _id ) ) continue
171186
172187 // Future: check for playoutSources
173188 idsToDelete . push ( doc . _id )
0 commit comments