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

149 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
2018-11-27 12:28:15 +00:00
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: 'Number',
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},
2019-02-12 13:23:04 +00:00
fields: ['id', 'clientFk'],
2019-12-19 11:17:53 +00:00
include: {
relation: 'client',
scope: {
include: {
relation: 'type'
2019-02-06 15:37:59 +00:00
}
}
2019-12-19 11:17:53 +00:00
}
2018-10-10 07:59:42 +00:00
});
if (!address)
throw new UserError(`This address doesn't exist`);
2019-03-14 09:43:14 +00:00
let agencyMode;
if (agencyModeId)
agencyMode = await models.AgencyMode.findById(agencyModeId);
2019-05-30 06:41:08 +00:00
if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
2019-02-06 15:37:59 +00:00
if (!address.client().isActive)
throw new UserError(`You can't create a ticket for a inactive client`);
2018-11-28 11:03:48 +00:00
}
2018-11-27 12:28:15 +00:00
let tx;
if ((typeof options) != 'object')
options = {};
if (!options.transaction) {
tx = await Self.beginTransaction({});
options.transaction = tx;
}
2019-01-25 10:58:50 +00:00
2019-04-02 12:36:49 +00:00
try {
if (!shipped && landed) {
const shippedResult = await models.Agency.getShipped(landed,
address.id, agencyModeId, warehouseId);
shipped = shippedResult && shippedResult.shipped;
2019-04-02 12:36:49 +00:00
}
2019-01-25 10:58:50 +00:00
if (shipped && !landed) {
const landedResult = await models.Agency.getLanded(shipped,
address.id, agencyModeId, warehouseId);
landed = landedResult && landedResult.landed;
2019-04-02 12:36:49 +00:00
}
2019-01-25 10:58:50 +00:00
2019-04-02 12:36:49 +00:00
query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
SELECT @result newTicketId;`;
2019-04-02 12:36:49 +00:00
let result = await Self.rawSql(query, [
clientId,
shipped,
warehouseId,
companyId || 442,
addressId,
agencyModeId || null,
routeId || null,
landed,
myUserId
], options);
2019-04-02 12:36:49 +00:00
2019-06-19 12:40:47 +00:00
let ticket = await models.Ticket.findById(result[1][0].newTicketId, null, options);
2019-04-02 12:36:49 +00:00
let cleanInstance = JSON.parse(JSON.stringify(ticket));
let logRecord = {
originFk: cleanInstance.id,
userFk: myUserId,
2019-04-02 12:36:49 +00:00
action: 'create',
changedModel: 'Ticket',
changedModelId: cleanInstance.id,
oldInstance: {},
newInstance: cleanInstance
};
2019-06-19 12:40:47 +00:00
await models.TicketLog.create(logRecord, options);
2019-04-02 12:36:49 +00:00
if (tx) await tx.commit();
2019-04-02 12:36:49 +00:00
return await ticket;
} catch (e) {
if (tx) await tx.rollback();
2019-04-02 12:36:49 +00:00
throw e;
}
};
};