Get data user ID

default discord avatar
shisue
last month

Hi,



I want to make a hook that enforces a certain field array to be empty if a role is removed from a user. Only admins can remove roles, so the req.user.id cannot be used, since I want to remove the roles from the target user not the admin.



I want to achieve something like this:



export const enforceFacilitator: FieldHook<User & { id: string }> = async ({ req, data }) => { const isFacilitator = targetUser.roles.includes('facilitator'); if (!isFacilitator) { await payload.update({ collection: 'users', id: targetUser.id, data: { array: [], }, }); } }

If possible, it would be great to check if the role of facilitator was removed instead of simply checking if it is there or not (to avoid unnecessary operations). I could just check if the array is already empty before making the update though, which achieves a similar result but isn't very organized.

    Open the post
    Continue the discussion in Discord
    Like what we're doing?
    Star us on GitHub!

    Star

    Connect with the Payload Community on Discord

    Discord

    online

    Can't find what you're looking for?

    Get help straight from the Payload team with an Enterprise License.