117 lines
2.9 KiB
JavaScript
117 lines
2.9 KiB
JavaScript
|
module.exports = Self => {
|
||
|
Self.remoteMethod('upsertFixedPrice', {
|
||
|
description: 'Inserts or updates a fixed price for an item',
|
||
|
accessType: 'WRITE',
|
||
|
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'
|
||
|
}],
|
||
|
returns: {
|
||
|
type: 'object',
|
||
|
root: true
|
||
|
},
|
||
|
http: {
|
||
|
path: `/upsertFixedPrice`,
|
||
|
verb: 'PATCH'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Self.upsertFixedPrice = async ctx => {
|
||
|
const models = Self.app.models;
|
||
|
|
||
|
const args = ctx.args;
|
||
|
const tx = await models.Address.beginTransaction({});
|
||
|
try {
|
||
|
const options = {transaction: tx};
|
||
|
delete args.ctx; // removed unwanted data
|
||
|
|
||
|
const fixedPrice = await models.FixedPrice.upsert(args, options);
|
||
|
const targetItem = await models.Item.findById(args.itemFk, null, options);
|
||
|
await targetItem.updateAttributes({
|
||
|
minPrice: args.minPrice,
|
||
|
hasMinPrice: args.minPrice ? true : false
|
||
|
}, options);
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const result = await models.FixedPrice.findById(fixedPrice.id, filter, options);
|
||
|
const item = result.item();
|
||
|
|
||
|
for (let key of itemFields)
|
||
|
result[key] = item[key];
|
||
|
|
||
|
await tx.commit();
|
||
|
return result;
|
||
|
} catch (e) {
|
||
|
await tx.rollback();
|
||
|
throw e;
|
||
|
}
|
||
|
};
|
||
|
};
|
||
|
|