refactor(ticketTracking.create): refs #6366 unify Ticket.state ticketTracking.create i vn.ticket_setState #1838
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
await models.Ticket.state(ctx, {
|
||||
ticketFk: ticketId,
|
||||
stateFk: deliveryState.id
|
||||
}, myOptions);
|
||||
}
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
alexm
commented
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
alexm
commented
Crec que queda mes simple com: 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();
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Este if no el acabe de vore.