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

135 lines
4.1 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',
2020-03-10 10:46:58 +00:00
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',
2020-03-10 10:46:58 +00:00
type: 'number',
description: `The warehouse id filter`,
required: true
},
{
arg: 'companyId',
2020-03-10 10:46:58 +00:00
type: 'number',
description: `The company id filter`
},
{
arg: 'addressId',
2020-03-10 10:46:58 +00:00
type: 'number',
description: `The address id filter`,
required: true
},
{
arg: 'agencyModeId',
2020-03-10 10:46:58 +00:00
type: 'any',
description: `The agencyMode id filter`
},
{
arg: 'routeId',
2020-03-10 10:46:58 +00:00
type: 'number',
description: `The route id filter`
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/new`,
verb: 'post'
}
});
2021-08-13 14:46:44 +00:00
Self.new = async(ctx, options) => {
const args = ctx.args;
2023-06-01 06:32:06 +00:00
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
2023-06-01 06:32:06 +00:00
const myOptions = {userId};
let tx;
2021-08-13 14:46:44 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
2021-08-13 14:46:44 +00:00
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
2021-08-13 14:46:44 +00:00
myOptions.transaction = tx;
}
2019-01-25 10:58:50 +00:00
2019-04-02 12:36:49 +00:00
try {
2021-08-13 14:46:44 +00:00
const address = await models.Address.findOne({
where: {id: args.addressId},
fields: ['id', 'clientFk'],
include: {
relation: 'client'
2021-08-13 14:46:44 +00:00
}
}, 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().typeFk === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
2021-08-13 14:46:44 +00:00
const canCreateTicket = await models.Client.canCreateTicket(args.clientId, myOptions);
if (!canCreateTicket)
2023-09-25 08:49:47 +00:00
throw new UserError(`You can't create a ticket for an inactive client`);
2021-08-13 14:46:44 +00:00
}
if (!args.shipped && args.landed) {
2023-05-11 06:00:49 +00:00
const shippedResult = await models.Agency.getShipped(ctx, args.landed,
2021-08-13 14:46:44 +00:00
address.id, args.agencyModeId, args.warehouseId, myOptions);
args.shipped = (shippedResult && shippedResult.shipped) || args.landed;
2019-04-02 12:36:49 +00:00
}
2019-01-25 10:58:50 +00:00
2021-08-13 14:46:44 +00:00
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;
2019-04-02 12:36:49 +00:00
}
2019-01-25 10:58:50 +00:00
query = `CALL vn.ticket_add(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
SELECT @result newTicketId;`;
2021-08-13 14:46:44 +00:00
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,
true,
2023-06-01 06:32:06 +00:00
userId
2021-08-13 14:46:44 +00:00
], myOptions);
2019-04-02 12:36:49 +00:00
2021-08-13 14:46:44 +00:00
const ticket = await models.Ticket.findById(result[1][0].newTicketId, null, myOptions);
2019-04-02 12:36:49 +00:00
if (tx) await tx.commit();
2021-08-13 14:46:44 +00:00
return ticket;
2019-04-02 12:36:49 +00:00
} catch (e) {
if (tx) await tx.rollback();
2019-04-02 12:36:49 +00:00
throw e;
}
};
};