diff --git a/db/changes/10440-fallas/00-claimState.sql b/db/changes/10440-fallas/00-claimState.sql new file mode 100644 index 000000000..329a9b977 --- /dev/null +++ b/db/changes/10440-fallas/00-claimState.sql @@ -0,0 +1,2 @@ +ALTER TABLE `vn`.`claimState` ADD `hasToNotify` TINYINT DEFAULT 0 NULL; +UPDATE `vn`.`claimState` SET `hasToNotify` = 1 WHERE `code` IN ('canceled', 'incomplete'); \ No newline at end of file diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 90ac5d69c..23f625e7f 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1700,15 +1700,15 @@ INSERT INTO `vn`.`clientSample`(`id`, `clientFk`, `typeFk`, `created`, `workerFk (4, 1102, 2, CURDATE(), 18, 18, 567), (5, 1102, 3, CURDATE(), 19, 19, 567); -INSERT INTO `vn`.`claimState`(`id`, `code`, `description`, `roleFk`, `priority`) +INSERT INTO `vn`.`claimState`(`id`, `code`, `description`, `roleFk`, `priority`, `hasToNotify`) VALUES - ( 1, 'pending', 'Pendiente', 1, 1), - ( 2, 'managed', 'Gestionado', 1, 5), - ( 3, 'resolved', 'Resuelto', 72, 7), - ( 4, 'canceled', 'Anulado', 72, 6), - ( 5, 'incomplete', 'Incompleta', 72, 3), - ( 6, 'mana', 'Mana', 1, 4), - ( 7, 'lack', 'Faltas', 1, 2); + ( 1, 'pending', 'Pendiente', 1, 1, 0), + ( 2, 'managed', 'Gestionado', 1, 5, 0), + ( 3, 'resolved', 'Resuelto', 72, 7, 0), + ( 4, 'canceled', 'Anulado', 72, 6, 1), + ( 5, 'incomplete', 'Incompleta', 72, 3, 1), + ( 6, 'mana', 'Mana', 1, 4, 0), + ( 7, 'lack', 'Faltas', 1, 2, 0); INSERT INTO `vn`.`claim`(`id`, `ticketCreated`, `claimStateFk`, `observation`, `clientFk`, `workerFk`, `responsibility`, `isChargedToMana`, `created`, `packages`) VALUES diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 447288077..b7242befb 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -70,6 +70,7 @@ "Sent units from ticket": "I sent *{{quantity}}* units of [{{concept}} ({{itemId}})]({{{itemUrl}}}) to *\"{{nickname}}\"* coming from ticket id [{{ticketId}}]({{{ticketUrl}}})", "Claim will be picked": "The product from the claim [({{claimId}})]({{{claimUrl}}}) from the client *{{clientName}}* will be picked", "Claim state has changed to incomplete": "The state of the claim [({{claimId}})]({{{claimUrl}}}) from client *{{clientName}}* has changed to *incomplete*", + "Claim state has changed to canceled": "The state of the claim [({{claimId}})]({{{claimUrl}}}) from client *{{clientName}}* has changed to *canceled*", "This ticket is not an stowaway anymore": "The ticket id [{{ticketId}}]({{{ticketUrl}}}) is not an stowaway anymore", "Customs agent is required for a non UEE member": "Customs agent is required for a non UEE member", "Incoterms is required for a non UEE member": "Incoterms is required for a non UEE member", diff --git a/loopback/locale/es.json b/loopback/locale/es.json index b71d9ec74..21abc6e9e 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -137,6 +137,7 @@ "Sent units from ticket": "Envio *{{quantity}}* unidades de [{{concept}} ({{itemId}})]({{{itemUrl}}}) a *\"{{nickname}}\"* provenientes del ticket id [{{ticketId}}]({{{ticketUrl}}})", "Claim will be picked": "Se recogerá el género de la reclamación [({{claimId}})]({{{claimUrl}}}) del cliente *{{clientName}}*", "Claim state has changed to incomplete": "Se ha cambiado el estado de la reclamación [({{claimId}})]({{{claimUrl}}}) del cliente *{{clientName}}* a *incompleta*", + "Claim state has changed to canceled": "Se ha cambiado el estado de la reclamación [({{claimId}})]({{{claimUrl}}}) del cliente *{{clientName}}* a *anulado*", "This ticket is not an stowaway anymore": "El ticket id [{{ticketId}}]({{{ticketUrl}}}) ha dejado de ser un polizón", "Client checked as validated despite of duplication": "Cliente comprobado a pesar de que existe el cliente id {{clientId}}", "ORDER_ROW_UNAVAILABLE": "No hay disponibilidad de este producto", diff --git a/modules/claim/back/methods/claim/specs/updateClaim.spec.js b/modules/claim/back/methods/claim/specs/updateClaim.spec.js index de7209c18..8d888eb40 100644 --- a/modules/claim/back/methods/claim/specs/updateClaim.spec.js +++ b/modules/claim/back/methods/claim/specs/updateClaim.spec.js @@ -47,7 +47,7 @@ describe('Update Claim', () => { expect(error.message).toEqual(`You don't have enough privileges to change that field`); }); - it(`should success to update the claim within privileges `, async() => { + it(`should success to update the claimState to 'canceled' and send a rocket message`, async() => { const tx = await app.models.Claim.beginTransaction({}); try { @@ -55,13 +55,15 @@ describe('Update Claim', () => { const newClaim = await app.models.Claim.create(originalData, options); + const chatModel = app.models.Chat; + spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + const canceledState = 4; const claimManagerId = 72; const ctx = { req: { - accessToken: { - userId: claimManagerId - } + accessToken: {userId: claimManagerId}, + headers: {origin: 'http://localhost'} }, args: { observation: 'valid observation', @@ -69,11 +71,56 @@ describe('Update Claim', () => { hasToPickUp: false } }; + ctx.req.__ = (value, params) => { + return params.nickname; + }; await app.models.Claim.updateClaim(ctx, newClaim.id, options); let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options); expect(updatedClaim.observation).toEqual(ctx.args.observation); + expect(chatModel.sendCheckingPresence).toHaveBeenCalled(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it(`should success to update the claimState to 'incomplete' and send a rocket message`, async() => { + const tx = await app.models.Claim.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const newClaim = await app.models.Claim.create(originalData, options); + + const chatModel = app.models.Chat; + spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + + const incompleteState = 5; + const claimManagerId = 72; + const ctx = { + req: { + accessToken: {userId: claimManagerId}, + headers: {origin: 'http://localhost'} + }, + args: { + observation: 'valid observation', + claimStateFk: incompleteState, + hasToPickUp: false + } + }; + ctx.req.__ = (value, params) => { + return params.nickname; + }; + await app.models.Claim.updateClaim(ctx, newClaim.id, options); + + let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options); + + expect(updatedClaim.observation).toEqual(ctx.args.observation); + expect(chatModel.sendCheckingPresence).toHaveBeenCalled(); await tx.rollback(); } catch (e) { diff --git a/modules/claim/back/methods/claim/updateClaim.js b/modules/claim/back/methods/claim/updateClaim.js index 50ad52a6e..cc9937c19 100644 --- a/modules/claim/back/methods/claim/updateClaim.js +++ b/modules/claim/back/methods/claim/updateClaim.js @@ -96,9 +96,12 @@ module.exports = Self => { // When claimState has been changed if (args.claimStateFk) { const newState = await models.ClaimState.findById(args.claimStateFk, null, options); - - if (newState.code == 'incomplete') - notifyStateChange(ctx, salesPerson.id, claim); + if (newState.hasToNotify) { + if (newState.code == 'incomplete') + notifyStateChange(ctx, salesPerson.id, claim, newState.code); + if (newState.code == 'canceled') + notifyStateChange(ctx, claim.workerFk, claim, newState.code); + } } if (tx) await tx.commit(); @@ -125,11 +128,12 @@ module.exports = Self => { return canUpdate; } - async function notifyStateChange(ctx, workerId, claim) { - const origin = ctx.req.headers.origin; + async function notifyStateChange(ctx, workerId, claim, state) { const models = Self.app.models; + const origin = ctx.req.headers.origin; const $t = ctx.req.__; // $translate - const message = $t('Claim state has changed to incomplete', { + + const message = $t(`Claim state has changed to ${state}`, { claimId: claim.id, clientName: claim.client().name, claimUrl: `${origin}/#!/claim/${claim.id}/summary` diff --git a/modules/claim/back/models/claim-state.json b/modules/claim/back/models/claim-state.json index e0a536e11..2fd6d4845 100644 --- a/modules/claim/back/models/claim-state.json +++ b/modules/claim/back/models/claim-state.json @@ -13,20 +13,24 @@ }, "properties": { "id": { - "type": "Number", + "type": "number", "id": true, "description": "Identifier" }, "code": { - "type": "String", + "type": "string", "required": true }, "description": { - "type": "String", + "type": "string", "required": true }, "priority": { - "type": "Number", + "type": "number", + "required": true + }, + "hasToNotify": { + "type": "boolean", "required": true } },