Skip to content

Commit

Permalink
fix(api-page-builder): add safeguards for invalid state of DB records (
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Oct 13, 2023
1 parent 949052a commit 80fefae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ export const createPageStorageOperations = (
const publish = async (params: PageStorageOperationsPublishParams): Promise<Page> => {
const { page, latestPage, publishedPage } = params;

page.status = "published";

/**
* Update the given revision of the page.
*/
Expand All @@ -451,9 +453,8 @@ export const createPageStorageOperations = (
];
const esItems = [];
/**
* If we are publishing the latest revision, let's also update the latest revision entry's
* status in ES. Also, if we are publishing the latest revision, we need to update the latest
* page revision entry in ES.
* If we are publishing the latest revision, update the latest revision
* status in ES. We also need to update the latest page revision entry in ES.
*/
if (latestPage.id === page.id) {
items.push(
Expand All @@ -475,11 +476,10 @@ export const createPageStorageOperations = (
);
}
/**
* If we have already published revision of this page:
* - set existing published page revision to unpublished
* - remove old published path if paths are different
* If we already have a published revision, and it's not the revision being published:
* - set the existing published revision to "unpublished"
*/
if (publishedPage) {
if (publishedPage && publishedPage.id !== page.id) {
items.push(
entity.putBatch({
...publishedPage,
Expand Down Expand Up @@ -568,6 +568,8 @@ export const createPageStorageOperations = (
const unpublish = async (params: PageStorageOperationsUnpublishParams): Promise<Page> => {
const { page, latestPage } = params;

page.status = "unpublished";

const items = [
entity.deleteBatch({
PK: createPartitionKey(page),
Expand Down
27 changes: 14 additions & 13 deletions packages/api-page-builder-so-ddb/src/operations/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,16 @@ export const createPageStorageOperations = (
};

/**
* We need to
* - update revision that it is published
* - if is latest update record that it is published
* - set status of previously published page to unpublished
* - create / update published record
* - create / update published path
* - update the revision record
* - if the revision being published is also the "latest" revision, update the "latest" record
* - set the status of the previously published revision to "unpublished"
* - create/update the "published" record
*/
const publish = async (params: PageStorageOperationsPublishParams): Promise<Page> => {
const { page, latestPage, publishedPage } = params;

page.status = "published";

const revisionKeys = {
PK: createRevisionPartitionKey(page),
SK: createRevisionSortKey(page)
Expand Down Expand Up @@ -433,10 +433,10 @@ export const createPageStorageOperations = (
);
}
/**
* If we have already published revision of this page:
* - set existing published page revision to unpublished
* If we already have a published revision, and it's not the revision being published:
* - set the existing published revision to "unpublished"
*/
if (publishedPage) {
if (publishedPage && publishedPage.id !== page.id) {
const publishedRevisionKeys = {
PK: createRevisionPartitionKey(publishedPage),
SK: createRevisionSortKey(publishedPage)
Expand Down Expand Up @@ -477,14 +477,15 @@ export const createPageStorageOperations = (

/**
* We need to
* - update revision record with new status
* - remove published record
* - remove published path record
* - update latest record with new status if is the latest
* - update the revision record
* - remove the "published" record
* - if the revision being unpublished is also the "latest" revision, update the "latest" record with the new status
*/
const unpublish = async (params: PageStorageOperationsUnpublishParams): Promise<Page> => {
const { page, latestPage } = params;

page.status = "unpublished";

const revisionKeys = {
PK: createRevisionPartitionKey(page),
SK: createRevisionSortKey(page)
Expand Down

0 comments on commit 80fefae

Please sign in to comment.