57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethod('getTaxes', {
|
|
description: 'Gets the taxes of a given order',
|
|
accessType: 'READ',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'order id',
|
|
http: {source: 'path'}
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/getTaxes`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.getTaxes = async(orderFk, options) => {
|
|
const conn = Self.dataSource.connector;
|
|
const stmts = [];
|
|
let stmt;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.order');
|
|
|
|
stmt = new ParameterizedSQL(`
|
|
CREATE TEMPORARY TABLE tmp.order
|
|
(INDEX (orderFk))
|
|
ENGINE = MEMORY
|
|
SELECT ? AS orderFk`, [orderFk]);
|
|
stmts.push(stmt);
|
|
|
|
stmts.push('CALL hedera.order_getTax()');
|
|
|
|
const orderTaxIndex = stmts.push('SELECT * FROM tmp.orderAmount') - 1;
|
|
|
|
stmts.push(`
|
|
DROP TEMPORARY TABLE
|
|
tmp.order,
|
|
tmp.orderTax`);
|
|
|
|
const sql = ParameterizedSQL.join(stmts, ';');
|
|
const result = await conn.executeStmt(sql, myOptions);
|
|
|
|
return result[orderTaxIndex];
|
|
};
|
|
};
|