From d7fd35c6f6fb60284263de3dcfdb9bac85c1b131 Mon Sep 17 00:00:00 2001 From: jgallego Date: Sat, 1 Feb 2025 14:05:45 +0100 Subject: [PATCH] refactor: refs #6802 improve SQL procedure logic and clean up code formatting --- .../vn/procedures/manaSpellers_requery.sql | 34 ++++++++------- .../methods/invoiceOut/specs/delete.spec.js | 41 +++++++++---------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/db/routines/vn/procedures/manaSpellers_requery.sql b/db/routines/vn/procedures/manaSpellers_requery.sql index 9e76364cb0..962de48041 100644 --- a/db/routines/vn/procedures/manaSpellers_requery.sql +++ b/db/routines/vn/procedures/manaSpellers_requery.sql @@ -14,24 +14,22 @@ BEGIN FROM departmentManaExcluded WHERE departmentFk = vDepartmentFk; - IF vIsDepartmentExcluded THEN - LEAVE whole_proc; + IF NOT vIsDepartmentExcluded THEN + CREATE OR REPLACE TEMPORARY TABLE tmp.client + SELECT id + FROM client + WHERE departmentFk = vDepartmentFk; + + CALL client_getMana(); + + INSERT INTO departmentMana (departmentFk, amount) + SELECT vDepartmentFk, SUM(mana) + FROM tmp.clientMana + ON DUPLICATE KEY UPDATE amount = VALUES(amount); + + DROP TEMPORARY TABLE + tmp.client, + tmp.clientMana; END IF; - - CREATE OR REPLACE TEMPORARY TABLE tmp.client - SELECT id - FROM client - WHERE departmentFk = vDepartmentFk; - - CALL client_getMana(); - - INSERT INTO departmentMana (departmentFk, amount) - SELECT vDepartmentFk, SUM(mana) - FROM tmp.clientMana - ON DUPLICATE KEY UPDATE amount = VALUES(amount); - - DROP TEMPORARY TABLE - tmp.client, - tmp.clientMana; END$$ DELIMITER ; diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/delete.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/delete.spec.js index 9d638ecf4f..4eaf9228ba 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/delete.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/delete.spec.js @@ -2,12 +2,12 @@ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); describe('invoiceOut delete()', () => { - const invoiceOutId = 2; + const invoiceOutId = 2; const userId = 106; const activeCtx = { accessToken: {userId: userId}, }; - + beforeEach(() => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx @@ -19,7 +19,7 @@ describe('invoiceOut delete()', () => { const options = {transaction: tx}; try { - const invoiceOut = await models.InvoiceOut.findById(invoiceOutId , {}, options); + const invoiceOut = await models.InvoiceOut.findById(invoiceOutId, {}, options); const tickets = await models.Ticket.find({where: {refFk: invoiceOut.ref}}, options); expect(tickets.length).toEqual(1); @@ -52,20 +52,20 @@ describe('invoiceOut delete()', () => { } }); - it('should delete the corresponding bookEntry and not notify', async () => { + it('should delete the corresponding bookEntry and not notify', async() => { const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; - try { + try { const filter = { where: { ASIEN: 2 }, fields: ['id', 'enlazadoSage'] }; - - const [beforeXdiario] = await models.Xdiario.find(filter, options) - + + const [beforeXdiario] = await models.Xdiario.find(filter, options); + const [beforeNotification] = await models.NotificationQueue.find({ where: { notificationFk: 'book-entry-deleted', @@ -74,16 +74,16 @@ describe('invoiceOut delete()', () => { }, options); expect(beforeXdiario).toBeDefined(); - expect(beforeXdiario.enlazadoSage).toBeFalsy; + expect(beforeXdiario.enlazadoSage).toBeFalsy(); expect(beforeNotification).not.toBeDefined(); await models.InvoiceOut.delete(4, options); - + const [afterXdiario] = await models.Xdiario.find({ where: { ASIEN: 2 } - }, options); + }, options); const [afterNotification] = await models.NotificationQueue.find({ where: { @@ -92,10 +92,10 @@ describe('invoiceOut delete()', () => { status: 'pending' } }, options); - + expect(afterXdiario).not.toBeDefined(); expect(afterNotification).not.toBeDefined(); - + await tx.rollback(); } catch (e) { await tx.rollback(); @@ -103,7 +103,7 @@ describe('invoiceOut delete()', () => { } }); - it('should delete the corresponding bookEntry and notify', async () => { + it('should delete the corresponding bookEntry and notify', async() => { const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; @@ -114,9 +114,9 @@ describe('invoiceOut delete()', () => { }, fields: ['id', 'enlazadoSage'] }; - - const [beforeXdiario] = await models.Xdiario.find(filter, options) - + + const [beforeXdiario] = await models.Xdiario.find(filter, options); + const [beforeNotification] = await models.NotificationQueue.find({ where: { notificationFk: 'book-entry-deleted', @@ -129,12 +129,12 @@ describe('invoiceOut delete()', () => { expect(beforeNotification).not.toBeDefined(); await models.InvoiceOut.delete(3, options); - + const [afterXdiario] = await models.Xdiario.find({ where: { ASIEN: 1 } - }, options); + }, options); const [afterNotification] = await models.NotificationQueue.find({ where: { @@ -146,12 +146,11 @@ describe('invoiceOut delete()', () => { expect(afterXdiario).not.toBeDefined(); expect(afterNotification).toBeDefined(); - + await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); - });