All files / server/api/users users.api.js

100% Statements 14/14
100% Branches 0/0
100% Functions 3/3
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47          1x 1x   1x 1x 1x 1x     1x             1x 5x               1x     4x                 1x 2x 2x    
/**
 * Users management API.
 *
 * @module server/api/users
 */
const _ = require('lodash');
const serialize = require('express-serializer');
 
const User = require('../../models/user');
const { fetcher, route } = require('../utils/api');
const errors = require('../utils/errors');
const policy = require('./users.policy');
 
// API resource name (used in some API errors)
exports.resourceName = 'user';
 
/**
 * Retrieves a single user.
 *
 * @function
 */
exports.retrieve = route(async function(req, res) {
  res.send(await serialize(req, req.user, policy));
});
 
/**
 * Middleware that fetches the user identified by the ID in the URL.
 *
 * @function
 */
exports.fetchUser = fetcher({
  model: User,
  resourceName: exports.resourceName,
  coerce: id => id.toLowerCase(),
  validate: 'uuid'
});
 
/**
 * Middleware that attaches the authenticated user to the request in a similar manner to `fetchUser`.
 *
 * @function
 */
exports.fetchMe = function(req, res, next) {
  req.user = req.currentUser;
  next();
};