318 lines
9.5 KiB
Plaintext
Raw Normal View History

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