@@ -30,10 +30,17 @@ import { BucketId, ShowStyleBaseId } from '@sofie-automation/corelib/dist/dataMo
3030import { ExpectedPackageDBType } from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages'
3131
3232export async function handleBucketItemImport ( context : JobContext , data : BucketItemImportProps ) : Promise < void > {
33- await regenerateBucketItemFromIngestInfo ( context , data . bucketId , data . showStyleBaseId , {
34- limitToShowStyleVariantIds : data . showStyleVariantIds ,
35- payload : data . payload ,
36- } )
33+ await regenerateBucketItemFromIngestInfo (
34+ context ,
35+ data . bucketId ,
36+ data . showStyleBaseId ,
37+ {
38+ limitToShowStyleVariantIds : data . showStyleVariantIds ,
39+ payload : data . payload ,
40+ } ,
41+ undefined ,
42+ undefined
43+ )
3744}
3845
3946export async function handleBucketItemRegenerate ( context : JobContext , data : BucketItemRegenerateProps ) : Promise < void > {
@@ -49,9 +56,11 @@ export async function handleBucketItemRegenerate(context: JobContext, data: Buck
4956 projection : {
5057 showStyleBaseId : 1 ,
5158 ingestInfo : 1 ,
59+ name : 1 ,
60+ _rank : 1 ,
5261 } ,
5362 }
54- ) as Promise < Pick < BucketAdLib , 'showStyleBaseId' | 'ingestInfo' > > | undefined ,
63+ ) as Promise < Pick < BucketAdLib , 'showStyleBaseId' | 'ingestInfo' | 'name' | '_rank' > > | undefined ,
5564 context . directCollections . BucketAdLibActions . findOne (
5665 {
5766 externalId : data . externalId ,
@@ -62,9 +71,10 @@ export async function handleBucketItemRegenerate(context: JobContext, data: Buck
6271 projection : {
6372 showStyleBaseId : 1 ,
6473 ingestInfo : 1 ,
74+ display : 1 ,
6575 } ,
6676 }
67- ) as Promise < Pick < BucketAdLibAction , 'showStyleBaseId' | 'ingestInfo' > > | undefined ,
77+ ) as Promise < Pick < BucketAdLibAction , 'showStyleBaseId' | 'ingestInfo' | 'display' > > | undefined ,
6878 ] )
6979
7080 // TODO - UserErrors?
@@ -74,15 +84,19 @@ export async function handleBucketItemRegenerate(context: JobContext, data: Buck
7484 context ,
7585 data . bucketId ,
7686 adlibAction . showStyleBaseId ,
77- adlibAction . ingestInfo
87+ adlibAction . ingestInfo ,
88+ adlibAction . display . _rank ,
89+ typeof adlibAction . display . label === 'string' ? adlibAction . display . label : undefined
7890 )
7991 } else if ( adlibPiece ) {
8092 if ( ! adlibPiece . ingestInfo ) throw new Error ( `Bucket AdLibPiece cannot be resynced, it has no ingest data` )
8193 await regenerateBucketItemFromIngestInfo (
8294 context ,
8395 data . bucketId ,
8496 adlibPiece . showStyleBaseId ,
85- adlibPiece . ingestInfo
97+ adlibPiece . ingestInfo ,
98+ adlibPiece . _rank ,
99+ adlibPiece . name
86100 )
87101 } else {
88102 throw new Error ( `No Bucket Items with externalId "${ data . externalId } " were found` )
@@ -93,7 +107,9 @@ async function regenerateBucketItemFromIngestInfo(
93107 context : JobContext ,
94108 bucketId : BucketId ,
95109 showStyleBaseId : ShowStyleBaseId ,
96- ingestInfo : BucketAdLibIngestInfo
110+ ingestInfo : BucketAdLibIngestInfo ,
111+ oldRank : number | undefined ,
112+ oldLabel : string | undefined
97113) : Promise < void > {
98114 const [ showStyleBase , allShowStyleVariants , allOldAdLibPieces , allOldAdLibActions , blueprint ] = await Promise . all ( [
99115 context . getShowStyleBase ( showStyleBaseId ) ,
@@ -130,7 +146,7 @@ async function regenerateBucketItemFromIngestInfo(
130146 const actionIdsToRemove = new Set ( allOldAdLibActions . map ( ( p ) => p . _id ) )
131147
132148 let isFirstShowStyleVariant = true
133- let newRank : number | undefined = undefined
149+ const newRank : number | undefined = oldRank ?? ( await calculateHighestRankInBucket ( context , bucketId ) ) + 1
134150 let onlyGenerateOneItem = false
135151
136152 const ps : Promise < any > [ ] = [ ]
@@ -150,13 +166,6 @@ async function regenerateBucketItemFromIngestInfo(
150166 core : getSystemVersion ( ) ,
151167 }
152168
153- // Cache the newRank, so we only have to calculate it once:
154- if ( newRank === undefined ) {
155- newRank = ( await calculateHighestRankInBucket ( context , bucketId ) ) + 1
156- } else {
157- newRank ++
158- }
159-
160169 if ( isAdlibAction ( rawAdlib ) ) {
161170 if ( isFirstShowStyleVariant ) {
162171 if ( rawAdlib . allVariants ) {
@@ -174,6 +183,7 @@ async function regenerateBucketItemFromIngestInfo(
174183 blueprint . blueprintId ,
175184 bucketId ,
176185 newRank ,
186+ oldLabel ,
177187 importVersions
178188 )
179189
@@ -194,6 +204,7 @@ async function regenerateBucketItemFromIngestInfo(
194204 blueprint . blueprintId ,
195205 bucketId ,
196206 newRank ,
207+ oldLabel ,
197208 importVersions
198209 )
199210
0 commit comments