Fixed getting changes after deletion

This commit is contained in:
DrMint 2024-07-27 07:33:09 +02:00
parent 1fb10535a6
commit 47dd0557d5
2 changed files with 36 additions and 12 deletions

View File

@ -14,7 +14,11 @@ import {
Video,
} from "../types/collections";
import { isPayloadType } from "../utils/asserts";
import { AfterChangeHook, AfterDeleteHook } from "payload/dist/collections/config/types";
import {
AfterChangeHook,
AfterDeleteHook,
BeforeDeleteHook,
} from "payload/dist/collections/config/types";
import { GeneratedTypes } from "payload";
import { uniqueBy } from "../utils/array";
import { GlobalAfterChangeHook } from "payload/types";
@ -35,12 +39,23 @@ export const afterOutgoingRelationRemovedSendChangesWebhook = async ({
export const afterChangeSendChangesWebhook: AfterChangeHook = async ({ doc, collection }) => {
if ("_status" in doc && doc._status === "draft") return doc;
await commonLogic(collection.slug as keyof GeneratedTypes["collections"], doc);
const changes = await getChanges(collection.slug as keyof GeneratedTypes["collections"], doc);
await sendWebhookMessage(changes);
return doc;
};
export const afterDeleteSendChangesWebhook: AfterDeleteHook = async ({ doc, collection }) => {
await commonLogic(collection.slug as keyof GeneratedTypes["collections"], doc);
export const beforeDeletePrepareChanges: BeforeDeleteHook = async ({ id, collection, context }) => {
const changes = await getChanges(collection.slug as keyof GeneratedTypes["collections"], { id });
context.beforeDeleteChanges = changes;
};
export const afterDeleteSendChangesWebhook: AfterDeleteHook = async ({ doc, context }) => {
const changes = context.beforeDeleteChanges as EndpointChange[] | undefined;
if (changes) {
await sendWebhookMessage(changes);
}
return doc;
};
@ -62,17 +77,20 @@ export const globalAfterChangeSendChangesWebhook: GlobalAfterChangeHook = async
return doc;
};
const commonLogic = async (slug: keyof GeneratedTypes["collections"], doc: any) => {
if (slug === "relationships") return doc;
if (slug === "payload-migrations") return doc;
if (slug === "payload-preferences") return doc;
const getChanges = async (
slug: keyof GeneratedTypes["collections"],
doc: any
): Promise<EndpointChange[]> => {
if (slug === "relationships") return [];
if (slug === "payload-migrations") return [];
if (slug === "payload-preferences") return [];
let relation: Relationship;
try {
relation = await findRelationByID(slug, doc.id);
} catch (e) {
relation = {
id: doc.id,
id: "",
document: {
relationTo: slug,
value: doc,
@ -91,7 +109,7 @@ const commonLogic = async (slug: keyof GeneratedTypes["collections"], doc: any)
changes.push(...getEndpointChangesFromOutgoingRelation(relation))
);
await sendWebhookMessage(uniqueBy(changes, ({ url }) => url));
return uniqueBy(changes, ({ url }) => url);
};
// -------------------------------------------------------------------------------------------------
@ -382,6 +400,10 @@ const sendWebhookMessage = async (changes: EndpointChange[]) => {
})
);
} catch (e) {
if (e instanceof Error) {
console.warn("Error while sending webhook", e.message);
} else {
console.warn("Error while sending webhook", e);
}
}
};

View File

@ -4,13 +4,14 @@ import { formatToPascalCase } from "./string";
import {
afterChangeSendChangesWebhook,
afterDeleteSendChangesWebhook,
beforeDeletePrepareChanges,
} from "../hooks/afterOperationSendChangesWebhook";
type CollectionConfigWithPlugins = CollectionConfig;
export type BuildCollectionConfig = Omit<
CollectionConfigWithPlugins,
"slug" | "typescript" | "labels" | "custom"
"slug" | "typescript" | "labels"
> & {
slug: keyof GeneratedTypes["collections"];
labels: { singular: string; plural: string };
@ -22,6 +23,7 @@ export const buildCollectionConfig = (config: BuildCollectionConfig): Collection
hooks: {
...config.hooks,
afterChange: [...(config.hooks?.afterChange ?? []), afterChangeSendChangesWebhook],
beforeDelete: [...(config.hooks?.beforeDelete ?? []), beforeDeletePrepareChanges],
afterDelete: [...(config.hooks?.afterDelete ?? []), afterDeleteSendChangesWebhook],
},
});