115 lines
3.8 KiB
JavaScript
115 lines
3.8 KiB
JavaScript
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('new', {
|
|
description: 'Create a newticket and returns the new ID',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'params',
|
|
type: 'object',
|
|
required: true,
|
|
description: 'ClientFk, Shipped, WarehouseFk, CompanyFk, AddressFk, AgencyModeFk, RouteFk, Landed, userId',
|
|
http: {source: 'body'}
|
|
}],
|
|
returns: {
|
|
type: 'number',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/new`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.new = async(ctx, params, options) => {
|
|
let models = Self.app.models;
|
|
let address = await models.Address.findOne({
|
|
where: {id: params.addressFk},
|
|
fields: ['id', 'clientFk'],
|
|
include: [
|
|
{relation: 'client',
|
|
scope: {
|
|
include: {
|
|
relation: 'type'
|
|
}
|
|
}
|
|
}
|
|
]
|
|
});
|
|
|
|
if (!address)
|
|
throw new UserError(`This address doesn't exist`);
|
|
|
|
let agencyMode;
|
|
if (params && params.agencyModeFk)
|
|
agencyMode = await models.AgencyMode.findById(params.agencyModeFk);
|
|
|
|
if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
|
|
if (!address.client().isActive)
|
|
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 (!params.shipped && params.landed) {
|
|
const shippedResult = await models.Agency.getShipped(params.landed,
|
|
address.id, params.agencyModeFk, params.warehouseFk);
|
|
params.shipped = shippedResult && shippedResult.shipped;
|
|
}
|
|
|
|
if (params.shipped && !params.landed) {
|
|
const landedResult = await models.Agency.getLanded(params.shipped,
|
|
address.id, params.agencyModeFk, params.warehouseFk);
|
|
params.landed = landedResult && landedResult.landed;
|
|
}
|
|
|
|
if (!params.userId && ctx.req && ctx.req.accessToken.userId)
|
|
params.userId = ctx.req.accessToken.userId;
|
|
|
|
query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
|
|
SELECT @result newTicketId;`;
|
|
let result = await Self.rawSql(query, [
|
|
params.clientFk,
|
|
params.shipped,
|
|
params.warehouseFk,
|
|
params.companyFk || 442,
|
|
params.addressFk,
|
|
params.agencyModeFk || null,
|
|
params.routeFk || null,
|
|
params.landed,
|
|
params.userId
|
|
], 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: params.userId,
|
|
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;
|
|
}
|
|
};
|
|
};
|