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

53 lines
1.4 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('newFromTicket', {
description: 'Create a new order and returns the new ID',
accessType: 'WRITE',
accepts: [{
arg: 'ticketFk',
type: 'number',
description: 'The ticket id',
required: true
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/newFromTicket`,
verb: 'post'
}
});
Self.newFromTicket = async(ctx, ticketFk, options) => {
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const ticket = await Self.app.models.Ticket.findOne({
where: {id: ticketFk}
}, myOptions);
const landed = ticket.landed;
const addressFk = ticket.addressFk;
const agencyModeFk = ticket.agencyModeFk;
const orderID = await Self.app.models.Order.new(ctx, landed, addressFk, agencyModeFk, myOptions);
if (tx) await tx.commit();
return orderID;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};