@@ -3,25 +3,21 @@ import {
33 JSONBlobParse ,
44 ShowStyleBlueprintManifest ,
55} from '@sofie-automation/blueprints-integration'
6- import { ShowStyleBaseId , TriggeredActionId } from '@sofie-automation/corelib/dist/dataModel/Ids'
7- import { normalizeArray , normalizeArrayToMap , getRandomId , literal , Complete } from '@sofie-automation/corelib/dist/lib'
8- import {
9- applyAndValidateOverrides ,
10- wrapDefaultObject ,
11- } from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
6+ import { ShowStyleBaseId } from '@sofie-automation/corelib/dist/dataModel/Ids'
7+ import { normalizeArray } from '@sofie-automation/corelib/dist/lib'
8+ import { applyAndValidateOverrides } from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
129import { wrapTranslatableMessageFromBlueprints } from '@sofie-automation/corelib/dist/TranslatableMessage'
1310import { BlueprintValidateConfigForStudioResult } from '@sofie-automation/corelib/dist/worker/studio'
1411import { Meteor } from 'meteor/meteor'
15- import { Blueprints , ShowStyleBases , TriggeredActions } from '../../collections'
12+ import { Blueprints , ShowStyleBases } from '../../collections'
1613import { DBShowStyleBase } from '@sofie-automation/corelib/dist/dataModel/ShowStyleBase'
17- import { DBTriggeredActions } from '@sofie-automation/meteor-lib/dist/collections/TriggeredActions'
1814import { evalBlueprint } from '../../api/blueprints/cache'
1915import { logger } from '../../logging'
2016import { CommonContext } from './context'
21- import type { AnyBulkWriteOperation } from 'mongodb'
2217import { FixUpBlueprintConfigContext } from '@sofie-automation/corelib/dist/fixUpBlueprintConfig/context'
2318import { Blueprint } from '@sofie-automation/corelib/dist/dataModel/Blueprint'
2419import { BlueprintFixUpConfigMessage } from '@sofie-automation/meteor-lib/dist/api/migration'
20+ import { updateTriggeredActionsForShowStyleBaseId } from './lib'
2521
2622export async function fixupConfigForShowStyleBase (
2723 showStyleBaseId : ShowStyleBaseId
@@ -100,7 +96,7 @@ export async function validateConfigForShowStyleBase(
10096 throwIfNeedsFixupConfigRunning ( showStyleBase , blueprint , blueprintManifest )
10197
10298 const blueprintContext = new CommonContext (
103- 'applyConfig ' ,
99+ 'validateConfig ' ,
104100 `showStyleBase:${ showStyleBaseId } ,blueprint:${ blueprint . _id } `
105101 )
106102 const rawBlueprintConfig = applyAndValidateOverrides ( showStyleBase . blueprintConfigWithOverrides ) . obj
@@ -146,69 +142,7 @@ export async function runUpgradeForShowStyleBase(showStyleBaseId: ShowStyleBaseI
146142 } ,
147143 } )
148144
149- const oldTriggeredActionsArray = await TriggeredActions . findFetchAsync ( {
150- showStyleBaseId : showStyleBaseId ,
151- blueprintUniqueId : { $ne : null } ,
152- } )
153- const oldTriggeredActions = normalizeArrayToMap ( oldTriggeredActionsArray , 'blueprintUniqueId' )
154-
155- const newDocIds : TriggeredActionId [ ] = [ ]
156- const bulkOps : AnyBulkWriteOperation < DBTriggeredActions > [ ] = [ ]
157-
158- for ( const newTriggeredAction of result . triggeredActions ) {
159- const oldValue = oldTriggeredActions . get ( newTriggeredAction . _id )
160- if ( oldValue ) {
161- // Update an existing TriggeredAction
162- newDocIds . push ( oldValue . _id )
163- bulkOps . push ( {
164- updateOne : {
165- filter : {
166- _id : oldValue . _id ,
167- } ,
168- update : {
169- $set : {
170- _rank : newTriggeredAction . _rank ,
171- name : newTriggeredAction . name ,
172- 'triggersWithOverrides.defaults' : newTriggeredAction . triggers ,
173- 'actionsWithOverrides.defaults' : newTriggeredAction . actions ,
174- } ,
175- } ,
176- } ,
177- } )
178- } else {
179- // Insert a new TriggeredAction
180- const newDocId = getRandomId < TriggeredActionId > ( )
181- newDocIds . push ( newDocId )
182- bulkOps . push ( {
183- insertOne : {
184- document : literal < Complete < DBTriggeredActions > > ( {
185- _id : newDocId ,
186- _rank : newTriggeredAction . _rank ,
187- name : newTriggeredAction . name ,
188- showStyleBaseId : showStyleBaseId ,
189- blueprintUniqueId : newTriggeredAction . _id ,
190- triggersWithOverrides : wrapDefaultObject ( newTriggeredAction . triggers ) ,
191- actionsWithOverrides : wrapDefaultObject ( newTriggeredAction . actions ) ,
192- styleClassNames : newTriggeredAction . styleClassNames ,
193- } ) ,
194- } ,
195- } )
196- }
197- }
198-
199- // Remove any removed TriggeredAction
200- // Future: should this orphan them or something? Will that cause issues if they get re-added?
201- bulkOps . push ( {
202- deleteMany : {
203- filter : {
204- showStyleBaseId : showStyleBaseId ,
205- blueprintUniqueId : { $ne : null } ,
206- _id : { $nin : newDocIds } ,
207- } ,
208- } ,
209- } )
210-
211- await TriggeredActions . bulkWriteAsync ( bulkOps )
145+ await updateTriggeredActionsForShowStyleBaseId ( showStyleBaseId , result . triggeredActions )
212146}
213147
214148async function loadShowStyleAndBlueprint ( showStyleBaseId : ShowStyleBaseId ) {
0 commit comments