Skip to content

Commit

Permalink
fix: page entry and revision syncy with aco record when deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
leopuleo authored and adrians5j committed Aug 14, 2023
1 parent 2b34707 commit 3007f82
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import WebinyError from "@webiny/error";

import { PbAcoContext } from "~/types";
import { PbAcoContext, PbPageRecordData } from "~/types";
import { PB_PAGE_TYPE } from "~/contants";
import { updatePageRecordPayload } from "~/utils/createRecordPayload";

export const onPageAfterDeleteHook = ({ pageBuilder, aco }: PbAcoContext) => {
export const onPageAfterDeleteHook = (context: PbAcoContext) => {
const { aco, pageBuilder } = context;
const app = aco.getApp(PB_PAGE_TYPE);

/**
* Intercept page deletion and delete the related search record.
* Intercept page deletion and delete or update the related search record.
*/
pageBuilder.onPageAfterDelete.subscribe(async ({ page }) => {
try {
await app.search.delete(page.pid);
} catch (error) {
if (error.code === "NOT_FOUND") {
return;
pageBuilder.onPageAfterDelete.subscribe(async ({ page, deleteMethod, latestPage }) => {
// If the deleteMethod is "deleteAll" delete the search record.
if (deleteMethod === "deleteAll") {
try {
await app.search.delete(page.pid);
} catch (error) {
// If the search record is not found, return without further action.
if (error.code === "NOT_FOUND") {
return;
}
throw WebinyError.from(error, {
message: "Error while executing onPageAfterDeleteHook hook",
code: "ACO_AFTER_PAGE_DELETE_HOOK"
});
}
return;
}

try {
if (latestPage) {
// Update the search record with the latest page data.
const payload = await updatePageRecordPayload(context, latestPage);
await app.search.update<PbPageRecordData>(page.pid, payload);
}
} catch (error) {
throw WebinyError.from(error, {
message: "Error while executing onPageAfterDeleteHook hook",
code: "ACO_AFTER_PAGE_DELETE_HOOK"
message: "Error while executing onPageAfterUpdateHook hook",
code: "ACO_AFTER_PAGE_UPDATE_HOOK"
});
}
});
Expand Down
28 changes: 23 additions & 5 deletions packages/api-page-builder/src/graphql/crud/pages.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,27 @@ export const createPageCrud = (params: CreatePageCrudParams): PagesCrud => {

const publishedPage = publishedPageRaw ? await decompressPage(publishedPageRaw) : null;

/**
* Load page revisions, we'll need these to determinate if we are going to delete a single revision or multiple ones
*/
const revisions = await storageOperations.pages.listRevisions({
where: {
pid: pageId,
tenant: getTenantId(),
locale: getLocaleCode()
},
sort: ["version_DESC"],
limit: 2,
after: undefined
});

/**
* We can either delete all the records connected to the given page, or a single revision.
*/
const deleteMethod: "deleteAll" | "delete" =
page.version === 1 ? "deleteAll" : "delete";
let deleteMethod: "delete" | "deleteAll" = "delete";
if (pageId === id || revisions.length === 1) {
deleteMethod = "deleteAll";
}

if (typeof storageOperations.pages[deleteMethod] !== "function") {
throw new WebinyError(
Expand All @@ -784,7 +800,8 @@ export const createPageCrud = (params: CreatePageCrudParams): PagesCrud => {
await onPageBeforeDelete.publish({
page: await decompressPage(page),
latestPage,
publishedPage
publishedPage,
deleteMethod
});

const [resultPageRaw, resultLatestPageRaw] = await storageOperations.pages[
Expand All @@ -804,7 +821,8 @@ export const createPageCrud = (params: CreatePageCrudParams): PagesCrud => {
await onPageAfterDelete.publish({
page: resultPage,
latestPage,
publishedPage
publishedPage,
deleteMethod
});

/**
Expand All @@ -814,7 +832,7 @@ export const createPageCrud = (params: CreatePageCrudParams): PagesCrud => {
/**
* 7. Done. We return both the deleted page, and the new latest one (if there is one).
*/
if (page.version === 1) {
if (deleteMethod === "deleteAll") {
return [resultPage, null] as any;
}
return [resultPage, latestPage] as any;
Expand Down
2 changes: 2 additions & 0 deletions packages/api-page-builder/src/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface OnPageBeforeDeleteTopicParams<TPage extends Page = Page> {
page: TPage;
latestPage: TPage;
publishedPage: TPage | null;
deleteMethod: "deleteAll" | "delete";
}
/**
* @category Lifecycle events
Expand All @@ -123,6 +124,7 @@ export interface OnPageAfterDeleteTopicParams<TPage extends Page = Page> {
page: TPage;
latestPage: TPage | null;
publishedPage: TPage | null;
deleteMethod: "deleteAll" | "delete";
}
/**
* @category Lifecycle events
Expand Down

0 comments on commit 3007f82

Please sign in to comment.