|
| 1 | +import { Meteor } from 'meteor/meteor' |
| 2 | +import { check } from '../../lib/check' |
| 3 | +import { MethodContext } from '../methodContext' |
| 4 | +import { checkAccessAndGetPeripheralDevice } from '../../security/check' |
| 5 | +import { PeripheralDeviceId } from '@sofie-automation/corelib/dist/dataModel/Ids' |
| 6 | +import { PieceStatusCode } from '@sofie-automation/corelib/dist/dataModel/Piece' |
| 7 | +import { |
| 8 | + RundownContentStatusResponse, |
| 9 | + RundownPieceContentStatus, |
| 10 | +} from '@sofie-automation/shared-lib/dist/peripheralDevice/rundownContentStatus' |
| 11 | +import { Blueprints, Parts, Pieces, Rundowns, ShowStyleBases } from '../../collections' |
| 12 | +import { fetchStudio } from '../../publications/pieceContentStatusUI/common' |
| 13 | +import { |
| 14 | + checkPieceContentStatusAndDependencies, |
| 15 | + PieceContentStatusPiece, |
| 16 | +} from '../../publications/pieceContentStatusUI/checkPieceContentStatus' |
| 17 | +import { PieceContentStatusMessageFactory } from '../../publications/pieceContentStatusUI/messageFactory' |
| 18 | +import { interpollateTranslation, translateMessage } from '@sofie-automation/corelib/dist/TranslatableMessage' |
| 19 | + |
| 20 | +function formatStatusReason( |
| 21 | + status: Awaited<ReturnType<typeof checkPieceContentStatusAndDependencies>>[0] |
| 22 | +): string | undefined { |
| 23 | + if (status.status === PieceStatusCode.OK) { |
| 24 | + return undefined |
| 25 | + } |
| 26 | + |
| 27 | + const firstMessage = status.messages[0] |
| 28 | + if (!firstMessage) { |
| 29 | + return undefined |
| 30 | + } |
| 31 | + |
| 32 | + return translateMessage(firstMessage, interpollateTranslation) |
| 33 | +} |
| 34 | + |
| 35 | +export namespace RundownContentStatusIntegration { |
| 36 | + export async function getContentStatusForRundown( |
| 37 | + context: MethodContext, |
| 38 | + deviceId: PeripheralDeviceId, |
| 39 | + deviceToken: string, |
| 40 | + rundownExternalId: string |
| 41 | + ): Promise<RundownContentStatusResponse> { |
| 42 | + check(rundownExternalId, String) |
| 43 | + |
| 44 | + const peripheralDevice = await checkAccessAndGetPeripheralDevice(deviceId, deviceToken, context) |
| 45 | + if (!peripheralDevice.studioAndConfigId) { |
| 46 | + throw new Meteor.Error(400, 'Device "' + peripheralDevice._id + '" has no studio') |
| 47 | + } |
| 48 | + |
| 49 | + const studioId = peripheralDevice.studioAndConfigId.studioId |
| 50 | + const studio = await fetchStudio(studioId) |
| 51 | + if (!studio) { |
| 52 | + throw new Meteor.Error(404, `Studio "${studioId}" not found`) |
| 53 | + } |
| 54 | + |
| 55 | + const rundown = await Rundowns.findOneAsync({ |
| 56 | + studioId, |
| 57 | + externalId: rundownExternalId, |
| 58 | + }) |
| 59 | + if (!rundown) { |
| 60 | + return { |
| 61 | + rundownExternalId, |
| 62 | + pieces: [], |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + const showStyleBase = await ShowStyleBases.findOneAsync(rundown.showStyleBaseId) |
| 67 | + const blueprint = showStyleBase ? await Blueprints.findOneAsync(showStyleBase.blueprintId) : undefined |
| 68 | + const messageFactory = new PieceContentStatusMessageFactory(blueprint) |
| 69 | + |
| 70 | + const parts = await Parts.findFetchAsync({ rundownId: rundown._id }) |
| 71 | + const partExternalIds = new Map(parts.map((part) => [part._id, part.externalId])) |
| 72 | + |
| 73 | + const pieceDocs = await Pieces.findFetchAsync({ |
| 74 | + startRundownId: rundown._id, |
| 75 | + invalid: { $ne: true }, |
| 76 | + }) |
| 77 | + |
| 78 | + const pieces: RundownPieceContentStatus[] = [] |
| 79 | + |
| 80 | + for (const pieceDoc of pieceDocs) { |
| 81 | + const sourceLayer = showStyleBase?.sourceLayers?.[pieceDoc.sourceLayerId] |
| 82 | + if (!sourceLayer) { |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + const statusPiece: PieceContentStatusPiece = { |
| 87 | + _id: pieceDoc._id, |
| 88 | + content: pieceDoc.content, |
| 89 | + expectedPackages: pieceDoc.expectedPackages, |
| 90 | + name: pieceDoc.name, |
| 91 | + } |
| 92 | + |
| 93 | + const [status] = await checkPieceContentStatusAndDependencies( |
| 94 | + studio, |
| 95 | + rundown._id, |
| 96 | + messageFactory, |
| 97 | + statusPiece, |
| 98 | + sourceLayer |
| 99 | + ) |
| 100 | + |
| 101 | + pieces.push({ |
| 102 | + pieceExternalId: pieceDoc.externalId, |
| 103 | + partExternalId: pieceDoc.startPartId ? partExternalIds.get(pieceDoc.startPartId) : undefined, |
| 104 | + statusCode: status.status, |
| 105 | + ready: status.status === PieceStatusCode.OK, |
| 106 | + reason: formatStatusReason(status), |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + return { |
| 111 | + rundownExternalId, |
| 112 | + pieces, |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments