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
|
2023-01-31 10:17:31 +00:00
|
|
|
|
|
|
|
if (args.id) {
|
|
|
|
const beforeFixedPrice = await models.FixedPrice.findById(args.id, {fields: ['rate3']}, myOptions);
|
|
|
|
const [result] = await Self.rawSql(`SELECT vn.priceFixed_getRate2(?, ?) as rate2`,
|
|
|
|
[args.id, args.rate3], myOptions);
|
|
|
|
|
2023-01-31 10:24:09 +00:00
|
|
|
if (beforeFixedPrice.rate3 != args.rate3 && result && result.rate2)
|
2023-01-31 10:17:31 +00:00
|
|
|
args.rate2 = result.rate2;
|
|
|
|
}
|
|
|
|
|
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,
|
2023-04-20 06:24:43 +00:00
|
|
|
hasMinPrice: args.hasMinPrice
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|