Created
May 4, 2018 09:14
-
-
Save ClementWalter/9f6b1e115ae18a32aaf7e02054ad416e to your computer and use it in GitHub Desktop.
Utils for the UserStamp mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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