salix/modules/order/back/methods/order/new.js

66 lines
1.8 KiB
JavaScript

let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('new', {
description: 'Create a new order and returns the new ID',
accessType: 'WRITE',
accepts: [
{
arg: 'landed',
description: 'The landed for the order',
type: 'date',
required: true
}, {
arg: 'addressId',
description: 'The address for the order',
type: 'number',
required: true
}, {
arg: 'agencyModeId',
description: 'The agencyMode for the order',
type: 'number',
required: true
}
],
returns: {
type: 'number',
root: true
},
http: {
path: `/new`,
verb: 'post'
}
});
Self.new = async(landed, addressId, agencyModeId) => {
let address = await Self.app.models.Address.findOne({
where: {id: addressId},
fields: ['clientFk'],
include: [
{relation: 'client',
scope: {
include: {
relation: 'type'
}
}
}
]
});
if (address.client().type().code === 'normal') {
if (!address.client().isActive)
throw new UserError(`You can't create an order for a inactive client`);
}
query = `CALL vn.orderListCreate(?, ?, ?, ?);`;
[result] = await Self.rawSql(query, [
landed,
agencyModeId,
addressId,
'SALIX'
]);
return result[0].vOrderId;
};
};