From 9ee05860f6a1f10aa6dbe4b40ebad3f3710b6b50 Mon Sep 17 00:00:00 2001 From: jgallego Date: Tue, 28 May 2024 12:19:54 +0200 Subject: [PATCH 1/2] feat: #6408 tests --- loopback/locale/en.json | 4 +- loopback/locale/es.json | 7 +- loopback/locale/fr.json | 7 +- loopback/locale/pt.json | 5 +- .../importToNewRefundTicket.spec.js | 1 + .../specs/setDelivered.spec.js | 5 +- .../back/methods/ticket/specs/state.spec.js | 72 ++++++++++++++++--- modules/ticket/back/methods/ticket/state.js | 58 ++++++++++++++- .../methods/zone/specs/deleteZone.spec.js | 23 +++--- 9 files changed, 146 insertions(+), 36 deletions(-) diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 412a7a17e..cb9e1d12c 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -61,7 +61,8 @@ "Changed sale discount": "I have changed the following lines discounts from the ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", "Created claim": "I have created the claim [{{claimId}}]({{{claimUrl}}}) for the following lines from the ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", "Changed sale price": "I have changed the price of [{{itemId}} {{concept}}]({{{itemUrl}}}) ({{quantity}}) from {{oldPrice}}€ ➔ *{{newPrice}}€* of the ticket [{{ticketId}}]({{{ticketUrl}}})", - "Changed sale quantity": "I have changed the quantity of [{{itemId}} {{concept}}]({{{itemUrl}}}) from {{oldQuantity}} ➔ *{{newQuantity}}* of the ticket [{{ticketId}}]({{{ticketUrl}}})", + "Changed sale quantity": "I have changed {{changes}} of the ticket [{{ticketId}}]({{{ticketUrl}}})", + "Changes in sales": "the quantity of [{{itemId}} {{concept}}]({{{itemUrl}}}) from {{oldQuantity}} ➔ *{{newQuantity}}*", "Changed sale reserved state": "I have changed the following lines reserved state from the ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", "Bought units from buy request": "Bought {{quantity}} units of [{{itemId}} {{concept}}]({{{urlItem}}}) for the ticket id [{{ticketId}}]({{{url}}})", "MESSAGE_INSURANCE_CHANGE": "I have changed the insurence credit of client [{{clientName}} ({{clientId}})]({{{url}}}) to *{{credit}} €*", @@ -230,4 +231,3 @@ "You can only have one PDA": "You can only have one PDA", "Incoterms and Customs agent are required for a non UEE member": "Incoterms and Customs agent are required for a non UEE member" } -} diff --git a/loopback/locale/es.json b/loopback/locale/es.json index fff7ebf70..5f9ec9db3 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -124,7 +124,8 @@ "Changed sale discount": "He cambiado el descuento de las siguientes lineas al ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", "Created claim": "He creado la reclamación [{{claimId}}]({{{claimUrl}}}) de las siguientes lineas del ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", "Changed sale price": "He cambiado el precio de [{{itemId}} {{concept}}]({{{itemUrl}}}) ({{quantity}}) de {{oldPrice}}€ ➔ *{{newPrice}}€* del ticket [{{ticketId}}]({{{ticketUrl}}})", - "Changed sale quantity": "He cambiado la cantidad de [{{itemId}} {{concept}}]({{{itemUrl}}}) de {{oldQuantity}} ➔ *{{newQuantity}}* del ticket [{{ticketId}}]({{{ticketUrl}}})", + "Changed sale quantity": "He cambiado {{changes}} del ticket [{{ticketId}}]({{{ticketUrl}}})", + "Changes in sales": "la cantidad de [{{itemId}} {{concept}}]({{{itemUrl}}}) de {{oldQuantity}} ➔ *{{newQuantity}}*", "State": "Estado", "regular": "normal", "reserved": "reservado", @@ -358,7 +359,7 @@ "Select ticket or client": "Elija un ticket o un client", "It was not able to create the invoice": "No se pudo crear la factura", "ticketCommercial": "El ticket {{ ticket }} para el vendedor {{ salesMan }} está en preparación. (mensaje generado automáticamente)", - "This PDA is already assigned to another user": "Este PDA ya está asignado a otro usuario", - "You can only have one PDA": "Solo puedes tener un PDA", + "This PDA is already assigned to another user": "Esta PDA ya está asignado a otro usuario", + "You can only have one PDA": "Solo puedes tener una PDA", "Incoterms and Customs agent are required for a non UEE member": "Se requieren Incoterms y agente de aduanas para un no miembro de la UEE" } diff --git a/loopback/locale/fr.json b/loopback/locale/fr.json index 44f5e35d3..6f3919e18 100644 --- a/loopback/locale/fr.json +++ b/loopback/locale/fr.json @@ -123,8 +123,9 @@ "Added sale to ticket": "J'ai ajouté la ligne suivante au ticket [{{ticketId}}]({{{ticketUrl}}}): {{{addition}}}", "Changed sale discount": "J'ai changé le rabais des lignes suivantes du ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", "Created claim": "J'ai créé la réclamation [{{claimId}}]({{{claimUrl}}}) des lignes suivantes du ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", - "Changed sale price": "J'ai changé le prix de [{{itemId}} {{concept}}]({{{itemUrl}}}) ({{quantity}}) de {{oldPrice}}€ ➔ *{{newPrice}}€* du ticket [{{ticketId}}]({{{ticketUrl}}})", - "Changed sale quantity": "J'ai changé la quantité de {{itemId}} {{concept}} de {{oldQuantity}} ➔ {{newQuantity}} du ticket [{{ticketId}}]({{{ticketUrl}}})", + "Changed sale price": " le prix de [{{itemId}} {{concept}}]({{{itemUrl}}}) ({{quantity}}) de {{oldPrice}}€ ➔ *{{newPrice}}€* du ticket [{{ticketId}}]({{{ticketUrl}}})",, + "Changed sale quantity": "J'ai changé {{changes}} du ticket [{{ticketId}}]({{{ticketUrl}}})", + "Changes in sales": "la quantité de {{itemId}} {{concept}} de {{oldQuantity}} ➔ {{newQuantity}}", "State": "État", "regular": "normal", "reserved": "réservé", @@ -357,4 +358,4 @@ "This workCenter is already assigned to this agency": "Ce centre de travail est déjà assigné à cette agence", "Select ticket or client": "Choisissez un ticket ou un client", "It was not able to create the invoice": "Il n'a pas été possible de créer la facture" -} \ No newline at end of file +} diff --git a/loopback/locale/pt.json b/loopback/locale/pt.json index b11eeefc6..3c156506c 100644 --- a/loopback/locale/pt.json +++ b/loopback/locale/pt.json @@ -124,7 +124,8 @@ "Changed sale discount": "Desconto da venda alterado no ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", "Created claim": "Reclamação criada [{{claimId}}]({{{claimUrl}}}) no ticket [{{ticketId}}]({{{ticketUrl}}}): {{{changes}}}", "Changed sale price": "Preço da venda alterado para [{{itemId}} {{concept}}]({{{itemUrl}}}) ({{quantity}}) de {{oldPrice}}€ ➔ *{{newPrice}}€* no ticket [{{ticketId}}]({{{ticketUrl}}})", - "Changed sale quantity": "Quantidade da venda alterada para [{{itemId}} {{concept}}]({{{itemUrl}}}) de {{oldQuantity}} ➔ *{{newQuantity}}* no ticket [{{ticketId}}]({{{ticketUrl}}})", + "Changed sale quantity": "Quantidade da venda alterada para {{changes}} no ticket [{{ticketId}}]({{{ticketUrl}}})", + "Changes in sales": " [{{itemId}} {{concept}}]({{{itemUrl}}}) de {{oldQuantity}} ➔ *{{newQuantity}}* ", "State": "Estado", "regular": "normal", "reserved": "reservado", @@ -357,4 +358,4 @@ "This workCenter is already assigned to this agency": "Este centro de trabalho já está atribuído a esta agência", "Select ticket or client": "Selecione um ticket ou cliente", "It was not able to create the invoice": "Não foi possível criar a fatura" -} \ No newline at end of file +} diff --git a/modules/claim/back/methods/claim-beginning/importToNewRefundTicket.spec.js b/modules/claim/back/methods/claim-beginning/importToNewRefundTicket.spec.js index b05b2ac15..156caaeec 100644 --- a/modules/claim/back/methods/claim-beginning/importToNewRefundTicket.spec.js +++ b/modules/claim/back/methods/claim-beginning/importToNewRefundTicket.spec.js @@ -6,6 +6,7 @@ describe('claimBeginning', () => { const claimManagerId = 72; const activeCtx = { accessToken: {userId: claimManagerId}, + __: value => value }; const ctx = {req: activeCtx}; diff --git a/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js index 3d37221c4..90d92dfa6 100644 --- a/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js +++ b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js @@ -2,9 +2,10 @@ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); describe('ticket setDelivered()', () => { - const userId = 50; + const userId = 49; const activeCtx = { accessToken: {userId: userId}, + __: value => value }; beforeAll(async() => { @@ -19,8 +20,6 @@ describe('ticket setDelivered()', () => { try { const options = {transaction: tx}; - const ctx = {req: {accessToken: {userId: 49}}}; - const originalTicketOne = await models.Ticket.findById(8, null, options); const originalTicketTwo = await models.Ticket.findById(10, null, options); diff --git a/modules/ticket/back/methods/ticket/specs/state.spec.js b/modules/ticket/back/methods/ticket/specs/state.spec.js index 947e72b79..d908aa2ef 100644 --- a/modules/ticket/back/methods/ticket/specs/state.spec.js +++ b/modules/ticket/back/methods/ticket/specs/state.spec.js @@ -7,6 +7,7 @@ describe('ticket state()', () => { const productionId = 49; const activeCtx = { accessToken: {userId: 9}, + __: value => value }; const ctx = {req: activeCtx}; const now = Date.vnNew(); @@ -88,7 +89,8 @@ describe('ticket state()', () => { const ticket = await models.Ticket.create(sampleTicket, options); activeCtx.accessToken.userId = productionId; - const params = {ticketFk: ticket.id, stateFk: 3}; + const stateOk = await models.State.findOne({where: {code: 'OK'}}, options); + const params = {ticketFk: ticket.id, stateFk: stateOk.id}; const ticketTracking = await models.Ticket.state(ctx, params, options); @@ -112,16 +114,68 @@ describe('ticket state()', () => { const options = {transaction: tx}; const ticket = await models.Ticket.create(sampleTicket, options); - const ctx = {req: {accessToken: {userId: 18}}}; + activeCtx.accessToken.userId = salesPersonId; const assignedState = await models.State.findOne({where: {code: 'PICKER_DESIGNED'}}, options); - const params = {ticketFk: ticket.id, stateFk: assignedState.id, userFk: 1}; - const res = await models.Ticket.state(ctx, params, options); + const paramsAssigned = {ticketFk: ticket.id, stateFk: assignedState.id, userFk: 1}; + const resAssigned = await models.Ticket.state(ctx, paramsAssigned, options); - expect(res.ticketFk).toBe(params.ticketFk); - expect(res.stateFk).toBe(params.stateFk); - expect(res.userFk).toBe(params.userFk); - expect(res.userFk).toBe(1); - expect(res.id).toBeDefined(); + expect(resAssigned.ticketFk).toBe(paramsAssigned.ticketFk); + expect(resAssigned.stateFk).toBe(paramsAssigned.stateFk); + expect(resAssigned.userFk).toBe(paramsAssigned.userFk); + expect(resAssigned.userFk).toBe(1); + expect(resAssigned.id).toBeDefined(); + + activeCtx.accessToken.userId = productionId; + const packedState = await models.State.findOne({where: {code: 'PACKED'}}, options); + const paramsPacked = {ticketFk: ticket.id, stateFk: packedState.id, userFk: salesPersonId}; + const resPacked = await models.Ticket.state(ctx, paramsPacked, options); + + expect(resPacked.stateFk).toBe(paramsPacked.stateFk); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('Should equalize the quantities of quantity and originalQuantity' + + ' if they are different', async() => { + const tx = await models.TicketTracking.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const ticket = await models.Ticket.create(sampleTicket, options); + activeCtx.accessToken.userId = salesPersonId; + + const sampleSale = { + ticketFk: ticket.id, + itemFk: 1, + concept: 'Test', + quantity: 10, + originalQuantity: 6 + }; + await models.Sale.create(sampleSale, options); + const assignedState = await models.State.findOne({where: {code: 'PICKER_DESIGNED'}}, options); + const paramsAssigned = {ticketFk: ticket.id, stateFk: assignedState.id, userFk: 1}; + const resAssigned = await models.Ticket.state(ctx, paramsAssigned, options); + + expect(resAssigned.ticketFk).toBe(paramsAssigned.ticketFk); + expect(resAssigned.stateFk).toBe(paramsAssigned.stateFk); + expect(resAssigned.userFk).toBe(paramsAssigned.userFk); + expect(resAssigned.userFk).toBe(1); + expect(resAssigned.id).toBeDefined(); + + activeCtx.accessToken.userId = productionId; + const packedState = await models.State.findOne({where: {code: 'PACKED'}}, options); + const paramsPacked = {ticketFk: ticket.id, stateFk: packedState.id, userFk: salesPersonId}; + const resPacked = await models.Ticket.state(ctx, paramsPacked, options); + + const sale = await models.Sale.findOne({where: {ticketFk: ticket.id}}, options); + + expect(resPacked.stateFk).toBe(paramsPacked.stateFk); + expect(sale.quantity).toBe(sale.originalQuantity); await tx.rollback(); } catch (e) { diff --git a/modules/ticket/back/methods/ticket/state.js b/modules/ticket/back/methods/ticket/state.js index fea9475f8..f75a98354 100644 --- a/modules/ticket/back/methods/ticket/state.js +++ b/modules/ticket/back/methods/ticket/state.js @@ -64,7 +64,63 @@ module.exports = Self => { if ((ticketState && !oldStateAllowed) || !newStateAllowed) throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED'); - await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [params.ticketFk, params.code], myOptions); + const ticket = await models.Ticket.findById(params.ticketFk, { + include: [{ + relation: 'client', + scope: { + fields: ['salesPersonFk'] + } + }], + fields: ['id', 'clientFk'] + }, myOptions); + + const salesPersonFk = ticket.client().salesPersonFk; + if (salesPersonFk) { + const sales = await Self.rawSql(` + SELECT DISTINCT s.id, + s.itemFk, + s.concept, + s.originalQuantity AS oldQuantity, + s.quantity AS newQuantity + FROM vn.sale s + JOIN vn.saleTracking st ON st.saleFk = s.id + JOIN vn.ticket t ON t.id = s.ticketFk + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.ticketState ts ON ts.ticketFk = t.id + JOIN vn.state s2 ON s2.id = ts.stateFk + WHERE s.ticketFk = ? + AND st.isChecked + AND s.originalQuantity IS NOT NULL + AND s.originalQuantity <> s.quantity + AND s2.\`order\` < (SELECT \`order\` FROM vn.state WHERE code = 'PACKING') + ORDER BY st.created DESC + `, [params.ticketFk], myOptions); + + let changes = ''; + const url = await models.Url.getUrl(); + const $t = ctx.req.__; + for (let sale of sales) { + changes += `\r\n-` + $t('Changes in sales', { + itemId: sale.itemFk, + concept: sale.concept, + oldQuantity: sale.oldQuantity, + newQuantity: sale.newQuantity, + itemUrl: `${url}item/${sale.itemFk}/summary` + }); + const currentSale = await models.Sale.findById(sale.id, null, myOptions); + await currentSale.updateAttributes({ + originalQuantity: currentSale.quantity + }, myOptions); + } + + const message = $t('Changed sale quantity', { + ticketId: ticket.id, + changes: changes, + ticketUrl: `${url}ticket/${ticket.id}/sale` + }); + await models.Chat.sendCheckingPresence(ctx, salesPersonFk, message, myOptions); + } + await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [ticket.id, params.code], myOptions); const ticketTracking = await models.TicketTracking.findOne({ where: {ticketFk: params.ticketFk}, diff --git a/modules/zone/back/methods/zone/specs/deleteZone.spec.js b/modules/zone/back/methods/zone/specs/deleteZone.spec.js index 968685fec..08dafd181 100644 --- a/modules/zone/back/methods/zone/specs/deleteZone.spec.js +++ b/modules/zone/back/methods/zone/specs/deleteZone.spec.js @@ -5,6 +5,7 @@ describe('zone deletezone()', () => { const userId = 9; const activeCtx = { accessToken: {userId: userId}, + __: value => value }; const ctx = {req: activeCtx}; const zoneId = 9; @@ -15,19 +16,15 @@ describe('zone deletezone()', () => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); - try { - const originalTickets = await models.Ticket.find({ - where: { - zoneFk: zoneId - } - }); - ticketIDs = originalTickets.map(ticket => ticket.id); - originalTicketStates = await models.TicketState.find({where: { - ticketFk: {inq: ticketIDs}, - code: 'FIXING'}}); - } catch (error) { - console.error(error); - } + const originalTickets = await models.Ticket.find({ + where: { + zoneFk: zoneId + } + }); + ticketIDs = originalTickets.map(ticket => ticket.id); + originalTicketStates = await models.TicketState.find({where: { + ticketFk: {inq: ticketIDs}, + code: 'FIXING'}}); }); it('should delete a zone and update their tickets', async() => { -- 2.40.1 From 5e55f2d92d61ff6a996753b0d21b348227fb398b Mon Sep 17 00:00:00 2001 From: jgallego Date: Thu, 30 May 2024 09:35:42 +0200 Subject: [PATCH 2/2] feat: refs # 6408 test ok --- .../back/methods/ticket-tracking/specs/setDelivered.spec.js | 1 + modules/ticket/back/methods/ticket/state.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js index 90d92dfa6..3d39ea278 100644 --- a/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js +++ b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js @@ -7,6 +7,7 @@ describe('ticket setDelivered()', () => { accessToken: {userId: userId}, __: value => value }; + const ctx = {req: activeCtx}; beforeAll(async() => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ diff --git a/modules/ticket/back/methods/ticket/state.js b/modules/ticket/back/methods/ticket/state.js index f75a98354..9b0b862f2 100644 --- a/modules/ticket/back/methods/ticket/state.js +++ b/modules/ticket/back/methods/ticket/state.js @@ -92,7 +92,7 @@ module.exports = Self => { AND st.isChecked AND s.originalQuantity IS NOT NULL AND s.originalQuantity <> s.quantity - AND s2.\`order\` < (SELECT \`order\` FROM vn.state WHERE code = 'PACKING') + AND s2.\`order\` < (SELECT \`order\` FROM vn.state WHERE code = 'CHECKED') ORDER BY st.created DESC `, [params.ticketFk], myOptions); -- 2.40.1