UNPKG

@fabric-es/fabric-cqrs

Version:

Hyperledger Fabric middleware for event sourcing and cqrs pattern

78 lines 3.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeEntity = exports.trackingReducer = exports.getReducer = void 0; const debug_1 = __importDefault(require("debug")); const _1 = require("."); const debug = debug_1.default('types:reducer'); const getReducer = (callback) => (history, initialState) => { return history.reduce((entity, event) => { var _a, _b, _c; const ntt = callback(entity, event); if (ntt) { if (!ntt[_1.CREATOR_FIELD] && ((_a = event.payload) === null || _a === void 0 ? void 0 : _a[_1.CREATOR_FIELD])) ntt[_1.CREATOR_FIELD] = event.payload[_1.CREATOR_FIELD]; if (!ntt[_1.CREATED_FIELD] && ((_b = event.payload) === null || _b === void 0 ? void 0 : _b[_1.CREATED_FIELD])) ntt[_1.CREATED_FIELD] = event.payload[_1.CREATED_FIELD]; if ((_c = event.payload) === null || _c === void 0 ? void 0 : _c[_1.TS_FIELD]) ntt[_1.TS_FIELD] = event.payload[_1.TS_FIELD]; } return ntt; }, initialState); }; exports.getReducer = getReducer; const trackingReducer = (commits) => { const result = commits.reduce((tracks, commit) => { var _a, _b; if (((_a = commit.events) === null || _a === void 0 ? void 0 : _a.filter((event) => event.type === _1.TRACK_EVENT).length) <= 0 && !tracks[_1.ORGAN_FIELD].includes(commit.mspId)) { tracks[_1.ORGAN_FIELD].push(commit.mspId); } (_b = commit.events) === null || _b === void 0 ? void 0 : _b.forEach((event) => { if (event.type === _1.TRACK_EVENT) { if (!tracks[_1.TRACK_FIELD][event.payload.entityName]) tracks[_1.TRACK_FIELD][event.payload.entityName] = []; tracks[_1.TRACK_FIELD][event.payload.entityName].push(commit.mspId); } }); return tracks; }, { [_1.ORGAN_FIELD]: [], [_1.TRACK_FIELD]: {}, }); debug('trackingReducer:result, %O', result); const olen = result[_1.ORGAN_FIELD].length; const tlen = Object.values(result[_1.TRACK_FIELD]).length; return olen > 0 && tlen > 0 ? result : olen > 0 ? { [_1.ORGAN_FIELD]: result[_1.ORGAN_FIELD] } : tlen > 0 ? { [_1.TRACK_FIELD]: result[_1.TRACK_FIELD] } : null; }; exports.trackingReducer = trackingReducer; const computeEntity = (commits, reducer) => { var _a; const history = []; commits.forEach(({ events }) => events.forEach((event) => history.push(event))); debug('computeEntity:history, %O', history); const state = reducer(history); debug('computeEntity:state, before appending trackingReducer, %O', state); if (state) { Object.assign(state, exports.trackingReducer(commits)); debug('computeEntity:state, after appending trackingReducer, %O', state); } else { if (commits.length === 1 && ((_a = commits[0].events) === null || _a === void 0 ? void 0 : _a.filter((event) => event.type === _1.TRACK_EVENT).length) === 1) { debug('computeEntity:commits, %O', commits); return { reduced: false }; } } return { state, reduced: true }; }; exports.computeEntity = computeEntity; //# sourceMappingURL=reducer.js.map