From b1856b15793700e6824a33b481e54b087f8c33d9 Mon Sep 17 00:00:00 2001 From: Adrian Smijulj Date: Tue, 10 Sep 2024 15:50:29 +0200 Subject: [PATCH] fix: mark prev published revision as unpublished (`createFrom` mutation) (#4258) --- .../src/operations/entry/index.ts | 24 +++++++++++++++++++ .../src/operations/entry/index.ts | 22 +++++++++++++++-- ...tentEntriesOnByMetaFieldsOverrides.test.ts | 12 ++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/api-headless-cms-ddb-es/src/operations/entry/index.ts b/packages/api-headless-cms-ddb-es/src/operations/entry/index.ts index 39e2a780bab..96d25211a96 100644 --- a/packages/api-headless-cms-ddb-es/src/operations/entry/index.ts +++ b/packages/api-headless-cms-ddb-es/src/operations/entry/index.ts @@ -314,6 +314,30 @@ export const createEntriesStorageOperations = ( ...publishedKeys }) ); + + // Unpublish previously published revision (if any). + const [publishedRevisionStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId( + { + model, + ids: [entry.id] + } + ); + + if (publishedRevisionStorageEntry) { + items.push( + entity.putBatch({ + ...publishedRevisionStorageEntry, + PK: createPartitionKey({ + id: publishedRevisionStorageEntry.id, + locale: model.locale, + tenant: model.tenant + }), + SK: createRevisionSortKey(publishedRevisionStorageEntry), + TYPE: createRecordType(), + status: CONTENT_ENTRY_STATUS.UNPUBLISHED + }) + ); + } } try { diff --git a/packages/api-headless-cms-ddb/src/operations/entry/index.ts b/packages/api-headless-cms-ddb/src/operations/entry/index.ts index 393387c5bbd..2ae39e96bd2 100644 --- a/packages/api-headless-cms-ddb/src/operations/entry/index.ts +++ b/packages/api-headless-cms-ddb/src/operations/entry/index.ts @@ -245,8 +245,10 @@ export const createEntriesStorageOperations = ( /** * We need to: * - create the main entry item - * - update the last entry item to a current one - * - update the published entry item to a current one (if the entry is published) + * - update the latest entry item to the current one + * - if the entry's status was set to "published": + * - update the published entry item to the current one + * - unpublish previously published revision (if any) */ const items = [ entity.putBatch({ @@ -279,6 +281,22 @@ export const createEntriesStorageOperations = ( GSI1_SK: createGSISortKey(storageEntry) }) ); + + // Unpublish previously published revision (if any). + const publishedRevisionStorageEntry = await getPublishedRevisionByEntryId(model, entry); + if (publishedRevisionStorageEntry) { + items.push( + entity.putBatch({ + ...publishedRevisionStorageEntry, + PK: partitionKey, + SK: createRevisionSortKey(publishedRevisionStorageEntry), + TYPE: createType(), + status: CONTENT_ENTRY_STATUS.UNPUBLISHED, + GSI1_PK: createGSIPartitionKey(model, "A"), + GSI1_SK: createGSISortKey(publishedRevisionStorageEntry) + }) + ); + } } try { diff --git a/packages/api-headless-cms/__tests__/contentAPI/contentEntriesOnByMetaFieldsOverrides.test.ts b/packages/api-headless-cms/__tests__/contentAPI/contentEntriesOnByMetaFieldsOverrides.test.ts index ee008997469..80255559064 100644 --- a/packages/api-headless-cms/__tests__/contentAPI/contentEntriesOnByMetaFieldsOverrides.test.ts +++ b/packages/api-headless-cms/__tests__/contentAPI/contentEntriesOnByMetaFieldsOverrides.test.ts @@ -163,5 +163,17 @@ describe("Content entries - Entry Meta Fields Overrides", () => { const { data: listEntriesRead } = await readIdentityA.listTestEntries(); expect(listEntriesRead[0].id).toEndWith("#0003"); + + // Extra check - ensure the previous revision is no longer published. + const { data: firstPublishedRevision } = await manageIdentityA.getTestEntry({ + revision: `${rev.entryId}#0001` + }); + + const { data: secondPublishedRevision } = await manageIdentityA.getTestEntry({ + revision: `${rev.entryId}#0001` + }); + + expect(firstPublishedRevision.meta.status).toBe("unpublished"); + expect(secondPublishedRevision.meta.status).toBe("unpublished"); }); });