@@ -116,6 +116,8 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
116116 }
117117
118118 protected readonly segmentsImpl : Map < SegmentId , SegmentWrapper >
119+ readonly #piecesWithChanges = new Set < PieceId > ( )
120+ #piecesImpl: ReadonlyArray < Piece >
119121
120122 readonly #rundownBaselineExpectedPackagesStore: ExpectedPackagesStore < ExpectedPackageForIngestModelBaseline >
121123
@@ -224,6 +226,8 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
224226 } )
225227 }
226228
229+ this . #piecesImpl = groupedPieces . get ( null ) ?? [ ]
230+
227231 this . #rundownBaselineObjs = new LazyInitialise ( async ( ) =>
228232 context . directCollections . RundownBaselineObjects . findFetch ( {
229233 rundownId : this . rundownId ,
@@ -253,6 +257,7 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
253257 )
254258
255259 this . segmentsImpl = new Map ( )
260+ this . #piecesImpl = [ ]
256261
257262 this . #rundownBaselineObjs = new LazyInitialise ( async ( ) => [ ] )
258263 this . #rundownBaselineAdLibPieces = new LazyInitialise ( async ( ) => [ ] )
@@ -334,6 +339,10 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
334339 return this . getAllOrderedParts ( ) . flatMap ( ( part ) => part . pieces )
335340 }
336341
342+ getGlobalPieces ( ) : ReadonlyDeep < Piece > [ ] {
343+ return [ ...this . #piecesImpl]
344+ }
345+
337346 findPart ( partId : PartId ) : IngestPartModel | undefined {
338347 for ( const segment of this . segmentsImpl . values ( ) ) {
339348 if ( ! segment || segment . deleted ) continue
@@ -477,7 +486,8 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
477486 async setRundownBaseline (
478487 timelineObjectsBlob : PieceTimelineObjectsBlob ,
479488 adlibPieces : RundownBaselineAdLibItem [ ] ,
480- adlibActions : RundownBaselineAdLibAction [ ]
489+ adlibActions : RundownBaselineAdLibAction [ ] ,
490+ pieces : Piece [ ]
481491 ) : Promise < void > {
482492 const [ loadedRundownBaselineObjs , loadedRundownBaselineAdLibPieces , loadedRundownBaselineAdLibActions ] =
483493 await Promise . all ( [
@@ -499,11 +509,13 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
499509 )
500510
501511 // Compare and update the adlibPieces
502- const newAdlibPieces = adlibPieces . map ( ( piece ) => ( {
503- ...clone ( piece ) ,
504- partId : undefined ,
505- rundownId : this . rundownId ,
506- } ) )
512+ const newAdlibPieces = adlibPieces . map (
513+ ( piece ) : AdLibPiece => ( {
514+ ...clone ( piece ) ,
515+ partId : undefined ,
516+ rundownId : this . rundownId ,
517+ } )
518+ )
507519 this . #rundownBaselineAdLibPieces. setValue (
508520 diffAndReturnLatestObjects (
509521 this . #rundownBaselineAdLibPiecesWithChanges,
@@ -513,18 +525,31 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
513525 )
514526
515527 // Compare and update the adlibActions
516- const newAdlibActions = adlibActions . map ( ( action ) => ( {
517- ...clone ( action ) ,
518- partId : undefined ,
519- rundownId : this . rundownId ,
520- } ) )
528+ const newAdlibActions = adlibActions . map (
529+ ( action ) : RundownBaselineAdLibAction => ( {
530+ ...clone ( action ) ,
531+ partId : undefined ,
532+ rundownId : this . rundownId ,
533+ } )
534+ )
521535 this . #rundownBaselineAdLibActions. setValue (
522536 diffAndReturnLatestObjects (
523537 this . #rundownBaselineAdLibActionsWithChanges,
524538 loadedRundownBaselineAdLibActions ,
525539 newAdlibActions
526540 )
527541 )
542+
543+ // Compare and update the rundown pieces
544+ const newPieces = pieces . map (
545+ ( piece ) : Piece => ( {
546+ ...clone ( piece ) ,
547+ startRundownId : this . rundownId ,
548+ startPartId : null ,
549+ startSegmentId : null ,
550+ } )
551+ )
552+ this . #piecesImpl = diffAndReturnLatestObjects ( this . #piecesWithChanges, this . #piecesImpl, newPieces )
528553 }
529554
530555 setRundownOrphaned ( orphaned : RundownOrphanedReason | undefined ) : void {
@@ -628,9 +653,26 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
628653 for ( const segment of this . segmentsImpl . values ( ) ) {
629654 if ( segment . deleted ) {
630655 logOrThrowError ( new Error ( `Failed no changes in model assertion, Segment has been changed` ) )
656+ break
631657 } else {
632658 const err = segment . segmentModel . checkNoChanges ( )
633- if ( err ) logOrThrowError ( err )
659+ if ( err ) {
660+ logOrThrowError ( err )
661+ break
662+ }
663+ }
664+ }
665+
666+ if ( this . #piecesWithChanges. size ) {
667+ logOrThrowError ( new Error ( `Failed no changes in model assertion, Rundown Pieces have been changed` ) )
668+ } else {
669+ for ( const piece of this . #piecesImpl. values ( ) ) {
670+ if ( ! piece ) {
671+ logOrThrowError (
672+ new Error ( `Failed no changes in model assertion, Rundown Pieces have been changed` )
673+ )
674+ break
675+ }
634676 }
635677 }
636678 } finally {
@@ -688,6 +730,8 @@ export class IngestModelImpl implements IngestModel, DatabasePersistedModel {
688730 saveHelper . addExpectedPackagesStore ( this . #rundownBaselineExpectedPackagesStore)
689731 this . #rundownBaselineExpectedPackagesStore. clearChangedFlags ( )
690732
733+ saveHelper . addChangedPieces ( this . #piecesImpl, this . #piecesWithChanges)
734+
691735 await Promise . all ( [
692736 this . #rundownHasChanged && this . #rundownImpl
693737 ? this . context . directCollections . Rundowns . replace ( this . #rundownImpl)
0 commit comments