1- import { PeripheralDeviceId , RundownId , RundownPlaylistId } from '@sofie-automation/corelib/dist/dataModel/Ids'
1+ import { PartId , PeripheralDeviceId , RundownId , RundownPlaylistId } from '@sofie-automation/corelib/dist/dataModel/Ids'
22import { ReadonlyDeep } from 'type-fest'
33import {
44 CustomPublishCollection ,
88 TriggerUpdate ,
99} from '../../lib/customPublication'
1010import { logger } from '../../logging'
11- import { ContentCache , createReactiveContentCache } from './reactiveContentCache'
11+ import { ContentCache , createReactiveContentCache , PartInstanceFields } from './reactiveContentCache'
1212import { RundownsObserver } from '../lib/rundownsObserver'
1313import { RundownContentObserver } from './rundownContentObserver'
1414import {
@@ -22,6 +22,7 @@ import { protectString } from '@sofie-automation/corelib/dist/protectedString'
2222import { DBRundown } from '@sofie-automation/corelib/dist/dataModel/Rundown'
2323import { NrcsIngestCacheType } from '@sofie-automation/corelib/dist/dataModel/NrcsIngestDataCache'
2424import _ from 'underscore'
25+ import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
2526
2627interface IngestRundownStatusArgs {
2728 readonly deviceId : PeripheralDeviceId
@@ -193,6 +194,28 @@ function regenerateForRundown(cache: ReadonlyDeep<ContentCache>, rundownId: Rund
193194 newDoc . active = playlist . rehearsal ? 'rehearsal' : 'active'
194195 }
195196
197+ // Find the most important part instance for each part
198+ const partInstanceMap = new Map < PartId , Pick < DBPartInstance , PartInstanceFields > > ( )
199+ if ( playlist ) {
200+ for ( const partInstance of cache . PartInstances . find ( { } ) . fetch ( ) ) {
201+ if ( partInstance . rundownId !== rundownId ) continue
202+ // Ignore the next partinstance
203+ if ( partInstance . _id === playlist . nextPartInfo ?. partInstanceId ) continue
204+
205+ // The current part instance is the most important
206+ if ( partInstance . _id === playlist . currentPartInfo ?. partInstanceId ) {
207+ partInstanceMap . set ( partInstance . part . _id , partInstance )
208+ continue
209+ }
210+
211+ // Take the part with the highest takeCount
212+ const existingEntry = partInstanceMap . get ( partInstance . part . _id )
213+ if ( ! existingEntry || existingEntry . takeCount < partInstance . takeCount ) {
214+ partInstanceMap . set ( partInstance . part . _id , partInstance )
215+ }
216+ }
217+ }
218+
196219 const nrcsSegments = cache . NrcsIngestData . find ( { rundownId, type : NrcsIngestCacheType . SEGMENT } ) . fetch ( )
197220 for ( const nrcsSegment of nrcsSegments ) {
198221 const nrcsParts = cache . NrcsIngestData . find ( {
@@ -207,16 +230,8 @@ function regenerateForRundown(cache: ReadonlyDeep<ContentCache>, rundownId: Rund
207230 nrcsParts . map ( ( nrcsPart ) => {
208231 if ( ! nrcsPart . partId || ! nrcsPart . segmentId ) return null
209232
210- const part = cache . Parts . findOne ( { rundownId, _id : nrcsPart . partId } )
211- const partInstance = cache . PartInstances . findOne ( {
212- rundownId,
213- // segmentId: nrcsPart.segmentId, // nocommit - these don't match for some reason
214- 'part._id' : nrcsPart . partId ,
215- // nocommit TODO - prefer the currentPartInstance
216- _id : {
217- $not : playlist ?. nextPartInfo ?. partInstanceId ?? protectString ( '' ) ,
218- } ,
219- } )
233+ const part = cache . Parts . findOne ( { _id : nrcsPart . partId , rundownId } )
234+ const partInstance = partInstanceMap . get ( nrcsPart . partId )
220235
221236 // Determine the playback status from the PartInstance
222237 let playbackStatus = IngestPartPlaybackStatus . UNKNOWN
0 commit comments