UNPKG

@energyweb/node-red-contrib-green-proof-worker

Version:

## Peer dependencies

62 lines (61 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProcessedFilterSqlite = void 0; const tslib_1 = require("tslib"); const z = tslib_1.__importStar(require("zod")); const errors_1 = require("../errors"); const helpers_1 = require("../helpers"); const input_message_1 = require("../input-message"); const node_1 = require("../node"); const InputMessage = input_message_1.InputMessages.UnitsChanged(); const Config = z.object({ sqliteConfig: z.string(), }); const CORRECT_OUTPUT = 0; /** * Filters out unit ids that are already processed, and forwards processing. * It may happen that `changes` in TxLog is filtered to zero entries. */ const ProcessedFilterSqlite = (api) => class ProcessedFilterSqlite extends node_1.Node { constructor(config) { super(api, config, InputMessage); const parsedConfig = Config.parse(config); const configNode = this.api.getNode(parsedConfig.sqliteConfig); if (!configNode) { throw new errors_1.GGPError(errors_1.ErrorCode.SqliteConfigNotFound, {}); } this.database = configNode.database; } async onInput(message) { const db = await this.database; const unitIds = message.payload.txLog.changes.map(c => c.unitId); if (unitIds.length === 0) { this.sendBuilder(message).sendToOutput(CORRECT_OUTPUT); return; } const existingUnits = await db .selectFrom('processed') .select('unit_id') .where('unit_id', 'in', unitIds) .execute() .then(result => result.map(row => row.unit_id)); const notExistingUnits = (0, helpers_1.diff)(unitIds, existingUnits); if (existingUnits.length !== 0) { if (existingUnits.length === unitIds.length) { this.api.log(`Filtered ALL changes (unit ids: ${existingUnits.join(', ')})`); } else { this.api.log(`Filtered some changes (unit ids ${existingUnits.join(', ')})`); } } this.sendBuilder(message) .addPayload({ txLog: { ...message.payload.txLog, changes: message.payload.txLog.changes.filter(change => notExistingUnits.includes(change.unitId)) } }) .sendToOutput(CORRECT_OUTPUT); } }; exports.ProcessedFilterSqlite = ProcessedFilterSqlite;