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

150 lines
4.4 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, clientId, shipped, landed, warehouseId,
companyId, addressId, agencyModeId, routeId, options) => {
const myUserId = ctx.req.accessToken.userId;
const models = Self.app.models;
const address = await models.Address.findOne({
where: {id: addressId},
fields: ['id', 'clientFk'],
include: {
relation: 'client',
scope: {
include: {
relation: 'type'
}
}
}
});
if (!address)
throw new UserError(`This address doesn't exist`);
let agencyMode;
if (agencyModeId)
agencyMode = await models.AgencyMode.findById(agencyModeId);
if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
const canCreateTicket = await models.Client.canCreateTicket(clientId);
if (!canCreateTicket)
throw new UserError(`You can't create a ticket for a inactive client`);
}
let tx;
if ((typeof options) != 'object')
options = {};
if (!options.transaction) {
tx = await Self.beginTransaction({});
options.transaction = tx;
}
try {
if (!shipped && landed) {
const shippedResult = await models.Agency.getShipped(landed,
address.id, agencyModeId, warehouseId);
shipped = shippedResult && shippedResult.shipped;
}
if (shipped && !landed) {
const landedResult = await models.Agency.getLanded(shipped,
address.id, agencyModeId, warehouseId, false);
landed = landedResult && landedResult.landed;
}
query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
SELECT @result newTicketId;`;
let result = await Self.rawSql(query, [
clientId,
shipped,
warehouseId,
companyId || 442,
addressId,
agencyModeId || null,
routeId || null,
landed,
myUserId
], options);
let ticket = await models.Ticket.findById(result[1][0].newTicketId, null, options);
let cleanInstance = JSON.parse(JSON.stringify(ticket));
let logRecord = {
originFk: cleanInstance.id,
userFk: myUserId,
action: 'create',
changedModel: 'Ticket',
changedModelId: cleanInstance.id,
oldInstance: {},
newInstance: cleanInstance
};
await models.TicketLog.create(logRecord, options);
if (tx) await tx.commit();
return await ticket;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};