salix/services/order/common/methods/new.js

52 lines
1.6 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('new', {
description: 'Create a new order and returns the new ID',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
http: {source: 'body'}
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/new`,
verb: 'post'
}
});
Self.new = async params => {
let cli = await Self.app.models.Address.findOne({where: {id: params.addressFk}, fields: 'clientFk'});
let client = await Self.app.models.Client.findOne({
where: {id: cli.clientFk},
fields: ['isTaxDataChecked', 'isFreezed', 'isActive']
});
if (client.isFreezed)
throw new Error(`You can't create an order for a frozen client`);
if (!client.isActive)
throw new Error(`You can't create an order for a inactive client`);
if (!client.isTaxDataChecked)
throw new Error(`You can't create an order for a client that doesn't has tax data verified`);
let query = `SELECT vn.clientGetDebt(?, CURDATE()) AS debt`;
let clientDebt = await Self.rawSql(query, [cli.clientFk]);
if (clientDebt[0].debt > 0)
throw new Error(`You can't create an order for a client that has a debt`);
query = `CALL vn.orderListCreate(?, ?, ?, ?);`;
result = await Self.rawSql(query, [
params.landed,
params.agencyModeFk,
params.addressFk,
"SALIX"
]);
return result[0].vOrderId;
};
};