11import { addMigrationSteps } from './databaseMigration'
2- import { PeripheralDevices , RundownPlaylists , Studios } from '../collections'
2+ import {
3+ AdLibActions ,
4+ AdLibPieces ,
5+ ExpectedPackages ,
6+ PeripheralDevices ,
7+ Pieces ,
8+ RundownPlaylists ,
9+ Studios ,
10+ } from '../collections'
311import { assertNever , clone , literal } from '@sofie-automation/corelib/dist/lib'
412import {
513 MappingExt ,
@@ -21,6 +29,13 @@ import {
2129} from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
2230import { JSONBlobStringify , JSONSchema , TSR } from '@sofie-automation/blueprints-integration'
2331import { DEFAULT_MINIMUM_TAKE_SPAN } from '@sofie-automation/shared-lib/dist/core/constants'
32+ import { PartId } from '@sofie-automation/shared-lib/dist/core/model/Ids'
33+ import { protectString } from '@sofie-automation/shared-lib/dist/lib/protectedString'
34+ import { ExpectedPackageDBType } from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages'
35+ import { AdLibActionId , PieceId , RundownBaselineAdLibActionId } from '@sofie-automation/corelib/dist/dataModel/Ids'
36+ import { Piece } from '@sofie-automation/corelib/dist/dataModel/Piece'
37+ import { AdLibPiece } from '@sofie-automation/corelib/dist/dataModel/AdLibPiece'
38+ import { AdLibAction } from '@sofie-automation/corelib/dist/dataModel/AdlibAction'
2439
2540/*
2641 * **************************************************************************************
@@ -145,6 +160,12 @@ const oldDeviceTypeToNewMapping = {
145160 [ OldDeviceType . MULTI_OSC ] : TSR . DeviceType . MULTI_OSC ,
146161}
147162
163+ const EXPECTED_PACKAGE_TYPES_ADDED_PART_ID = [
164+ ExpectedPackageDBType . PIECE ,
165+ ExpectedPackageDBType . ADLIB_PIECE ,
166+ ExpectedPackageDBType . ADLIB_ACTION ,
167+ ]
168+
148169export const addSteps = addMigrationSteps ( '1.50.0' , [
149170 {
150171 id : `Mos gateway fix up config` ,
@@ -824,6 +845,88 @@ export const addSteps = addMigrationSteps('1.50.0', [
824845 )
825846 } ,
826847 } ,
848+
849+ {
850+ id : `ExpectedPackageDBFromAdLibAction and ExpectedPackageDBFromPiece add partId` ,
851+ canBeRunAutomatically : true ,
852+ validate : async ( ) => {
853+ const objectCount = await ExpectedPackages . countDocuments ( {
854+ fromPieceType : { $in : EXPECTED_PACKAGE_TYPES_ADDED_PART_ID as any } , // Force the types, as the query does not match due to the interfaces
855+ partId : { $exists : false } ,
856+ } )
857+
858+ if ( objectCount ) {
859+ return `object needs to be updated`
860+ }
861+ return false
862+ } ,
863+ migrate : async ( ) => {
864+ const objects = await ExpectedPackages . findFetchAsync ( {
865+ fromPieceType : { $in : EXPECTED_PACKAGE_TYPES_ADDED_PART_ID as any } , // Force the types, as the query does not match due to the interfaces
866+ partId : { $exists : false } ,
867+ } )
868+
869+ const neededPieceIds : Array < PieceId | AdLibActionId | RundownBaselineAdLibActionId > = _ . compact (
870+ objects . map ( ( obj ) => obj . pieceId )
871+ )
872+ const [ pieces , adlibPieces , adlibActions ] = await Promise . all ( [
873+ Pieces . findFetchAsync (
874+ {
875+ _id : { $in : neededPieceIds as PieceId [ ] } ,
876+ } ,
877+ {
878+ projection : {
879+ _id : 1 ,
880+ startPartId : 1 ,
881+ } ,
882+ }
883+ ) as Promise < Pick < Piece , '_id' | 'startPartId' > [ ] > ,
884+ AdLibPieces . findFetchAsync (
885+ {
886+ _id : { $in : neededPieceIds as PieceId [ ] } ,
887+ } ,
888+ {
889+ projection : {
890+ _id : 1 ,
891+ partId : 1 ,
892+ } ,
893+ }
894+ ) as Promise < Pick < AdLibPiece , '_id' | 'partId' > [ ] > ,
895+ AdLibActions . findFetchAsync (
896+ {
897+ _id : { $in : neededPieceIds as AdLibActionId [ ] } ,
898+ } ,
899+ {
900+ projection : {
901+ _id : 1 ,
902+ partId : 1 ,
903+ } ,
904+ }
905+ ) as Promise < Pick < AdLibAction , '_id' | 'partId' > [ ] > ,
906+ ] )
907+
908+ const partIdLookup = new Map < PieceId | AdLibActionId | RundownBaselineAdLibActionId , PartId > ( )
909+ for ( const piece of pieces ) {
910+ partIdLookup . set ( piece . _id , piece . startPartId )
911+ }
912+ for ( const adlib of adlibPieces ) {
913+ if ( adlib . partId ) partIdLookup . set ( adlib . _id , adlib . partId )
914+ }
915+ for ( const action of adlibActions ) {
916+ partIdLookup . set ( action . _id , action . partId )
917+ }
918+
919+ for ( const expectedPackage of objects ) {
920+ if ( ! expectedPackage . pieceId ) continue
921+
922+ await ExpectedPackages . mutableCollection . updateAsync ( expectedPackage . _id , {
923+ $set : {
924+ partId : partIdLookup . get ( expectedPackage . pieceId ) ?? protectString ( '' ) ,
925+ } ,
926+ } )
927+ }
928+ } ,
929+ } ,
827930] )
828931
829932function updatePlayoutDeviceTypeInOverride ( op : SomeObjectOverrideOp ) : ObjectOverrideSetOp | undefined {
0 commit comments