refactor(ticketTracking.create): refs #6366 unify Ticket.state ticketTracking.create i vn.ticket_setState
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Pablo Natek 2023-11-14 14:49:54 +01:00
parent ad07c4ec97
commit 0a5e8e1902
6 changed files with 41 additions and 28 deletions

View File

@ -296,6 +296,7 @@
"Invalid NIF for VIES": "Invalid NIF for VIES",
"Ticket does not exist": "Este ticket no existe",
"Ticket is already signed": "Este ticket ya ha sido firmado",
"The DELIVERED state does not exist": "El estado DELIVERED no existe",
"Authentication failed": "Autenticación fallida",
"You can't use the same password": "No puedes usar la misma contraseña",
"You can only add negative amounts in refund tickets": "Solo se puede añadir cantidades negativas en tickets abono",

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,
workerFk: worker.id

View File

@ -130,7 +130,19 @@ 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);
if (!deliveryState)
throw new UserError('The DELIVERED state does not exist');
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) {
@ -94,10 +92,10 @@ describe('ticket state()', () => {
const ticketTracking = await models.Ticket.state(ctx, params, options);
expect(ticketTracking.__data.ticketFk).toBe(params.ticketFk);
expect(ticketTracking.__data.stateFk).toBe(params.stateFk);
expect(ticketTracking.__data.workerFk).toBe(49);
expect(ticketTracking.__data.id).toBeDefined();
expect(ticketTracking.ticketFk).toBe(params.ticketFk);
expect(ticketTracking.stateFk).toBe(params.stateFk);
expect(ticketTracking.workerFk).toBe(49);
expect(ticketTracking.id).toBeDefined();
await tx.rollback();
} catch (e) {
@ -119,11 +117,10 @@ describe('ticket state()', () => {
const params = {ticketFk: ticket.id, stateFk: assignedState.id, workerFk: 1};
const res = await models.Ticket.state(ctx, params, options);
expect(res.__data.ticketFk).toBe(params.ticketFk);
expect(res.__data.stateFk).toBe(params.stateFk);
expect(res.__data.workerFk).toBe(params.workerFk);
expect(res.__data.workerFk).toBe(1);
expect(res.__data.id).toBeDefined();
expect(res.ticketFk).toBe(params.ticketFk);
expect(res.stateFk).toBe(params.stateFk);
expect(res.workerFk).toBe(params.workerFk);
expect(res.id).toBeDefined();
await tx.rollback();
} catch (e) {

View File

@ -37,18 +37,14 @@ module.exports = Self => {
}
try {
const userId = ctx.req.accessToken.userId;
const {userId} = ctx.req.accessToken;
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;
}
if (!params.workerFk) {
@ -68,12 +64,19 @@ module.exports = Self => {
oldStateAllowed = 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 == true) && newStateAllowed == true))
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);
if (params.workerFk)
await ticketTracking.updateAttribute('workerFk', params.workerFk, 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,
workerFk: worker.id