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, Video,
} from "../types/collections"; } from "../types/collections";
import { isPayloadType } from "../utils/asserts"; 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 { GeneratedTypes } from "payload";
import { uniqueBy } from "../utils/array"; import { uniqueBy } from "../utils/array";
import { GlobalAfterChangeHook } from "payload/types"; import { GlobalAfterChangeHook } from "payload/types";
@ -35,12 +39,23 @@ export const afterOutgoingRelationRemovedSendChangesWebhook = async ({
export const afterChangeSendChangesWebhook: AfterChangeHook = async ({ doc, collection }) => { export const afterChangeSendChangesWebhook: AfterChangeHook = async ({ doc, collection }) => {
if ("_status" in doc && doc._status === "draft") return doc; 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; return doc;
}; };
export const afterDeleteSendChangesWebhook: AfterDeleteHook = async ({ doc, collection }) => { export const beforeDeletePrepareChanges: BeforeDeleteHook = async ({ id, collection, context }) => {
await commonLogic(collection.slug as keyof GeneratedTypes["collections"], doc); 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; return doc;
}; };
@ -62,17 +77,20 @@ export const globalAfterChangeSendChangesWebhook: GlobalAfterChangeHook = async
return doc; return doc;
}; };
const commonLogic = async (slug: keyof GeneratedTypes["collections"], doc: any) => { const getChanges = async (
if (slug === "relationships") return doc; slug: keyof GeneratedTypes["collections"],
if (slug === "payload-migrations") return doc; doc: any
if (slug === "payload-preferences") return doc; ): Promise<EndpointChange[]> => {
if (slug === "relationships") return [];
if (slug === "payload-migrations") return [];
if (slug === "payload-preferences") return [];
let relation: Relationship; let relation: Relationship;
try { try {
relation = await findRelationByID(slug, doc.id); relation = await findRelationByID(slug, doc.id);
} catch (e) { } catch (e) {
relation = { relation = {
id: doc.id, id: "",
document: { document: {
relationTo: slug, relationTo: slug,
value: doc, value: doc,
@ -91,7 +109,7 @@ const commonLogic = async (slug: keyof GeneratedTypes["collections"], doc: any)
changes.push(...getEndpointChangesFromOutgoingRelation(relation)) 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) { } catch (e) {
console.warn("Error while sending webhook", 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 { import {
afterChangeSendChangesWebhook, afterChangeSendChangesWebhook,
afterDeleteSendChangesWebhook, afterDeleteSendChangesWebhook,
beforeDeletePrepareChanges,
} from "../hooks/afterOperationSendChangesWebhook"; } from "../hooks/afterOperationSendChangesWebhook";
type CollectionConfigWithPlugins = CollectionConfig; type CollectionConfigWithPlugins = CollectionConfig;
export type BuildCollectionConfig = Omit< export type BuildCollectionConfig = Omit<
CollectionConfigWithPlugins, CollectionConfigWithPlugins,
"slug" | "typescript" | "labels" | "custom" "slug" | "typescript" | "labels"
> & { > & {
slug: keyof GeneratedTypes["collections"]; slug: keyof GeneratedTypes["collections"];
labels: { singular: string; plural: string }; labels: { singular: string; plural: string };
@ -22,6 +23,7 @@ export const buildCollectionConfig = (config: BuildCollectionConfig): Collection
hooks: { hooks: {
...config.hooks, ...config.hooks,
afterChange: [...(config.hooks?.afterChange ?? []), afterChangeSendChangesWebhook], afterChange: [...(config.hooks?.afterChange ?? []), afterChangeSendChangesWebhook],
beforeDelete: [...(config.hooks?.beforeDelete ?? []), beforeDeletePrepareChanges],
afterDelete: [...(config.hooks?.afterDelete ?? []), afterDeleteSendChangesWebhook], afterDelete: [...(config.hooks?.afterDelete ?? []), afterDeleteSendChangesWebhook],
}, },
}); });