@fabric-es/fabric-cqrs
Version:
Hyperledger Fabric middleware for event sourcing and cqrs pattern
105 lines • 5.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNotificationCenter = void 0;
const util_1 = __importDefault(require("util"));
const utils_1 = require("../utils");
const constants_1 = require("./constants");
const pipelineExec_1 = require("./pipelineExec");
const createNotificationCenter = (client) => {
const logger = utils_1.getLogger({
name: '[query-handler] createNotificationCenter.js',
target: 'console',
});
const getPatternOrSingleKey = ({ creator, entityName, id, commitId }) => commitId
? creator && entityName && id && `n:${creator}:${entityName}:${id}:${commitId}`
: id
? creator && entityName && `n:${creator}:${entityName}:${id}:*`
: entityName
? creator && `n:${creator}:${entityName}:*`
: `n:${creator}:*`;
return {
clearNotification: async ({ creator, entityName, id, commitId }) => {
const key = getPatternOrSingleKey({ creator, entityName, id, commitId });
if (!key)
throw new Error(constants_1.INVALID_ARG);
try {
await client.redis.del(key);
return { status: 'OK', data: [key] };
}
catch (e) {
logger.error(util_1.default.format('%s, %j', constants_1.REDIS_ERR, e));
return { status: 'ERROR', errors: [e] };
}
},
clearNotifications: async ({ creator, entityName, id }) => {
const pattern = getPatternOrSingleKey({ creator, entityName, id });
if (!pattern)
throw new Error(constants_1.INVALID_ARG);
try {
const [_, keys] = await client.redis.scan(0, 'MATCH', pattern);
const errors = await pipelineExec_1.pipelineExec(client, 'DELETE', pattern).then((data) => data.map(([e]) => e));
const isError = errors.reduce((pre, cur) => pre || !!cur, false);
return isError ? { status: 'ERROR', errors } : { status: 'OK', data: keys };
}
catch (e) {
logger.error(util_1.default.format('%s, %j', constants_1.REDIS_ERR, e));
return { status: 'ERROR', errors: [e] };
}
},
notify: async ({ creator, entityName, id, commitId, expiryBySec = 86400 }) => {
const key = `n:${creator}:${entityName}:${id}:${commitId}`;
if (!key)
throw new Error(constants_1.INVALID_ARG);
try {
await client.redis.set(key, 1, 'EX', expiryBySec);
return { status: 'OK' };
}
catch (e) {
logger.error(util_1.default.format('%s, %j', constants_1.REDIS_ERR, e));
return { status: 'ERROR', errors: [e] };
}
},
getNotification: async ({ creator, entityName, id, commitId }) => {
const key = getPatternOrSingleKey({ creator, entityName, id, commitId });
if (!key)
throw new Error(constants_1.INVALID_ARG);
try {
const data = await client.redis.getset(key, '0').then((value) => ({ [key]: value }));
return { status: 'OK', data };
}
catch (e) {
logger.error(util_1.default.format('%s, %j', constants_1.REDIS_ERR, e));
return { status: 'ERROR', errors: [e] };
}
},
getNotificationsByFields: async ({ creator, entityName, id }) => {
const pattern = getPatternOrSingleKey({ creator, entityName, id });
if (!pattern)
throw new Error(constants_1.INVALID_ARG);
try {
const data = {};
const [_, keys] = await client.redis.scan(0, 'MATCH', pattern, 'COUNT', 1000);
if ((keys === null || keys === void 0 ? void 0 : keys.length) === 0)
return { status: 'OK', data: [] };
const [errors, items] = await pipelineExec_1.pipelineExec(client, 'GET', pattern).then((data) => [data.map(([e, _]) => e), data.map(([_, item]) => item)]);
if ((items === null || items === void 0 ? void 0 : items.length) !== (keys === null || keys === void 0 ? void 0 : keys.length))
return { status: 'ERROR', errors: [new Error('unexpected error')] };
for (const index in keys) {
if (Object.prototype.hasOwnProperty.call(keys, index))
data[keys[index]] = items[index];
}
const isError = errors.reduce((pre, cur) => pre || !!cur, false);
return isError ? { status: 'ERROR', errors } : { status: 'OK', data };
}
catch (e) {
logger.error(util_1.default.format('%s, %j', constants_1.REDIS_ERR, e));
return { status: 'ERROR', errors: [e] };
}
},
};
};
exports.createNotificationCenter = createNotificationCenter;
//# sourceMappingURL=createNotificationCenter.js.map