refactor(ticketTracking.create): refs #6366 unify Ticket.state ticketTracking.create i vn.ticket_setState #1838

Merged
pablone merged 11 commits from 6366-unifyTicketChangeState into dev 2024-01-09 07:00:19 +00:00
6 changed files with 34 additions and 27 deletions

View File

@ -120,7 +120,7 @@ module.exports = Self => {
observationTypeFk: obsevationType.id
}, myOptions);
await models.TicketTracking.create({
await models.Ticket.state(ctx, {
ticketFk: newRefundTicket.id,
stateFk: state.id,
userFk: worker.id

View File

@ -49,7 +49,7 @@ module.exports = Self => {
for (const id of ticketIds) {
const promise = await models.Ticket.state(ctx, {
stateFk: state.id,
workerFk: worker.id,
userFk: worker.id,
ticketFk: id
}, myOptions);
promises.push(promise);

View File

@ -130,7 +130,17 @@ module.exports = Self => {
await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions);
const ticket = await models.Ticket.findById(ticketId, null, myOptions);
await ticket.updateAttribute('isSigned', true, myOptions);
await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [ticketId, 'DELIVERED'], myOptions);
const deliveryState = await models.State.find({
where: {
code: 'DELIVERED'
}
}, options);
pablone marked this conversation as resolved
Review

Este if no el acabe de vore.

Este if no el acabe de vore.
await models.Ticket.state(ctx, {
ticketFk: ticketId,
stateFk: deliveryState.id
}, myOptions);
}
if (tx) await tx.commit();

View File

@ -45,9 +45,8 @@ describe('ticket state()', () => {
const options = {transaction: tx};
activeCtx.accessToken.userId = salesPersonId;
const params = {ticketFk: 2, stateFk: 3};
await models.Ticket.state(ctx, params, options);
await models.Ticket.state(ctx, {ticketFk: 2, stateFk: 3}, options);
await tx.rollback();
} catch (e) {
@ -67,9 +66,8 @@ describe('ticket state()', () => {
const options = {transaction: tx};
activeCtx.accessToken.userId = employeeId;
const params = {ticketFk: 11, stateFk: 13};
await models.Ticket.state(ctx, params, options);
await models.Ticket.state(ctx, {ticketFk: 11, stateFk: 13}, options);
await tx.rollback();
} catch (e) {

View File

@ -7,7 +7,6 @@ module.exports = Self => {
accepts: [
{
arg: 'data',
description: 'Model instance data',
type: 'Object',
required: true,
http: {source: 'body'}
@ -37,25 +36,21 @@ module.exports = Self => {
}
try {
const userId = ctx.req.accessToken.userId;
if (!params.stateFk && !params.code)
throw new UserError('State cannot be blank');
if (params.code) {
const state = await models.State.findOne({
where: {code: params.code},
fields: ['id']
}, myOptions);
params.stateFk = state.id;
if (params.stateFk) {
const {code} = await models.State.findById(params.stateFk, {fields: ['code']}, myOptions);
params.code = code;
} else {
const {id} = await models.State.findOne({where: {code: params.code}}, myOptions);
params.stateFk = id;
}
if (!params.userFk) {
const worker = await models.Worker.findOne({
where: {id: userId}
where: {id: ctx.req.accessToken.userId}
}, myOptions);
params.userFk = worker.id;
}
@ -63,17 +58,21 @@ module.exports = Self => {
fields: ['stateFk']
}, myOptions);
let oldStateAllowed;
if (ticketState)
oldStateAllowed = await models.State.isEditable(ctx, ticketState.stateFk, myOptions);
const oldStateAllowed = ticketState && await models.State.isEditable(ctx, ticketState.stateFk, myOptions);
pablone marked this conversation as resolved Outdated
Outdated
Review

const oldStateAllowed = ticketState && await models.State.isEditable(ctx, ticketState.stateFk, myOptions);

const oldStateAllowed = ticketState && await models.State.isEditable(ctx, ticketState.stateFk, myOptions);
const newStateAllowed = await models.State.isEditable(ctx, params.stateFk, myOptions);
const isAllowed = (!ticketState || oldStateAllowed == true) && newStateAllowed == true;
if (!isAllowed)
if ((ticketState && !oldStateAllowed) || !newStateAllowed)
throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
const ticketTracking = await models.TicketTracking.create(params, myOptions);
await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [params.ticketFk, params.code], myOptions);
pablone marked this conversation as resolved
Review

Crec que queda mes simple com:
if ((ticketState && !oldStateAllowed) || !newStateAllowed)

Crec que queda mes simple com: if ((ticketState && !oldStateAllowed) || !newStateAllowed)
const ticketTracking = await models.TicketTracking.findOne({
where: {ticketFk: params.ticketFk},
order: 'id DESC',
limit: 1
}, myOptions);
await ticketTracking.updateAttribute('userFk', params.userFk, myOptions);
if (tx) await tx.commit();

View File

@ -61,7 +61,7 @@ module.exports = Self => {
for (ticket of ticketList) {
if (ticket.ticketState().alertLevel == 0) {
promises.push(models.TicketTracking.create({
promises.push(models.Ticket.state(ctx, {
ticketFk: ticket.id,
stateFk: fixingState.id,
userFk: worker.id