All files / Zapcard_API/services relationship.service.js

57.89% Statements 22/38
100% Branches 9/9
40% Functions 4/10
60.61% Lines 20/33
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                1x 1x                 4x 4x 4x 1x   3x 3x 3x 3x 3x                                 3x 3x 3x 2x   1x                     1x                                 1x                                   1x                       1x              
/**
 *  @fileOverview A service that handles CRUD operations for relationship objects.
 *
 *  @author       Ralfs Lagzda
 *
 *  @requires     NPM:lodash
 *  @requires     ../models/relationship
 */
const _ = require('lodash');
const Relationship = require('../models/relationship');
/**
 * List all relationships
 *
 * @memberOf RelationshipService
 * @function list
 * @returns {Promise<Relationship>} eventually resolves an array of
 * relationships or throws error
 */
const list = (userId, params = {}) => new Promise(async (resolve, reject) => {
  try {
    if (_.isEmpty(params) || !_.has(params, 'sharingUser')) {
      resolve(await Relationship.find());
    } else {
      const populateParameter = (params.sharingUser === 'true') ? 'receivingUser' : 'sharingUser';
      const queryParameter = (params.sharingUser === 'true') ? 'sharingUser' : 'receivingUser';
      const query = {};
      query[queryParameter] = userId;
      resolve(await Relationship.find(query)
        .populate(populateParameter, ['name', 'about', 'profilePicture']));
    }
  } catch (err) {
    reject(err);
  }
});
/**
 * Create a relationship
 *
 * @memberOf RelationshipService
 * @function create
 * @param {String} userId the owner of the relationship
 * @param {Object} relationship the relationship object to create
 * @returns {Promise<Relationship>} Promise that eventually creates
 * and resolves a relationship or throws error
 */
const create = (userId, relationship) => new Promise(async (resolve, reject) => {
  try {
    const creatableRelationship = await Object.assign(relationship, { createdBy: userId });
    resolve(new Relationship(creatableRelationship).save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Retrieves a single relationship by ID
 *
 * @memberOf RelationshipService
 * @function read
 * @param {String} relationshipId the ID of the relationship
 * @returns {Promise<Relationship>} Promise that eventually resolves a relationship or throws error
 */
const read = relationshipId => new Promise(async (resolve, reject) => {
  try {
    resolve(await Relationship.findById(relationshipId));
  } catch (err) {
    reject(err);
  }
});
/**
 * Updates a single relationship
 *
 * @memberOf RelationshipService
 * @function update
 * @param {String} relationshipId the ID of the relationship
 * @param {Object} body key/value pairs of the fields that should be changed
 * @returns {Promise<Relationship>} Promise that eventually updates
 * and resolves a relationship or throws error
 */
const update = (relationshipId, body) => new Promise(async (resolve, reject) => {
  try {
    const relationship = await Relationship.findById(relationshipId);
    const updatedRelationship = await Object.assign(relationship, body);
    resolve(updatedRelationship.save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Deletes a single relationship
 *
 * @memberOf RelationshipService
 * @function del
 * @param {String} relationshipId the ID of the relationship
 * @returns {Promise} Promise that eventually deletes
 * a relationship or throws error
 */
const del = relationshipId => new Promise(async (resolve, reject) => {
  try {
    await Relationship.remove({ _id: relationshipId });
    resolve();
  } catch (err) {
    reject(err);
  }
});
/**
 * A service that handles CRUD operations for relationship objects.
 * @namespace RelationshipService
 */
module.exports = {
  list,
  create,
  read,
  update,
  del,
};