Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { MigrationStepCore } from '@sofie-automation/meteor-lib/dist/migrations'
import { Studios } from '../../../collections'
import {
convertObjectIntoOverrides,
ObjectWithOverrides,
} from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
import { flatObjectToOverrides } from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
import { StudioPackageContainerSettings } from '@sofie-automation/shared-lib/dist/core/model/PackageContainer'

export class ContainerIdsToObjectWithOverridesMigrationStep implements Omit<MigrationStepCore, 'version'> {
Expand All @@ -29,10 +26,21 @@ export class ContainerIdsToObjectWithOverridesMigrationStep implements Omit<Migr
// @ts-expect-error thumbnailContainerIds is typed as string[]
const oldThumbnailContainerIds = studio.thumbnailContainerIds

const newPackageContainers = convertObjectIntoOverrides({
previewContainerIds: oldPreviewContainerIds ?? [],
thumbnailContainerIds: oldThumbnailContainerIds ?? [],
} satisfies StudioPackageContainerSettings) as ObjectWithOverrides<StudioPackageContainerSettings>
const changedValues: Partial<StudioPackageContainerSettings> = {}
if (oldPreviewContainerIds && oldPreviewContainerIds.length > 0) {
changedValues.previewContainerIds = oldPreviewContainerIds
}
if (oldThumbnailContainerIds && oldThumbnailContainerIds.length > 0) {
changedValues.thumbnailContainerIds = oldThumbnailContainerIds
}

const newPackageContainers = flatObjectToOverrides<StudioPackageContainerSettings>(
{
previewContainerIds: [],
thumbnailContainerIds: [],
},
changedValues
)

await Studios.updateAsync(studio._id, {
$set: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ describe('ContainerIdsToObjectWithOverridesMigrationStep', () => {
const studio = await Studios.findOneAsync(protectString('studio0'))
expect(studio).toBeTruthy()
expect(studio?.packageContainerSettingsWithOverrides).toMatchObject({
defaults: {},
defaults: {
previewContainerIds: [],
thumbnailContainerIds: [],
},
overrides: [
{ op: 'set', path: 'previewContainerIds', value: ['preview1'] },
{ op: 'set', path: 'thumbnailContainerIds', value: ['thumb1'] },
Expand Down Expand Up @@ -60,11 +63,11 @@ describe('ContainerIdsToObjectWithOverridesMigrationStep', () => {
const studio = await Studios.findOneAsync(protectString('studio1'))
expect(studio).toBeTruthy()
expect(studio?.packageContainerSettingsWithOverrides).toMatchObject({
defaults: {},
overrides: [
{ op: 'set', path: 'previewContainerIds', value: [] },
{ op: 'set', path: 'thumbnailContainerIds', value: [] },
],
defaults: {
previewContainerIds: [],
thumbnailContainerIds: [],
},
overrides: [],
})

const validateResultAfter = await step.validate()
Expand Down
24 changes: 24 additions & 0 deletions packages/corelib/src/settings/objectWithOverrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ export function convertObjectIntoOverrides<T>(
return result
}

/**
* Create ObjectWithOverrides with specified defaults and overrides for changed properties
*/
export function flatObjectToOverrides<T extends object>(
defaults: T,
changedValues: Partial<T>
): ObjectWithOverrides<T> {
const result = wrapDefaultObject<T>(defaults)

for (const [key, value] of Object.entries<any>(changedValues)) {
if (value !== undefined) {
result.overrides.push(
literal<ObjectOverrideSetOp>({
op: 'set',
path: key,
value: value,
})
)
}
}

return result
}

/**
* Update the ObjectWithOverrides overrides values from a flat object.
* If there is an exiting override for a value update the override value if required.
Expand Down
Loading