2018-12-27 11:54:16 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-06-28 13:24:27 +00:00
|
|
|
module.exports = Self => {
|
2018-11-27 12:28:15 +00:00
|
|
|
Self.remoteMethodCtx('new', {
|
2019-12-26 06:12:34 +00:00
|
|
|
description: 'Creates a new ticket and returns the id',
|
2018-06-28 13:24:27 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2019-12-26 06:12:34 +00:00
|
|
|
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`
|
2018-06-28 13:24:27 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'number',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2018-07-11 11:52:04 +00:00
|
|
|
path: `/new`,
|
2018-06-28 13:24:27 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-12-26 06:12:34 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-03-06 13:06:36 +00:00
|
|
|
if (!address)
|
|
|
|
throw new UserError(`This address doesn't exist`);
|
2018-06-28 13:24:27 +00:00
|
|
|
|
2019-03-14 09:43:14 +00:00
|
|
|
let agencyMode;
|
2019-12-26 06:12:34 +00:00
|
|
|
if (agencyModeId)
|
|
|
|
agencyMode = await models.AgencyMode.findById(agencyModeId);
|
2019-03-06 13:06:36 +00:00
|
|
|
|
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
|
|
|
|
2019-06-12 15:31:45 +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 {
|
2019-12-26 06:12:34 +00:00
|
|
|
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
|
|
|
|
2019-12-26 06:12:34 +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);
|
2018-09-26 13:48:33 +00:00
|
|
|
SELECT @result newTicketId;`;
|
2019-04-02 12:36:49 +00:00
|
|
|
let result = await Self.rawSql(query, [
|
2019-12-26 06:12:34 +00:00
|
|
|
clientId,
|
|
|
|
shipped,
|
|
|
|
warehouseId,
|
|
|
|
companyId || 442,
|
|
|
|
addressId,
|
|
|
|
agencyModeId || null,
|
|
|
|
routeId || null,
|
|
|
|
landed,
|
|
|
|
myUserId
|
2019-06-12 15:31:45 +00:00
|
|
|
], 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,
|
2019-12-26 06:12:34 +00:00
|
|
|
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
|
|
|
|
2019-06-12 15:31:45 +00:00
|
|
|
if (tx) await tx.commit();
|
2019-04-02 12:36:49 +00:00
|
|
|
return await ticket;
|
|
|
|
} catch (e) {
|
2019-06-12 15:31:45 +00:00
|
|
|
if (tx) await tx.rollback();
|
2019-04-02 12:36:49 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2018-06-28 13:24:27 +00:00
|
|
|
};
|
|
|
|
};
|