Merge pull request 'fixes #4928 Supplier payMethod notify' (!1202) from 4928-supplier-payMethod-notify into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #1202
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
This commit is contained in:
Alexandre Riera 2022-12-20 07:07:35 +00:00
commit 2bb37e733a
9 changed files with 173 additions and 48 deletions

View File

@ -12,14 +12,9 @@ BEGIN
* @param vAuthorFk The notification author or %NULL if there is no author
* @return The notification id
*/
DECLARE vNotificationFk INT;
SELECT id INTO vNotificationFk
FROM `notification`
WHERE `name` = vNotificationName;
INSERT INTO notificationQueue
SET notificationFk = vNotificationFk,
SET notificationFk = vNotificationName,
params = vParams,
authorFk = vAuthorFk;

View File

@ -0,0 +1,46 @@
DROP TRIGGER IF EXISTS `vn`.`supplier_beforeUpdate`;
USE `vn`;
DELIMITER $$
$$
CREATE DEFINER=`root`@`localhost` TRIGGER `vn`.`supplier_beforeUpdate`
BEFORE UPDATE ON `supplier`
FOR EACH ROW
BEGIN
DECLARE vHasChange BOOL;
DECLARE vPayMethodChanged BOOL;
DECLARE vPayMethodHasVerified BOOL;
DECLARE vParams JSON;
DECLARE vOldPayMethodName VARCHAR(20);
DECLARE vNewPayMethodName VARCHAR(20);
SELECT hasVerified INTO vPayMethodHasVerified
FROM payMethod
WHERE id = NEW.payMethodFk;
SET vPayMethodChanged = NOT(NEW.payMethodFk <=> OLD.payMethodFk);
IF vPayMethodChanged THEN
SELECT name INTO vOldPayMethodName
FROM payMethod
WHERE id = OLD.payMethodFk;
SELECT name INTO vNewPayMethodName
FROM payMethod
WHERE id = NEW.payMethodFk;
SET vParams = JSON_OBJECT(
'name', NEW.name,
'oldPayMethod', vOldPayMethodName,
'newPayMethod', vNewPayMethodName
);
SELECT util.notification_send('supplier-pay-method-update', vParams, NULL) INTO @id;
END IF;
SET vHasChange = NOT(NEW.payDemFk <=> OLD.payDemFk AND NEW.payDay <=> OLD.payDay) OR vPayMethodChanged;
IF vHasChange AND vPayMethodHasVerified THEN
SET NEW.isPayMethodChecked = FALSE;
END IF;
END$$
DELIMITER ;

View File

@ -2689,7 +2689,8 @@ INSERT INTO `util`.`notificationConfig`
INSERT INTO `util`.`notification` (`id`, `name`, `description`)
VALUES
(1, 'print-email', 'notification fixture one');
(1, 'print-email', 'notification fixture one'),
(3, 'supplier-pay-method-update', 'A supplier pay method has been updated');
INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`)
VALUES
@ -2746,3 +2747,4 @@ INSERT INTO `salix`.`url` (`appName`, `environment`, `url`)
INSERT INTO `vn`.`payDemDetail` (`id`, `detail`)
VALUES
(1, 1);

View File

@ -8,7 +8,6 @@ describe('loopback model Supplier', () => {
beforeAll(async() => {
supplierOne = await models.Supplier.findById(1);
supplierTwo = await models.Supplier.findById(442);
const activeCtx = {
accessToken: {userId: 9},
http: {
@ -23,71 +22,106 @@ describe('loopback model Supplier', () => {
});
});
afterAll(async() => {
await supplierOne.updateAttribute('payMethodFk', supplierOne.payMethodFk);
await supplierTwo.updateAttribute('payMethodFk', supplierTwo.payMethodFk);
});
describe('payMethodFk', () => {
it('should throw an error when attempting to set an invalid payMethod id in the supplier', async() => {
let error;
const expectedError = 'You can not select this payment method without a registered bankery account';
const supplier = await models.Supplier.findById(1);
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
await supplier.updateAttribute('payMethodFk', 8)
.catch(e => {
error = e;
try {
let error;
const expectedError = 'You can not select this payment method without a registered bankery account';
expect(error.message).toContain(expectedError);
});
await supplierOne.updateAttribute('payMethodFk', 8, options)
.catch(e => {
error = e;
expect(error).toBeDefined();
expect(error.message).toContain(expectedError);
});
expect(error).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should not throw if the payMethod id is valid', async() => {
let error;
const supplier = await models.Supplier.findById(442);
await supplier.updateAttribute('payMethodFk', 4)
.catch(e => {
error = e;
});
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
expect(error).not.toBeDefined();
try {
let error;
await supplierTwo.updateAttribute('payMethodFk', 4, options)
.catch(e => {
error = e;
});
expect(error).not.toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should have checked isPayMethodChecked for payMethod hasVerfified is false', async() => {
const supplier = await models.Supplier.findById(442);
await supplier.updateAttribute('isPayMethodChecked', true);
await supplier.updateAttribute('payMethodFk', 5);
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
const result = await models.Supplier.findById(442);
try {
await supplierTwo.updateAttribute('isPayMethodChecked', true, options);
await supplierTwo.updateAttribute('payMethodFk', 5, options);
expect(result.isPayMethodChecked).toEqual(true);
const result = await models.Supplier.findById(442, null, options);
expect(result.isPayMethodChecked).toEqual(true);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should have unchecked isPayMethodChecked for payMethod hasVerfified is true', async() => {
const supplier = await models.Supplier.findById(442);
await supplier.updateAttribute('isPayMethodChecked', true);
await supplier.updateAttribute('payMethodFk', 2);
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
const result = await models.Supplier.findById(442);
try {
await supplierTwo.updateAttribute('isPayMethodChecked', true, options);
await supplierTwo.updateAttribute('payMethodFk', 2, options);
expect(result.isPayMethodChecked).toEqual(false);
const result = await models.Supplier.findById(442, null, options);
expect(result.isPayMethodChecked).toEqual(false);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should have unchecked isPayMethodChecked for payDay and peyDemFk', async() => {
const supplier = await models.Supplier.findById(442);
const tx = await models.Supplier.beginTransaction({});
const options = {transaction: tx};
await supplier.updateAttribute('isPayMethodChecked', true);
await supplier.updateAttribute('payDay', 5);
const firstResult = await models.Supplier.findById(442);
try {
await supplierTwo.updateAttribute('payMethodFk', 2, options);
await supplierTwo.updateAttribute('isPayMethodChecked', true, options);
await supplierTwo.updateAttribute('payDay', 5, options);
const firstResult = await models.Supplier.findById(442, null, options);
await supplier.updateAttribute('isPayMethodChecked', true);
await supplier.updateAttribute('payDemFk', 1);
const secondResult = await models.Supplier.findById(442);
await supplierTwo.updateAttribute('isPayMethodChecked', true, options);
await supplierTwo.updateAttribute('payDemFk', 1, options);
const secondResult = await models.Supplier.findById(442, null, options);
expect(firstResult.isPayMethodChecked).toEqual(false);
expect(secondResult.isPayMethodChecked).toEqual(false);
expect(firstResult.isPayMethodChecked).toEqual(false);
expect(secondResult.isPayMethodChecked).toEqual(false);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});
});

View File

@ -0,0 +1,11 @@
const Stylesheet = require(`vn-print/core/stylesheet`);
const path = require('path');
const vnPrintPath = path.resolve('print');
module.exports = new Stylesheet([
`${vnPrintPath}/common/css/spacing.css`,
`${vnPrintPath}/common/css/misc.css`,
`${vnPrintPath}/common/css/layout.css`,
`${vnPrintPath}/common/css/email.css`])
.mergeStyles();

View File

@ -0,0 +1,3 @@
subject: Pay method updated
title: Pay method updated
description: The pay method of the supplier {0} has been updated from {1} to {2}

View File

@ -0,0 +1,3 @@
subject: Método de pago actualizado
title: Método de pago actualizado
description: Se ha actualizado el método de pago del proveedor {0} de {1} a {2}

View File

@ -0,0 +1,8 @@
<email-body v-bind="$props">
<div class="grid-row">
<div class="grid-block vn-pa-ml">
<h1>{{ $t('title') }}</h1>
<p v-html="$t('description', [name, oldPayMethod, newPayMethod])"></p>
</div>
</div>
</email-body>

View File

@ -0,0 +1,23 @@
const Component = require(`vn-print/core/component`);
const emailBody = new Component('email-body');
module.exports = {
name: 'supplier-pay-method-update',
components: {
'email-body': emailBody.build(),
},
props: {
name: {
type: String,
required: true
},
oldPayMethod: {
type: String,
required: true
},
newPayMethod: {
type: String,
required: true
}
}
};