Fix for when after from function or change is an array

This commit is contained in:
Steve Walsh
2021-07-08 16:57:27 +01:00
parent 2dba4dae7a
commit e67175157b
6 changed files with 116 additions and 15 deletions

View File

@@ -8,7 +8,7 @@ const variablesToCheck = [
];
module.exports = class PayloadValidator {
constructor(_before) {
constructor(_before, id) {
try {
const before = clone(_before);
const {
@@ -20,6 +20,9 @@ module.exports = class PayloadValidator {
organization,
region
}
},
event: {
workers: [{ id: workerId }]
}
} = before;
this.before = before;
@@ -28,9 +31,11 @@ module.exports = class PayloadValidator {
this.conversationId = conversationId;
this.organization = organization;
this.region = region;
this.workerId = workerId.split(':::')[0];
this.nodeId = id.split(`${organization}-${this.workerId}-`)[1];
this.isValidBefore = true;
} catch (e) {
console.log('Error while instantiating class with invalid object');
console.log('Error while instantiating class with invalid object for');
console.log(e);
this.isValidBefore = false;
}
@@ -40,13 +45,19 @@ module.exports = class PayloadValidator {
return location.split('.').reduce((p, c) => (p && p[c]) || null, object);
}
verify(after) {
verify(_after) {
if (this.isValidBefore) {
try {
let after = _after;
if (Array.isArray(after)) {
after = after.find((msg) => !!msg);
}
variablesToCheck.forEach((location) => {
if (this.getValue(this.before, location) !== this.getValue(after, location)) {
const details = {
message: `msg.${location} changed from "${this.getValue(this.before, location)}" to "${this.getValue(after, location)}" for bot "${this.bot}"`
message: `msg.${location} changed from "${this.getValue(this.before, location)}" to "${this.getValue(after, location)}" for bot "${this.bot}"`,
nodeId: this.nodeId,
workerId: this.workerId
};
this.logger.error(details.message);
this.logger.app.platform.organization({

View File

@@ -209,7 +209,7 @@ module.exports = function(RED) {
try {
this.on("input", function(msg) {
try {
const payloadValidator = new PayloadValidator(msg)
const payloadValidator = new PayloadValidator(msg, this.id)
var start = process.hrtime();
sandbox.msg = msg;
const vm2Instance = new vm2.VM({ sandbox, timeout: 5000 });

View File

@@ -229,7 +229,7 @@ module.exports = function(RED) {
}
if (valid) {
this.on('input', function(msg) {
const payloadValidator = new PayloadValidator(msg)
const payloadValidator = new PayloadValidator(msg, this.id)
for (var i=0; i<this.rules.length; i++) {
if (this.rules[i].t === "move") {
var r = this.rules[i];