salix/modules/item/back/methods/fixed-price/upsertFixedPrice.js

128 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-01-18 13:16:39 +00:00
module.exports = Self => {
Self.remoteMethod('upsertFixedPrice', {
description: 'Inserts or updates a fixed price for an item',
accessType: 'WRITE',
2021-07-12 10:54:59 +00:00
accepts: [
{
arg: 'ctx',
type: 'object',
http: {source: 'context'}
},
{
arg: 'id',
type: 'number',
description: 'The fixed price id'
},
{
arg: 'itemFk',
type: 'number'
},
{
arg: 'warehouseFk',
type: 'number'
},
{
arg: 'started',
type: 'date'
},
{
arg: 'ended',
type: 'date'
},
{
arg: 'rate2',
type: 'number'
},
{
arg: 'rate3',
type: 'number'
},
{
arg: 'minPrice',
type: 'number'
},
{
arg: 'hasMinPrice',
type: 'any'
}
],
2021-01-18 13:16:39 +00:00
returns: {
type: 'object',
root: true
},
http: {
path: `/upsertFixedPrice`,
verb: 'PATCH'
}
});
2021-07-12 10:54:59 +00:00
Self.upsertFixedPrice = async(ctx, options) => {
2021-01-18 13:16:39 +00:00
const models = Self.app.models;
const args = ctx.args;
2021-07-12 10:54:59 +00:00
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
2021-01-18 13:16:39 +00:00
try {
delete args.ctx; // removed unwanted data
2021-07-12 13:00:20 +00:00
const fixedPrice = await models.FixedPrice.upsert(args, myOptions);
const targetItem = await models.Item.findById(args.itemFk, null, myOptions);
2021-07-12 10:54:59 +00:00
2021-01-18 13:16:39 +00:00
await targetItem.updateAttributes({
minPrice: args.minPrice,
hasMinPrice: args.minPrice ? true : false
2021-07-12 10:54:59 +00:00
}, myOptions);
2021-01-18 13:16:39 +00:00
const itemFields = [
'minPrice',
'hasMinPrice',
'name',
'subName',
'tag5',
'value5',
'tag6',
'value6',
'tag7',
'value7',
'tag8',
'value8',
'tag9',
'value9',
'tag10',
'value10'
];
const fieldsCopy = [].concat(itemFields);
const filter = {
include: {
relation: 'item',
scope: {
fields: fieldsCopy
}
}
};
2021-07-12 10:54:59 +00:00
const result = await models.FixedPrice.findById(fixedPrice.id, filter, myOptions);
2021-01-18 13:16:39 +00:00
const item = result.item();
for (let key of itemFields)
result[key] = item[key];
2021-07-12 10:54:59 +00:00
if (tx) await tx.commit();
2021-01-18 13:16:39 +00:00
return result;
} catch (e) {
2021-07-12 10:54:59 +00:00
if (tx) await tx.rollback();
2021-01-18 13:16:39 +00:00
throw e;
}
};
};