237 lines
7.0 KiB
Plaintext
Raw Normal View History

2016-03-18 11:12:50 +01:00
'use strict';
2016-03-25 22:22:34 +01:00
/**
* <%= filename %> service
*
* @description: A set of functions similar to controller's actions to avoid code duplication.
2016-03-25 22:22:34 +01:00
*/
// Public dependencies.
2016-03-18 11:12:50 +01:00
const _ = require('lodash');
2016-03-25 22:22:34 +01:00
// Strapi utilities.
const utils = require('strapi-bookshelf/lib/utils/');
2016-03-18 11:12:50 +01:00
module.exports = {
/**
* Promise to fetch all <%= subIdPluralized || idPluralized %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
fetchAll: (params) => {
return <%= globalID %>.forge(params).query(params).fetchAll({
withRelated: _.keys(_.groupBy(_.reject(strapi.models.<%= subId || id %>.associations, {autoPopulate: false}), 'alias'))
2016-03-18 11:12:50 +01:00
});
},
/**
* Promise to fetch a/an <%= subId || id %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
fetch: (params) => {
return <%= globalID %>.forge(_.pick(params, 'id')).fetch({
withRelated: _.keys(_.groupBy(_.reject(strapi.models.<%= subId || id %>.associations, {autoPopulate: false}), 'alias'))
2016-03-18 11:12:50 +01:00
});
},
/**
* Promise to add a/an <%= subId || id %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
add: (values) => {
return <%= globalID %>.forge(values).save();
2016-03-18 11:12:50 +01:00
},
/**
* Promise to edit a/an <%= subId || id %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
edit: (params, values) => {
return <%= globalID %>.forge(params).save(values, {path: true});
2016-03-18 11:12:50 +01:00
},
/**
* Promise to remove a/an <%= subId || id %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
remove: (params) => {
return <%= globalID %>.forge(params).destroy();
2016-03-18 11:12:50 +01:00
},
/**
* Add relation to a specific <%= subId || id %> (only from a to-many relationships).
2016-03-18 11:12:50 +01:00
*
* @return {Object}
*/
addRelation: (params, values) => {
const relation = _.find(strapi.models.<%= subId || id %>.associations, {alias: params.relation});
2016-03-18 11:12:50 +01:00
if (!_.isEmpty(relation) && _.isArray(values)) {
switch (relation.nature) {
case 'manyToOne': {
const PK = utils.getPK(_.get(relation, relation.type), undefined, strapi.models);
2016-03-18 11:12:50 +01:00
const arrayOfPromises = _.map(values, function (value) {
const parameters = {};
_.set(parameters, PK, value);
_.set(parameters, 'relation', relation.via);
return strapi.services[_.get(relation, relation.type)].editRelation(parameters, [_.get(params, 'id') || null]);
});
return Promise.all(arrayOfPromises);
}
case 'manyToMany': {
return <%= globalID %>.forge(_.omit(params, 'relation'))[params.relation]().attach(values);
}
default:
return new Error('Impossible to add relation on this type of relation');
}
}
return new Error('Relationship Not Found');
},
/**
* Edit relation to a specific <%= subId || id %>.
*
* @return {Object}
*/
editRelation: (params, values) => {
const relation = _.find(strapi.models.<%= subId || id %>.associations, {alias: params.relation});
if (!_.isEmpty(relation) && _.isArray(values)) {
switch (relation.nature) {
case 'oneWay':
case 'oneToOne':
case 'oneToMany': {
const data = _.set({}, params.relation, _.first(values) || null);
return <%= globalID %>.forge(_.omit(params, 'relation')).save(data, {path: true});
}
case 'manyToOne': {
const PK = utils.getPK(_.get(relation, relation.type), undefined, strapi.models);
const results = await <%= globalID %>.forge(_.omit(params, 'relation')).fetch({
withRelated: _.get(params, 'relation')
});
// Remove relationship between records.
const data = results.toJSON() || {};
const currentValues = _.keys(_.groupBy(_.get(data, _.get(params, 'relation')), PK));
const valuesToRemove = _.difference(currentValues, values);
const arrayOfRemovePromises = _.map(valuesToRemove, value => {
const params = {};
_.set(params, PK, value);
_.set(params, 'relation', relation.via);
return strapi.services[_.get(relation, relation.type)].editRelation(params, [null]);
});
await Promise.all(arrayOfRemovePromises);
// Add relationship between records.
const arrayOfAddPromises = _.map(values, value => {
const params = {};
_.set(params, PK, value);
_.set(params, 'relation', relation.via);
return strapi.services[_.get(relation, relation.type)].editRelation(params, [_.get(params, 'id') || null]);
});
await Promise.all(arrayOfAddPromises);
return;
}
case 'manyToMany': {
const results = <%= globalID %>.forge(_.omit(params, 'relation')).fetch({
withRelated: _.get(params, 'relation')
});
const data = results.toJSON() || {};
const PK = utils.getPK('<%= globalID %>', <%= globalID %>, strapi.models);
// Values to add
const currentValues = _.keys(_.groupBy(_.get(data, _.get(params, 'relation')), PK));
const valuesToAdd = _.difference(_.map(values, o => {
return o.toString();
}), currentValues);
try {
await <%= globalID %>.forge(_.omit(params, 'relation'))[params.relation]().attach(valuesToAdd);
} catch (err) {
return err;
}
// Values to remove
const valuesToDrop = _.difference(currentValues, _.map(values, o => {
return o.toString();
}));
try {
await <%= globalID %>.forge(_.omit(params, 'relation'))[params.relation]().detach(valuesToDrop);
} catch (err) {
return err;
}
return;
}
2016-03-18 11:12:50 +01:00
default:
return new Error('Impossible to update relation on this type of relation');
2016-03-18 11:12:50 +01:00
}
}
return new Error ('Relationship Not Found');
2016-03-18 11:12:50 +01:00
},
/**
* Promise to remove a specific entry from a specific <%= subId || id %> (only from a to-many relationships).
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
removeRelation: (params, values) => {
const relation = _.find(strapi.models.<%= subId || id %>.associations, {alias: params.relation});
2016-03-18 11:12:50 +01:00
if (!_.isEmpty(relation) && _.isArray(values)) {
switch (relation.nature) {
case 'manyToOne':
const PK = utils.getPK(_.get(relation, relation.type), undefined, strapi.models);
2016-03-18 11:12:50 +01:00
const arrayOfPromises = _.map(values, value => {
const parameters = {};
_.set(parameters, PK, value);
_.set(parameters, 'relation', relation.via);
return strapi.services[_.get(relation, relation.type)].editRelation(parameters, [null]);
});
return Promise.all(arrayOfPromises);
case 'manyToMany':
return <%= globalID %>.forge(_.omit(params, 'relation'))[params.relation]().detach(values);
default:
return new Error('Impossible to delete relation on this type of relation');
2016-03-18 11:12:50 +01:00
}
}
return new Error('Relationship Not Found');
2016-03-18 11:12:50 +01:00
}
};