Skip to content

Instantly share code, notes, and snippets.

@ClementWalter
Created May 4, 2018 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClementWalter/9f6b1e115ae18a32aaf7e02054ad416e to your computer and use it in GitHub Desktop.
Save ClementWalter/9f6b1e115ae18a32aaf7e02054ad416e to your computer and use it in GitHub Desktop.
Utils for the UserStamp mixin
'use strict';
module.exports = {
addUserInfoToOptions: function(context, unused, next) {
if (!context.req.currentUser || !context.req.currentUser.id) {
const noCurrentUserError = new Error(
'The request does not contain currentUser information'
);
noCurrentUserError.status = 401;
throw noCurrentUserError;
}
context.args.options.creatorId = context.req.currentUser.id;
next();
},
addUserInfoToInstance: function(options) {
return function(context, next) {
if (!options.creatorId) {
const noCurrentUserError = new Error(
'The userStamp mixin does not provides valide options'
);
noCurrentUserError.status = 500;
throw noCurrentUserError;
}
if (context.instance) {
context.instance[options.creatorId] =
context.instance[options.creatorId] || context.options.creatorId;
} else {
context.data[options.creatorId] =
context.data[options.creatorId] || context.options.creatorId;
}
next();
};
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment