All files / server/api/locations locations.policy.js

100% Statements 21/21
100% Branches 1/1
100% Functions 7/7
100% Lines 21/21
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1001x 1x                 1x 6x                   1x 8x                   1x 2x                   1x 7x                   1x 2x                           1x   6x 6x   6x                           1x   20x       20x 20x   20x    
const Location = require('../../models/location');
const { hasRole, jsonToColumns } = require('../utils/policy');
 
/**
 * An administrator can create a location.
 *
 * @function
 * @name canCreate
 * @memberof module:server/api/locations
 */
exports.canCreate = function(req) {
  return hasRole(req, 'admin');
};
 
/**
 * Anyone can list locations.
 *
 * @function
 * @name canList
 * @memberof module:server/api/locations
 */
exports.canList = function(req) {
  return true;
};
 
/**
 * Anyone can retrieve a location.
 *
 * @function
 * @name canRetrieve
 * @memberof module:server/api/locations
 */
exports.canRetrieve = function(req) {
  return true;
};
 
/**
 * An administrator can update a location.
 *
 * @function
 * @name canUpdate
 * @memberof module:server/api/locations
 */
exports.canUpdate = function(req) {
  return hasRole(req, 'admin');
};
 
/**
 * An administrator can destroy a location.
 *
 * @function
 * @name canDestroy
 * @memberof module:server/api/locations
 */
exports.canDestroy = function(req) {
  return hasRole(req, 'admin');
};
 
/**
 * Updates a location with the specified data.
 *
 * @function
 * @name parse
 * @memberof module:server/api/locations
 *
 * @param {object} data - The data (with camel-case property names), typically an API request body.
 * @param {Location} [location] - The location to update.
 * @returns {Location} The updated location.
 */
exports.parse = function(data, location = new Location()) {
 
  location.parseFrom(data, [ 'name', 'shortName', 'description', 'phone', 'photoUrl', 'siteUrl', 'geometry', 'properties' ]);
  location.parseFrom(data, [ 'street', 'number', 'zipCode', 'city', 'state' ], { columnPrefix: 'address_', sourcePrefix: 'address.' });
 
  return location;
};
 
/**
 * Serializes a location for API responses.
 *
 * @function
 * @name serialize
 * @memberof module:server/api/locations
 *
 * @param {Request} req - The Express request object.
 * @param {Location} location - A location record.
 * @returns {object} A serialized location.
 */
exports.serialize = function(req, location) {
 
  const serialized = {
    id: location.get('api_id')
  };
 
  location.serializeTo(serialized, [ 'name', 'short_name', 'description', 'photo_url', 'site_url', 'phone', 'geometry', 'properties', 'created_at', 'updated_at' ]);
  location.serializeTo(serialized, [ 'street', 'number', 'zip_code', 'city', 'state' ], { columnPrefix: 'address_', targetPrefix: 'address.' });
 
  return serialized;
};