Source: relationship.service.js

/**
 *  @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,
};