Merge pull request 'refactor(ticketTracking.create): refs #6366 unify Ticket.state ticketTracking.create i vn.ticket_setState' (!1838) from 6366-unifyTicketChangeState into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #1838
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
This commit is contained in:
Pablo Natek 2024-01-09 07:00:15 +00:00
commit dffb212722
6 changed files with 34 additions and 27 deletions

View File

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

View File

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

View File

@ -130,7 +130,17 @@ module.exports = Self => {
await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions); await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions);
const ticket = await models.Ticket.findById(ticketId, null, myOptions); const ticket = await models.Ticket.findById(ticketId, null, myOptions);
await ticket.updateAttribute('isSigned', true, 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);
await models.Ticket.state(ctx, {
ticketFk: ticketId,
stateFk: deliveryState.id
}, myOptions);
} }
if (tx) await tx.commit(); if (tx) await tx.commit();

View File

@ -45,9 +45,8 @@ describe('ticket state()', () => {
const options = {transaction: tx}; const options = {transaction: tx};
activeCtx.accessToken.userId = salesPersonId; 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(); await tx.rollback();
} catch (e) { } catch (e) {
@ -67,9 +66,8 @@ describe('ticket state()', () => {
const options = {transaction: tx}; const options = {transaction: tx};
activeCtx.accessToken.userId = employeeId; 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(); await tx.rollback();
} catch (e) { } catch (e) {

View File

@ -7,7 +7,6 @@ module.exports = Self => {
accepts: [ accepts: [
{ {
arg: 'data', arg: 'data',
description: 'Model instance data',
type: 'Object', type: 'Object',
required: true, required: true,
http: {source: 'body'} http: {source: 'body'}
@ -37,25 +36,21 @@ module.exports = Self => {
} }
try { try {
const userId = ctx.req.accessToken.userId;
if (!params.stateFk && !params.code) if (!params.stateFk && !params.code)
throw new UserError('State cannot be blank'); throw new UserError('State cannot be blank');
if (params.code) { if (params.stateFk) {
const state = await models.State.findOne({ const {code} = await models.State.findById(params.stateFk, {fields: ['code']}, myOptions);
where: {code: params.code}, params.code = code;
fields: ['id'] } else {
}, myOptions); const {id} = await models.State.findOne({where: {code: params.code}}, myOptions);
params.stateFk = id;
params.stateFk = state.id;
} }
if (!params.userFk) { if (!params.userFk) {
const worker = await models.Worker.findOne({ const worker = await models.Worker.findOne({
where: {id: userId} where: {id: ctx.req.accessToken.userId}
}, myOptions); }, myOptions);
params.userFk = worker.id; params.userFk = worker.id;
} }
@ -63,17 +58,21 @@ module.exports = Self => {
fields: ['stateFk'] fields: ['stateFk']
}, myOptions); }, myOptions);
let oldStateAllowed; const oldStateAllowed = ticketState && await models.State.isEditable(ctx, ticketState.stateFk, myOptions);
if (ticketState)
oldStateAllowed = await models.State.isEditable(ctx, ticketState.stateFk, myOptions);
const newStateAllowed = await models.State.isEditable(ctx, params.stateFk, myOptions); const newStateAllowed = await models.State.isEditable(ctx, params.stateFk, myOptions);
const isAllowed = (!ticketState || oldStateAllowed == true) && newStateAllowed == true; if ((ticketState && !oldStateAllowed) || !newStateAllowed)
if (!isAllowed)
throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED'); 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);
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(); if (tx) await tx.commit();

View File

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