Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend transactions to cover after and beforeOperation hooks #4960

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
denolfe marked this conversation as resolved.
Show resolved Hide resolved
}) => {
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
denolfe marked this conversation as resolved.
Show resolved Hide resolved
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
Loading