salix/services/loopback/common/methods/ticket/new.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

let UserError = require('../../helpers').UserError;
module.exports = Self => {
Self.remoteMethod('new', {
description: 'Create a newticket and returns the new ID',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
required: true,
description: 'ClientFk, Shipped, WharehouseFk, CompanyFk, AddressFk, AgencyModeFk, RouteFk, Landed, userId',
http: {source: 'body'}
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/new`,
verb: 'post'
}
});
Self.new = async(params, transaction) => {
2018-10-10 07:59:42 +00:00
let existsAddress = await Self.app.models.Address.findOne({
where: {
id: params.addressFk,
clientFk: params.clientFk}
});
if (!existsAddress)
throw new UserError(`This address doesn't exist`);
let query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
SELECT @result newTicketId;`;
let result = await Self.rawSql(query, [
params.clientFk,
params.shipped,
params.warehouseFk,
params.companyFk | 442,
params.addressFk,
2018-10-03 12:05:25 +00:00
params.agencyModeFk | null,
params.routeFk | null,
params.landed,
params.userId
], transaction);
2018-10-10 07:59:42 +00:00
return await Self.findOne({
where: {id: result[1][0].newTicketId}
}, transaction);
};
};