233 lines
7.6 KiB
JavaScript
233 lines
7.6 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
const loggable = require('vn-loopback/util/log');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('componentUpdate', {
|
|
description: 'Save ticket sale components',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The ticket id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'clientFk',
|
|
type: 'number',
|
|
description: 'The client id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'nickname',
|
|
type: 'string',
|
|
description: 'The client nickname'
|
|
},
|
|
{
|
|
arg: 'agencyModeFk',
|
|
type: 'number',
|
|
description: 'The agencyMode id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'addressFk',
|
|
type: 'number',
|
|
description: 'The address id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'zoneFk',
|
|
type: 'number',
|
|
description: 'The zone id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'warehouseFk',
|
|
type: 'number',
|
|
description: 'The warehouse id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'companyFk',
|
|
type: 'number',
|
|
description: 'The company id',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'shipped',
|
|
type: 'date',
|
|
description: 'The shipped date',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'landed',
|
|
type: 'date',
|
|
description: 'The landing date',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'isDeleted',
|
|
type: 'boolean',
|
|
description: 'Ticket is deleted',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'option',
|
|
type: 'number',
|
|
description: 'Action id',
|
|
required: true
|
|
}],
|
|
returns: {
|
|
type: ['object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/componentUpdate`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.componentUpdate = async(ctx, options) => {
|
|
const args = ctx.args;
|
|
const myOptions = {};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const userId = ctx.req.accessToken.userId;
|
|
const models = Self.app.models;
|
|
const $t = ctx.req.__; // $translate
|
|
const isEditable = await models.Ticket.isEditable(ctx, args.id, myOptions);
|
|
|
|
if (!isEditable)
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
|
|
|
const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss', myOptions);
|
|
if (!isProductionBoss) {
|
|
const zoneShipped = await models.Agency.getShipped(args.landed, args.addressFk, args.agencyModeFk, args.warehouseFk, myOptions);
|
|
|
|
if (!zoneShipped || zoneShipped.zoneFk != args.zoneFk)
|
|
throw new UserError(`You don't have privileges to change the zone`);
|
|
}
|
|
|
|
const originalTicket = await models.Ticket.findOne({
|
|
where: {id: args.id},
|
|
fields: [
|
|
'id',
|
|
'clientFk',
|
|
'agencyModeFk',
|
|
'addressFk',
|
|
'zoneFk',
|
|
'warehouseFk',
|
|
'companyFk',
|
|
'shipped',
|
|
'landed',
|
|
'isDeleted'
|
|
],
|
|
include: [
|
|
{
|
|
relation: 'client',
|
|
scope: {
|
|
fields: 'salesPersonFk'
|
|
}
|
|
}]
|
|
}, myOptions);
|
|
|
|
const updatedTicket = Object.assign({}, args);
|
|
delete updatedTicket.ctx;
|
|
delete updatedTicket.option;
|
|
|
|
// Force to unroute ticket
|
|
const hasToBeUnrouted = true;
|
|
const query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
|
const res = await Self.rawSql(query, [
|
|
args.id,
|
|
args.clientFk,
|
|
args.nickname,
|
|
args.agencyModeFk,
|
|
args.addressFk,
|
|
args.zoneFk,
|
|
args.warehouseFk,
|
|
args.companyFk,
|
|
args.shipped,
|
|
args.landed,
|
|
args.isDeleted,
|
|
hasToBeUnrouted,
|
|
args.option
|
|
], myOptions);
|
|
|
|
if (originalTicket.addressFk != updatedTicket.addressFk && args.id) {
|
|
await models.TicketObservation.destroyAll({
|
|
ticketFk: args.id
|
|
}, myOptions);
|
|
|
|
const address = await models.Address.findOne({
|
|
where: {id: args.addressFk},
|
|
include: {
|
|
relation: 'observations'
|
|
}
|
|
}, myOptions);
|
|
const observations = address.observations();
|
|
if (observations.length) {
|
|
const clonedObservations = observations.map(observation => {
|
|
observation.ticketFk = args.id;
|
|
observation.id = undefined;
|
|
return observation;
|
|
});
|
|
|
|
await models.TicketObservation.create(clonedObservations, myOptions);
|
|
}
|
|
}
|
|
|
|
const changes = loggable.getChanges(originalTicket, updatedTicket);
|
|
const oldProperties = await loggable.translateValues(Self, changes.old);
|
|
const newProperties = await loggable.translateValues(Self, changes.new);
|
|
|
|
await models.TicketLog.create({
|
|
originFk: args.id,
|
|
userFk: userId,
|
|
action: 'update',
|
|
changedModel: 'Ticket',
|
|
changedModelId: args.id,
|
|
oldInstance: oldProperties,
|
|
newInstance: newProperties
|
|
}, myOptions);
|
|
|
|
const salesPersonId = originalTicket.client().salesPersonFk;
|
|
if (salesPersonId) {
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
let changesMade = '';
|
|
for (let change in newProperties) {
|
|
let value = newProperties[change];
|
|
let oldValue = oldProperties[change];
|
|
|
|
changesMade += `\r\n~${$t(change)}: ${oldValue}~ ➔ *${$t(change)}: ${value}*`;
|
|
}
|
|
|
|
const message = $t('Changed this data from the ticket', {
|
|
ticketId: args.id,
|
|
ticketUrl: `${origin}/#!/ticket/${args.id}/sale`,
|
|
changes: changesMade
|
|
});
|
|
await models.Chat.sendCheckingPresence(ctx, salesPersonId, message);
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return res;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|