/**
* @fileOverview A service that handles CRUD operations for user's telephones
*
* @author Ralfs Lagzda
*
* @requires NPM:lodash
* @requires ../models/telephone
*/
const _ = require('lodash');
const Telephone = require('../models/telephone');
/**
* List all telephones
*
* @memberOf TelephoneService
* @function list
* @param {Object} params the parameters to limit the query to desired results
* @returns {Promise<Telephone[]>} Promise that eventually resolves an array
* of telephones or throws error
*/
const list = params => new Promise(async (resolve, reject) => {
try {
if (typeof (params) !== 'object' || _.isEmpty(params)) throw (Error('Invalid parameters'));
resolve(await Telephone.find(params));
} catch (err) {
reject(err);
}
});
/**
* Create a telephone
*
* @memberOf TelephoneService
* @function create
* @param {String} userId the owner of the telephone
* @param {Object} telephone the telephone object to create
* @returns {Promise<Telephone>} Promise that eventually creates and resolves
* a telephone or throws error
*/
const create = (userId, telephone) => new Promise(async (resolve, reject) => {
try {
const creatableTelephone = await Object.assign(telephone, { createdBy: userId });
resolve(await new Telephone(creatableTelephone).save());
} catch (err) {
reject(err);
}
});
/**
* Retrieves a single telephone by ID
*
* @memberOf TelephoneService
* @function read
* @param {String} telephoneId the ID of the telephone
* @returns {Promise<Telephone>} Promise that eventually resolves a telephone or throws error
*/
const read = telephoneId => new Promise(async (resolve, reject) => {
try {
resolve(await Telephone.findById(telephoneId));
} catch (err) {
reject(err);
}
});
/**
* Updates a single telephone
*
* @memberOf TelephoneService
* @function update
* @param {String} telephoneId the ID of the telephone
* @param {Object} body key/value pairs of the fields that should be changed
* @returns {Promise<Telephone>} Promise that eventually updates and
* resolves a telephone or throws error
*/
const update = (telephoneId, body) => new Promise(async (resolve, reject) => {
try {
const updatedTelephone = await Object.assign(await Telephone.findById(telephoneId), body);
resolve(await updatedTelephone.save());
} catch (err) {
reject(err);
}
});
/**
* Deletes a single telephone
*
* @memberOf TelephoneService
* @function del
* @param {String} telephoneId the ID of the telephone
* @returns {Promise} Promise that eventually deletes
* a telephone or throws error
*/
const del = telephoneId => new Promise(async (resolve, reject) => {
try {
await Telephone.remove({ _id: telephoneId });
resolve();
} catch (err) {
reject(err);
}
});
/**
* Find or Create a telephone
*
* @memberOf TelephoneService
* @function findOrCreate
* @param {String} userId the owner of the telephone
* @param {Object} telephone the telephone object to create
* @param {String} telephone.telephone the telephone string in the object
* @returns {Promise<Telephone>} eventually creates and returns a
* telephone or throws error
*/
const findOrCreate = (userId, telephone) => new Promise(async (resolve, reject) => {
try {
if (!telephone) return resolve('');
const existingTelephone = await Telephone.findOne({ telephone });
if (!existingTelephone) {
const creatableTelephone = await Object.assign({ telephone }, { createdBy: userId });
return resolve(await new Telephone(creatableTelephone).save());
}
return resolve(existingTelephone);
} catch (err) {
return reject(err);
}
});
/**
* A service that handles CRUD operations for user's telephones
* @namespace TelephoneService
*/
module.exports = {
list,
create,
read,
update,
del,
findOrCreate,
};