@@ -18,7 +18,7 @@ import {
1818 StudioRouteSetExclusivityGroup ,
1919} from '@sofie-automation/corelib/dist/dataModel/Studio'
2020import { Complete , clone , literal } from '@sofie-automation/corelib/dist/lib'
21- import { protectString } from '@sofie-automation/corelib/dist/protectedString'
21+ import { protectString , unprotectString } from '@sofie-automation/corelib/dist/protectedString'
2222import { applyAndValidateOverrides } from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
2323import { wrapTranslatableMessageFromBlueprints } from '@sofie-automation/corelib/dist/TranslatableMessage'
2424import {
@@ -31,6 +31,7 @@ import { JobContext } from '../jobs/index.js'
3131import { FixUpBlueprintConfigContext } from '@sofie-automation/corelib/dist/fixUpBlueprintConfig/context'
3232import { DEFAULT_MINIMUM_TAKE_SPAN } from '@sofie-automation/shared-lib/dist/core/constants'
3333import { PERIPHERAL_SUBTYPE_PROCESS , PeripheralDevice } from '@sofie-automation/corelib/dist/dataModel/PeripheralDevice'
34+ import { PeripheralDeviceId } from '@sofie-automation/corelib/dist/dataModel/Ids'
3435
3536/**
3637 * Run the Blueprint applyConfig for the studio
@@ -64,20 +65,53 @@ export async function handleBlueprintUpgradeForStudio(context: JobContext, _data
6465 ] )
6566 )
6667
67- const peripheralDevices = ( await context . directCollections . PeripheralDevices . findFetch (
68- { subType : PERIPHERAL_SUBTYPE_PROCESS , 'studioAndConfigId.studioId' : context . studioId } ,
68+ const allPeripheralDevices = ( await context . directCollections . PeripheralDevices . findFetch (
69+ { subType : PERIPHERAL_SUBTYPE_PROCESS } ,
6970 { projection : { _id : 1 , studioAndConfigId : 1 } }
7071 ) ) as Array < Pick < PeripheralDevice , '_id' | 'studioAndConfigId' > >
7172
73+ const configIdMap = new Map < string , PeripheralDeviceId > ( ) // configId -> deviceId
74+ for ( const pd of allPeripheralDevices ) {
75+ if ( pd . studioAndConfigId ) configIdMap . set ( pd . studioAndConfigId . configId , pd . _id )
76+ }
77+
78+ // Assign configId and name to peripheral devices
79+ for ( const configId in parentDevices ) {
80+ const peripheralDevice = allPeripheralDevices . find ( ( pd ) => unprotectString ( pd . _id ) . startsWith ( configId ) )
81+ if ( peripheralDevice ) {
82+ if ( configIdMap . has ( configId ) ) {
83+ // Need to ensure there is only one reference to a configId in the peripheralDevices collection
84+ const existingPeripheralDeviceId = configIdMap . get ( configId )
85+ await context . directCollections . PeripheralDevices . update (
86+ {
87+ studioAndConfigId : { studioId : context . studioId , configId : configId } ,
88+ _id : { $ne : existingPeripheralDeviceId ?? protectString ( '' ) } ,
89+ } ,
90+ {
91+ $unset : {
92+ studioAndConfigId : 1 ,
93+ } ,
94+ }
95+ )
96+ configIdMap . delete ( configId )
97+ }
98+ await context . directCollections . PeripheralDevices . update ( peripheralDevice . _id , {
99+ $set : {
100+ studioAndConfigId : { studioId : context . studioId , configId : configId } ,
101+ } ,
102+ } )
103+ configIdMap . set ( configId , peripheralDevice . _id )
104+ }
105+ }
106+
72107 const playoutDevices = Object . fromEntries (
73- Object . entries < { parentDeviceName ?: string ; options : TSR . DeviceOptionsAny } > ( result . playoutDevices ?? { } ) . map (
108+ Object . entries < { parentConfigId ?: string ; options : TSR . DeviceOptionsAny } > ( result . playoutDevices ?? { } ) . map (
74109 ( dev ) => {
110+ const parentConfigId = dev [ 1 ] . parentConfigId
75111 return [
76112 dev [ 0 ] ,
77113 literal < Complete < StudioPlayoutDevice > > ( {
78- peripheralDeviceId : peripheralDevices . find (
79- ( p ) => p . studioAndConfigId ?. configId === dev [ 1 ] . parentDeviceName
80- ) ?. _id ,
114+ peripheralDeviceId : parentConfigId ? configIdMap . get ( parentConfigId ) : undefined ,
81115 options : dev [ 1 ] . options ,
82116 } ) ,
83117 ]
@@ -86,27 +120,25 @@ export async function handleBlueprintUpgradeForStudio(context: JobContext, _data
86120 )
87121
88122 const ingestDevices = Object . fromEntries (
89- Object . entries < { parentDeviceName ?: string ; options : unknown } > ( result . ingestDevices ?? { } ) . map ( ( dev ) => {
123+ Object . entries < { parentConfigId ?: string ; options : unknown } > ( result . ingestDevices ?? { } ) . map ( ( dev ) => {
124+ const parentConfigId = dev [ 1 ] . parentConfigId
90125 return [
91126 dev [ 0 ] ,
92127 literal < Complete < StudioIngestDevice > > ( {
93- peripheralDeviceId : peripheralDevices . find (
94- ( p ) => p . studioAndConfigId ?. configId === dev [ 1 ] . parentDeviceName
95- ) ?. _id ,
128+ peripheralDeviceId : parentConfigId ? configIdMap . get ( parentConfigId ) : undefined ,
96129 options : dev [ 1 ] . options ,
97130 } ) ,
98131 ]
99132 } )
100133 )
101134
102135 const inputDevices = Object . fromEntries (
103- Object . entries < { parentDeviceName ?: string ; options : unknown } > ( result . inputDevices ?? { } ) . map ( ( dev ) => {
136+ Object . entries < { parentConfigId ?: string ; options : unknown } > ( result . inputDevices ?? { } ) . map ( ( dev ) => {
137+ const parentConfigId = dev [ 1 ] . parentConfigId
104138 return [
105139 dev [ 0 ] ,
106140 literal < Complete < StudioInputDevice > > ( {
107- peripheralDeviceId : peripheralDevices . find (
108- ( p ) => p . studioAndConfigId ?. configId === dev [ 1 ] . parentDeviceName
109- ) ?. _id ,
141+ peripheralDeviceId : parentConfigId ? configIdMap . get ( parentConfigId ) : undefined ,
110142 options : dev [ 1 ] . options ,
111143 } ) ,
112144 ]
0 commit comments