Skip to content

Commit

Permalink
feat: extend transactions to cover after and beforeOperation hooks (#…
Browse files Browse the repository at this point in the history
…4960)

* feat: extend transactions to cover after and beforeOperation hooks

* feat: use transactions in refresh operation

* docs: add req to beforeOperation and afterOperation args
  • Loading branch information
DanRibbens committed Feb 2, 2024
1 parent 5d934ba commit 1e8a6b7
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 448 deletions.
2 changes: 2 additions & 0 deletions docs/hooks/collections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import { CollectionBeforeOperationHook } from 'payload/types'
const beforeOperationHook: CollectionBeforeOperationHook = async ({
args, // original arguments passed into the operation
operation, // name of the operation
req, // full express request
}) => {
return args // return modified operation arguments as necessary
}
Expand Down Expand Up @@ -209,6 +210,7 @@ import { CollectionAfterOperationHook } from 'payload/types'
const afterOperationHook: CollectionAfterOperationHook = async ({
args, // arguments passed into the operation
operation, // name of the operation
req, // full express request
result, // the result of the operation, before modifications
}) => {
return result // return modified result as necessary
Expand Down
63 changes: 32 additions & 31 deletions packages/payload/src/auth/operations/forgotPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,38 @@ async function forgotPassword(incomingArgs: Arguments): Promise<null | string> {

let args = incomingArgs

// /////////////////////////////////////
// beforeOperation - Collection
// /////////////////////////////////////

await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
await priorHook

args =
(await hook({
args,
collection: args.collection?.config,
context: args.req.context,
operation: 'forgotPassword',
})) || args
}, Promise.resolve())

const {
collection: { config: collectionConfig },
data,
disableEmail,
expiration,
req: {
payload: { config, emailOptions, sendEmail: email },
payload,
t,
},
req,
} = args

try {
const shouldCommit = await initTransaction(req)
const shouldCommit = await initTransaction(args.req)

// /////////////////////////////////////
// beforeOperation - Collection
// /////////////////////////////////////

await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
await priorHook

args =
(await hook({
args,
collection: args.collection?.config,
context: args.req.context,
operation: 'forgotPassword',
req: args.req,
})) || args
}, Promise.resolve())

const {
collection: { config: collectionConfig },
data,
disableEmail,
expiration,
req: {
payload: { config, emailOptions, sendEmail: email },
payload,
t,
},
req,
} = args

// /////////////////////////////////////
// Forget password
Expand Down Expand Up @@ -159,7 +160,7 @@ async function forgotPassword(incomingArgs: Arguments): Promise<null | string> {

return token
} catch (error: unknown) {
await killTransaction(req)
await killTransaction(args.req)
throw error
}
}
Expand Down
63 changes: 32 additions & 31 deletions packages/payload/src/auth/operations/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,38 @@ async function login<TSlug extends keyof GeneratedTypes['collections']>(
): Promise<Result & { user: GeneratedTypes['collections'][TSlug] }> {
let args = incomingArgs

// /////////////////////////////////////
// beforeOperation - Collection
// /////////////////////////////////////

await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
await priorHook

args =
(await hook({
args,
collection: args.collection?.config,
context: args.req.context,
operation: 'login',
})) || args
}, Promise.resolve())

const {
collection: { config: collectionConfig },
data,
depth,
overrideAccess,
req,
req: {
payload,
payload: { config, secret },
},
showHiddenFields,
} = args

try {
const shouldCommit = await initTransaction(req)
const shouldCommit = await initTransaction(args.req)

// /////////////////////////////////////
// beforeOperation - Collection
// /////////////////////////////////////

await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
await priorHook

args =
(await hook({
args,
collection: args.collection?.config,
context: args.req.context,
operation: 'login',
req: args.req,
})) || args
}, Promise.resolve())

const {
collection: { config: collectionConfig },
data,
depth,
overrideAccess,
req,
req: {
payload,
payload: { config, secret },
},
showHiddenFields,
} = args

// /////////////////////////////////////
// Login
Expand Down Expand Up @@ -262,7 +263,7 @@ async function login<TSlug extends keyof GeneratedTypes['collections']>(

return result
} catch (error: unknown) {
await killTransaction(req)
await killTransaction(args.req)
throw error
}
}
Expand Down
Loading

0 comments on commit 1e8a6b7

Please sign in to comment.