From f0446558d23dcd7c08d6c825e8ecb6eecb1b29ca Mon Sep 17 00:00:00 2001 From: Kendell Joseph Date: Wed, 24 Jan 2024 14:28:54 -0500 Subject: [PATCH] feat(restore): Adds beforeOperation hook --- .../collections/operations/restoreVersion.ts | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/payload/src/collections/operations/restoreVersion.ts b/packages/payload/src/collections/operations/restoreVersion.ts index b007390fef..70eda1c2b7 100644 --- a/packages/payload/src/collections/operations/restoreVersion.ts +++ b/packages/payload/src/collections/operations/restoreVersion.ts @@ -3,7 +3,7 @@ import httpStatus from 'http-status' import type { FindOneArgs } from '../../database/types' import type { PayloadRequest } from '../../express/types' -import type { Collection, TypeWithID } from '../config/types' +import type { Collection, TypeWithID, BeforeOperationHook } from '../config/types' import executeAccess from '../../auth/executeAccess' import { hasWhereAccessResult } from '../../auth/types' @@ -45,6 +45,26 @@ async function restoreVersion(args: Arguments): Prom throw new APIError('Missing ID of version to restore.', httpStatus.BAD_REQUEST) } + // TODO: https://github.com/payloadcms/payload/discussions/4901 + // ///////////////////////////////////// + // beforeOperation - Collection + // ///////////////////////////////////// + + await args.collection.config.hooks.beforeOperation.reduce( + async (priorHook: BeforeOperationHook | Promise, hook: BeforeOperationHook) => { + await priorHook + + args = + (await hook({ + args, + collection: args.collection.config, + context: args.req.context, + operation: 'update', + })) || args + }, + Promise.resolve(), + ) + // ///////////////////////////////////// // Retrieve original raw version // /////////////////////////////////////