salix/modules/entry/back/methods/entry/editLatestBuys.js

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-08-26 14:33:47 +00:00
module.exports = Self => {
2022-04-19 12:12:31 +00:00
Self.remoteMethodCtx('editLatestBuys', {
2020-08-27 15:54:43 +00:00
description: 'Updates a column for one or more buys',
2020-08-26 14:33:47 +00:00
accessType: 'WRITE',
accepts: [{
2020-09-02 11:09:30 +00:00
arg: 'field',
2022-04-20 06:55:43 +00:00
type: 'string',
2020-09-02 11:09:30 +00:00
required: true,
description: `the column to edit`
},
{
arg: 'newValue',
2022-04-20 06:55:43 +00:00
type: 'any',
2020-08-26 14:33:47 +00:00
required: true,
2020-09-02 11:09:30 +00:00
description: `The new value to save`
2020-08-26 14:33:47 +00:00
},
{
arg: 'lines',
2022-04-20 06:55:43 +00:00
type: ['object'],
2020-08-26 14:33:47 +00:00
required: true,
description: `the buys which will be modified`
},
{
arg: 'filter',
2022-04-13 06:15:47 +00:00
type: 'object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
2020-08-26 14:33:47 +00:00
}],
returns: {
2022-04-20 06:55:43 +00:00
type: 'object',
2020-08-26 14:33:47 +00:00
root: true
},
http: {
path: `/editLatestBuys`,
verb: 'POST'
}
});
2022-04-19 12:12:31 +00:00
Self.editLatestBuys = async(ctx, field, newValue, lines, filter, options) => {
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
2020-08-26 14:33:47 +00:00
let modelName;
let identifier;
2020-08-27 15:54:43 +00:00
2020-09-02 11:09:30 +00:00
switch (field) {
2020-08-26 14:33:47 +00:00
case 'size':
2022-08-24 12:13:26 +00:00
case 'weightByPiece':
case 'description':
case 'packingOut':
2020-08-26 14:33:47 +00:00
modelName = 'Item';
identifier = 'itemFk';
break;
case 'packing':
case 'grouping':
case 'groupingMode':
case 'packageValue':
case 'weight':
modelName = 'Buy';
identifier = 'id';
}
const models = Self.app.models;
const model = models[modelName];
try {
2022-04-13 06:15:47 +00:00
const promises = [];
const value = {};
value[field] = newValue;
2022-04-19 12:12:31 +00:00
if (filter) {
ctx.args = {where: filter, limit: null};
lines = await models.Buy.latestBuysFilter(ctx, null, myOptions);
2022-04-19 12:12:31 +00:00
}
const targets = lines.map(line => {
return line[identifier];
2020-08-26 14:33:47 +00:00
});
for (let target of targets)
promises.push(model.upsertWithWhere({id: target}, value, myOptions));
const result = await Promise.all(promises);
if (tx) await tx.commit();
2020-08-26 14:33:47 +00:00
return result;
} catch (e) {
if (tx) await tx.rollback();
throw e;
2020-08-26 14:33:47 +00:00
}
};
};