166 lines
4.3 KiB
JavaScript
166 lines
4.3 KiB
JavaScript
|
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('addBuy', {
|
|
description: 'Inserts a new buy for the current entry',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The entry id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'itemFk',
|
|
type: 'number',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'quantity',
|
|
type: 'number',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'packageFk',
|
|
type: 'string',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'packing',
|
|
type: 'number',
|
|
},
|
|
{
|
|
arg: 'grouping',
|
|
type: 'number'
|
|
},
|
|
{
|
|
arg: 'weight',
|
|
type: 'number',
|
|
},
|
|
{
|
|
arg: 'stickers',
|
|
type: 'number',
|
|
},
|
|
{
|
|
arg: 'price2',
|
|
type: 'number',
|
|
},
|
|
{
|
|
arg: 'price3',
|
|
type: 'number',
|
|
},
|
|
{
|
|
arg: 'buyingValue',
|
|
type: 'number'
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/addBuy`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.addBuy = async(ctx, options) => {
|
|
const conn = Self.dataSource.connector;
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const models = Self.app.models;
|
|
|
|
ctx.args.entryFk = ctx.args.id;
|
|
|
|
// remove unwanted properties
|
|
delete ctx.args.id;
|
|
delete ctx.args.ctx;
|
|
|
|
const newBuy = await models.Buy.create(ctx.args, myOptions);
|
|
|
|
const filter = {
|
|
fields: [
|
|
'id',
|
|
'itemFk',
|
|
'stickers',
|
|
'packing',
|
|
'grouping',
|
|
'quantity',
|
|
'packageFk',
|
|
'weight',
|
|
'buyingValue',
|
|
'price2',
|
|
'price3'
|
|
],
|
|
include: {
|
|
relation: 'item',
|
|
scope: {
|
|
fields: [
|
|
'id',
|
|
'typeFk',
|
|
'name',
|
|
'size',
|
|
'minPrice',
|
|
'tag5',
|
|
'value5',
|
|
'tag6',
|
|
'value6',
|
|
'tag7',
|
|
'value7',
|
|
'tag8',
|
|
'value8',
|
|
'tag9',
|
|
'value9',
|
|
'tag10',
|
|
'value10',
|
|
'groupingMode'
|
|
],
|
|
include: {
|
|
relation: 'itemType',
|
|
scope: {
|
|
fields: ['code', 'description']
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
const stmts = [];
|
|
let stmt;
|
|
|
|
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.buyRecalc');
|
|
stmt = new ParameterizedSQL(
|
|
`CREATE TEMPORARY TABLE tmp.buyRecalc
|
|
(INDEX (id))
|
|
ENGINE = MEMORY
|
|
SELECT ? AS id`, [newBuy.id]);
|
|
|
|
stmts.push(stmt);
|
|
stmts.push('CALL buy_recalcPrices()');
|
|
|
|
const sql = ParameterizedSQL.join(stmts, ';');
|
|
await conn.executeStmt(sql, myOptions);
|
|
|
|
const buy = await models.Buy.findById(newBuy.id, filter, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return buy;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|