output.text('Create Saga Tasks');
const assetBuilds = base.getTable('Asset Builds');
const sagaLanes = base.getTable('Saga Lanes');
const sagaEpisodes = base.getTable('Saga Episodes');
const tasks = base.getTable('Tasks');
const taskBundles = base.getTable('Task Bundles');
const sagaBundleTasks = await taskBundles.selectRecordsAsync({fields: [
'Automation Type',
'Summary',
'Scope',
'Product Default',
'Copy Default',
'Economy & Config Default',
]});
const sagaBundleTask = sagaBundleTasks.records.find((record) => record.getCellValueAsString('Automation Type') === 'Saga');
if (sagaBundleTask == null) {
throw new Error('You must have a saga task in the room bundles table');
}
const numEpisodes = +sagaBundleTask.getCellValueAsString('Summary').trim().split(' ')[0];
if (isNaN(numEpisodes) || numEpisodes < 1 || numEpisodes > 50) {
throw new Error('You must have a valid number of episodes in the summary field of the saga task');
}
const assetBuild = await input.recordAsync('Asset Build', assetBuilds);
if (assetBuild == null) {
throw new Error('You must select a valid asset build');
}
const sagaLane = await input.recordAsync('Saga & Lane', sagaLanes);
if (sagaLane == null) {
throw new Error('You must select a valid saga & lane');
}
let latestEpisodeNo = sagaLane.getCellValue('Latest Episode No.');
if (isNaN(latestEpisodeNo) || latestEpisodeNo < 0 || !Number.isSafeInteger(latestEpisodeNo)) {
latestEpisodeNo = 0;
}
const episodesName = await input.textAsync('Episodes Name');
const newEpisodeRecords = await sagaEpisodes.createRecordsAsync(Array.from({length: numEpisodes}, (_, i) => ({
fields: {
'Name': episodesName,
'Episode': i + 1 + latestEpisodeNo,
'Saga & Lane': [{id: sagaLane.id}],
},
})));
await tasks.createRecordsAsync(newEpisodeRecords.map((episodeId, i) => ({
fields: {
'Summary': `${episodesName} ${i + 1 + latestEpisodeNo}`,
'Asset Build': [{id: assetBuild.id}],
'Scope': {name: sagaBundleTask.getCellValue('Scope').name},
'Product': {name: sagaBundleTask.getCellValue('Product Default').name},
'Assets': {name: 'Todo'},
'Tech': {name: 'Todo'},
'Copy': {name: sagaBundleTask.getCellValue('Copy Default').name},
'Economy': {name: sagaBundleTask.getCellValue('Economy & Config Default').name},
'QA': {name: 'Todo'},
'Saga Episode': [{id: episodeId}],
'Prevent Automation': true,
},
})));
output.text('Done!');