Skip to content

Commit

Permalink
fix: mark prev published revision as unpublished (createFrom mutati…
Browse files Browse the repository at this point in the history
…on) (#4258)
  • Loading branch information
adrians5j authored Sep 10, 2024
1 parent 420cc08 commit b1856b1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/api-headless-cms-ddb-es/src/operations/entry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 20 additions & 2 deletions packages/api-headless-cms-ddb/src/operations/entry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

0 comments on commit b1856b1

Please sign in to comment.