#Determining the author of a specific page version

1 messages · Page 1 of 1 (latest)

alpine helm
#

Heyo 👋

I'm having a bit of an issue determining which user authored a specific document version. According to https://payloadcms.com/docs/versions/overview, it's possible: "With Versions, you can: Maintain an audit log / history of every change ever made to a document, including monitoring for what user made which change

Is there any easy way of figuring this out? Right now I'm considering creating a new field in the collection file that gets overwritten with the afterChange hook, but I'm not sure if that's the most efficient way to go about it.

rocky krakenBOT
rocky krakenBOT
sage mica
#

Hi @alpine helm, what did you end up doing to solve this ?

alpine helm
# sage mica Hi <@95694298137243648>, what did you end up doing to solve this ?

Hello! We ended up creating a new relationship field in the collection called versionAuthor which has a relationTo users. The field is hidden in the admin dashboard so it doesn't get confused with the author field (the person who originally posted the document). Then we created a CollectionBeforeChangeHook like this.

import payload from "payload";
import type {CollectionBeforeChangeHook} from "payload/types";

// Automatically replaces the versionAuthor field before a change is submitted
// in order to allow us to determine which user made a specific document version
export const replaceVersionAuthor: CollectionBeforeChangeHook = async ({
   data,
   req,
   operation,
   originalDoc
}) => {
   try {
      if(operation == "update") {
         data.versionAuthor = req.user.id;
      }
      return data;
   } catch (err: unknown) {
      payload.logger.error(`${err}`);
   }
};
sage mica
#

Thanks for that, that's a really nice way to handle it !