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+ isPackageReferencedByPlayout ,
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'
@@ -110,13 +114,13 @@ async function writeExpectedPackagesChangesForRundown(
110114 {
111115 projection : {
112116 _id : 1 ,
113- // Future: playoutSources
117+ playoutSources : 1 , // nocommit - can this be made just a count?
114118 } ,
115119 }
116- ) ) as Pick < ExpectedPackageDB , '_id' | 'created' > [ ]
120+ ) ) as Pick < ExpectedPackageDB , '_id' | 'created' | 'playoutSources' > [ ]
117121 const existingDocsMap = normalizeArrayToMap ( existingDocs , '_id' )
118122
119- const packagesToSave = new Map < ExpectedPackageId , ExpectedPackageDB > ( )
123+ const packagesToSave = new Map < ExpectedPackageId , Omit < ExpectedPackageDB , 'playoutSources' > > ( )
120124 for ( const doc of documentsToSave ) {
121125 const partialDoc = packagesToSave . get ( doc . packageId )
122126
@@ -148,7 +152,12 @@ async function writeExpectedPackagesChangesForRundown(
148152 // Insert this new document
149153 ops . push ( {
150154 insertOne : {
151- document : doc ,
155+ document : {
156+ ...doc ,
157+ playoutSources : {
158+ pieceInstanceIds : [ ] ,
159+ } ,
160+ } ,
152161 } ,
153162 } )
154163 } else {
@@ -170,13 +179,17 @@ async function writeExpectedPackagesChangesForRundown(
170179
171180 // Look over the existing documents, and see is no longer referenced
172181 const idsToDelete : ExpectedPackageId [ ] = [ ]
182+ const idsToClearSources : ExpectedPackageId [ ] = [ ]
173183
174184 for ( const doc of existingDocs ) {
175185 // Skip if this document is in the list of documents to save
176186 if ( packagesToSave . has ( doc . _id ) ) continue
177187
178- // Future: check for playoutSources
179- idsToDelete . push ( doc . _id )
188+ if ( isPackageReferencedByPlayout ( doc ) ) {
189+ idsToClearSources . push ( doc . _id )
190+ } else {
191+ idsToDelete . push ( doc . _id )
192+ }
180193 }
181194
182195 if ( idsToDelete . length > 0 ) {
@@ -186,6 +199,18 @@ async function writeExpectedPackagesChangesForRundown(
186199 } ,
187200 } )
188201 }
202+ if ( idsToClearSources . length > 0 ) {
203+ ops . push ( {
204+ updateMany : {
205+ filter : { _id : { $in : idsToClearSources as any } } ,
206+ update : {
207+ $set : {
208+ ingestSources : [ ] ,
209+ } ,
210+ } ,
211+ } ,
212+ } )
213+ }
189214
190215 if ( ops . length > 0 ) await context . directCollections . ExpectedPackages . bulkWrite ( ops )
191216}
0 commit comments