'use strict'; /** * Module dependencies */ // Public dependencies. const _ = require('lodash'); // Strapi utilities. const utils = require('strapi-bookshelf/lib/utils/'); /** * A set of functions called "actions" for `<%= globalID %>` */ module.exports = { /** * Promise to fetch all <%= idPluralized %>. * * @return {Promise} */ fetchAll: params => { return new Promise((resolve, reject) => { <%= globalID %>.forge(params).query(params).fetchAll({ withRelated: _.keys(_.groupBy(_.reject(strapi.models.<%= id %>.associations, {autoPopulate: false}), 'alias')) }) .then(<%= idPluralized %> => { resolve(<%= idPluralized %>); }) .catch(err => { reject(err); }); }); }, /** * Promise to fetch a/an <%= id %>. * * @return {Promise} */ fetch: params => { return new Promise((resolve, reject) => { <%= globalID %>.forge(_.pick(params, 'id')).fetch({ withRelated: _.keys(_.groupBy(_.reject(strapi.models.<%= id %>.associations, {autoPopulate: false}), 'alias')) }) .then(<%= id %> => { resolve(<%= id %>); }) .catch(err => { reject(err); }); }); }, /** * Promise to add a/an <%= id %>. * * @return {Promise} */ add: values => { return new Promise((resolve, reject) => { <%= globalID %>.forge(values).save() .then(<%= id %> => { resolve(<%= id %>); }) .catch(err => { reject(err); }); }); }, /** * Promise to edit a/an <%= id %>. * * @return {Promise} */ edit: (params, values) => { return new Promise((resolve, reject) => { <%= globalID %>.forge(params).save(values, {path: true}) .then(<%= id %> => { resolve(<%= id %>); }) .catch(err => { reject(err); }); }); }, /** * Promise to remove a/an <%= id %>. * * @return {Promise} */ remove: params => { return new Promise((resolve, reject) => { <%= globalID %>.forge(params).destroy() .then(<%= id %> => { resolve(<%= id %>); }) .catch(err => { reject(err); }); }); }, /** * Add relation to a specific <%= id %> (only from a to-many relationships). * * @return {Object} */ addRelation: (params, values) => { return new Promise((resolve, reject) => { const relation = _.find(strapi.models.<%= id %>.associations, {alias: params.relation}); if (!_.isEmpty(relation) && _.isArray(values)) { switch (relation.nature) { case 'manyToOne': const PK = utils.getPK(_.get(relation, relation.type), undefined, strapi.models); 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]); }); Promise.all(arrayOfPromises) .then(() => { resolve(); }) .catch(err => { reject(err); }); break; case 'manyToMany': <%= globalID %>.forge(_.omit(params, 'relation'))[params.relation]().attach(values) .then(<%= id %> => { resolve(<%= id %>); }) .catch(err => { reject(err); }); break; default: reject('Impossible to add relation on this type of relation'); } } else { reject('Bad request'); } }); }, /** * Edit relation to a specific <%= id %>. * * @return {Object} */ editRelation: (params, values) => { return new Promise((resolve, reject) => { const relation = _.find(strapi.models.<%= 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); <%= globalID %>.forge(_.omit(params, 'relation')).save(data, {path: true}) .then(<%= id %> => { resolve(); }) .catch(err => { reject(err); }); break; case 'manyToOne': const PK = utils.getPK(_.get(relation, relation.type), undefined, strapi.models); <%= globalID %>.forge(_.omit(params, 'relation')).fetch({ withRelated: _.get(params, 'relation') }) .then(<%= id %> => { const data = <%= id %>.toJSON() || {}; const currentValues = _.keys(_.groupBy(_.get(data, _.get(params, 'relation')), PK)); const valuesToRemove = _.difference(currentValues, values); const arrayOfPromises = _.map(valuesToRemove, value => { const params = {}; _.set(params, PK, value); _.set(params, 'relation', relation.via); return strapi.services[_.get(relation, relation.type)].editRelation(params, [null]); }); return Promise.all(arrayOfPromises); }) .then(() => { const arrayOfPromises = _.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]); }); return Promise.all(arrayOfPromises); }) .then(() => { resolve(); }) .catch(err => { reject(err); }); break; case 'manyToMany': <%= globalID %>.forge(_.omit(params, 'relation')).fetch({ withRelated: _.get(params, 'relation') }) .then(<%= id %> => { const data = <%= id %>.toJSON() || {}; const PK = utils.getPK('<%= globalID %>', <%= globalID %>, strapi.models); const currentValues = _.keys(_.groupBy(_.get(data, _.get(params, 'relation')), PK)); const valuesToAdd = _.difference(_.map(values, o => { return o.toString(); }), currentValues); return <%= globalID %>.forge(_.omit(params, 'relation'))[params.relation]().attach(valuesToAdd) .then(function () { return <%= id %>; }) }) .then(<%= id %> => { const data = <%= id %>.toJSON() || {}; const PK = utils.getPK('<%= globalID %>', <%= globalID %>, strapi.models); const currentValues = _.keys(_.groupBy(_.get(data, _.get(params, 'relation')), PK)); const valuesToDrop = _.difference(currentValues, _.map(values, o => { return o.toString(); })); return <%= globalID %>.forge(_.omit(params, 'relation'))[params.relation]().detach(valuesToDrop); }) .then(() => { resolve(); }) .catch(err => { reject(err); }); break; default: reject('Impossible to update relation on this type of relation'); } } else { reject('Bad request'); } }); }, /** * Promise to remove a specific entry from a specific <%= id %> (only from a to-many relationships). * * @return {Promise} */ removeRelation: (params, values) => { return new Promise((resolve, reject) => { const relation = _.find(strapi.models.<%= id %>.associations, {alias: params.relation}); if (!_.isEmpty(relation) && _.isArray(values)) { switch (relation.nature) { case 'manyToOne': const PK = utils.getPK(_.get(relation, relation.type), undefined, strapi.models); 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]); }); Promise.all(arrayOfPromises) .then(() => { resolve(); }) .catch(err => { reject(err); }); break; case 'manyToMany': <%= globalID %>.forge(_.omit(params, 'relation'))[params.relation]().detach(values) .then(<%= id %> => { resolve(<%= id %>); }) .catch(err => { reject(err); }); break; default: reject('Impossible to delete relation on this type of relation'); } } else { reject('Bad request'); } }); } };