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

152 lines
4.7 KiB
JavaScript

let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('new', {
description: 'Creates a new ticket and returns the id',
accessType: 'WRITE',
accepts: [{
arg: 'clientId',
type: 'number',
description: `The client id filter`,
required: true
},
{
arg: 'shipped',
type: 'date',
description: `The shipment date filter`
},
{
arg: 'landed',
type: 'date',
description: `The landing date filter`
},
{
arg: 'warehouseId',
type: 'number',
description: `The warehouse id filter`,
required: true
},
{
arg: 'companyId',
type: 'number',
description: `The company id filter`
},
{
arg: 'addressId',
type: 'number',
description: `The address id filter`,
required: true
},
{
arg: 'agencyModeId',
type: 'any',
description: `The agencyMode id filter`
},
{
arg: 'routeId',
type: 'number',
description: `The route id filter`
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/new`,
verb: 'post'
}
});
Self.new = async(ctx, options) => {
const args = ctx.args;
const myUserId = ctx.req.accessToken.userId;
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const address = await models.Address.findOne({
where: {id: args.addressId},
fields: ['id', 'clientFk'],
include: {
relation: 'client',
scope: {
include: {
relation: 'type'
}
}
}
}, myOptions);
if (!address)
throw new UserError(`This address doesn't exist`);
let agencyMode;
if (args.agencyModeId)
agencyMode = await models.AgencyMode.findById(args.agencyModeId, null, myOptions);
if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
const canCreateTicket = await models.Client.canCreateTicket(args.clientId, myOptions);
if (!canCreateTicket)
throw new UserError(`You can't create a ticket for a inactive client`);
}
if (!args.shipped && args.landed) {
const shippedResult = await models.Agency.getShipped(args.landed,
address.id, args.agencyModeId, args.warehouseId, myOptions);
args.shipped = (shippedResult && shippedResult.shipped) || args.landed;
}
if (args.shipped && !args.landed) {
const landedResult = await models.Agency.getLanded(args.shipped,
address.id, args.agencyModeId, args.warehouseId, false, myOptions);
args.landed = landedResult && landedResult.landed;
}
query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
SELECT @result newTicketId;`;
const result = await Self.rawSql(query, [
args.clientId,
args.shipped,
args.warehouseId,
args.companyId || 442,
args.addressId,
args.agencyModeId || null,
args.routeId || null,
args.landed,
myUserId
], myOptions);
const ticket = await models.Ticket.findById(result[1][0].newTicketId, null, myOptions);
const cleanInstance = JSON.parse(JSON.stringify(ticket));
const logRecord = {
originFk: cleanInstance.id,
userFk: myUserId,
action: 'insert',
changedModel: 'Ticket',
changedModelId: cleanInstance.id,
oldInstance: {},
newInstance: cleanInstance
};
await models.TicketLog.create(logRecord, myOptions);
if (tx) await tx.commit();
return ticket;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};