import type { CollectionBeforeChangeHook } from 'payload'

/**
 * Hook to automatically set the createdBy field to the current user on creation
 */
export const setCreatedBy: CollectionBeforeChangeHook = async ({ data, req, operation }) => {
  if (operation === 'create' && req.user) {
    return {
      ...data,
      createdBy: req.user.id,
    }
  }
  return data
}
