UNPKG

@tomei/customer-base

Version:

Tomei Customer Base Package

152 lines 6.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SyncLogger = void 0; const cuid_1 = require("../../helpers/cuid"); const event_type_enum_1 = require("../../enum/event-type.enum"); const sync_logger_repository_1 = require("./sync-logger.repository"); const sync_status_enum_1 = require("../../enum/sync-status.enum"); const system_sync_policy_1 = require("../system-sync-policy"); const system_sync_policy_enum_1 = require("../../enum/system-sync-policy.enum"); const sync_queue_1 = require("../sync-queue/sync-queue"); class SyncLogger { async logSyncTargets(params) { const { CustomerId, SourceSystemCode, TargetSystemCodes, TriggeredBy, EventType, dbTransaction, } = params; for (const targetSystemCode of TargetSystemCodes) { try { if (targetSystemCode === SourceSystemCode) continue; const policy = await system_sync_policy_1.SystemSyncPolicy.get(SourceSystemCode, targetSystemCode, EventType); const isAllowed = !!policy && !((EventType === event_type_enum_1.EventTypeEnum.Insert && policy.AllowInsertYN !== system_sync_policy_enum_1.IsPolicyAllowInsertEnum.Y) || (EventType === event_type_enum_1.EventTypeEnum.Update && policy.AllowUpdateYN !== system_sync_policy_enum_1.IsPolicyAllowUpdateEnum.Y)); if (!isAllowed) { console.info('[SyncLogger.logSyncTargets] skipped by policy', { CustomerId, SourceSystemCode, TargetSystemCode: targetSystemCode, EventType, }); continue; } const logEntry = { SyncLogId: (0, cuid_1.default)(), CustomerId, SourceSystemCode, TargetSystemCode: targetSystemCode, EventType, TrigerredBy: TriggeredBy, Status: sync_status_enum_1.SyncStatusEnum.Pending, CreatedAt: new Date(), UpdatedAt: new Date(), }; await SyncLogger._Repository.create(logEntry, dbTransaction); await this.enqueueForLog(logEntry.SyncLogId, CustomerId, targetSystemCode, dbTransaction); } catch (err) { console.error('[SyncLogger.logSyncTargets] outer failure', { CustomerId, SourceSystemCode, EventType, error: err === null || err === void 0 ? void 0 : err.message, }); throw err; } } } async enqueueForLog(syncLogId, customerId, targetSystemCode, dbTransaction) { const doEnqueue = async () => { try { const job = await sync_queue_1.syncQueue.add('syncCustomer', { syncLogId, customerId, targetSystemCode, }); await SyncLogger._Repository.update({ QueueJobId: String(job.id), LastStatus: sync_status_enum_1.SyncStatusEnum.Pending, LastAttemptAt: new Date(), }, { where: { SyncLogId: syncLogId }, }); } catch (err) { await SyncLogger._Repository.update({ LastErrorMessage: String(err), LastAttemptAt: new Date() }, { where: { SyncLogId: syncLogId } }); console.error('[SyncLogger.logSyncTargets] enqueue failed', { SyncLogId: syncLogId, error: err === null || err === void 0 ? void 0 : err.message, }); } }; if (dbTransaction) { dbTransaction.afterCommit(() => { void doEnqueue(); }); } else { await doEnqueue(); } } async markSuccess(syncLogId, transaction) { try { await SyncLogger._Repository.update({ Status: sync_status_enum_1.SyncStatusEnum.Success, LastStatus: sync_status_enum_1.SyncStatusEnum.Success, SyncedAt: new Date(), LastAttemptAt: new Date(), LastErrorMessage: null, }, { where: { SyncLogId: syncLogId }, transaction }); } catch (err) { console.error('[SyncLogger.markSuccess] failed', { syncLogId, error: err === null || err === void 0 ? void 0 : err.message, }); throw err; } } async markFailure(syncLogId, error, transaction) { var _a; try { const record = await SyncLogger._Repository.findByPk(syncLogId, transaction); const currentRetryCount = (_a = record === null || record === void 0 ? void 0 : record.RetryCount) !== null && _a !== void 0 ? _a : 0; await SyncLogger._Repository.update({ Status: sync_status_enum_1.SyncStatusEnum.Failed, LastStatus: sync_status_enum_1.SyncStatusEnum.Failed, LastAttemptAt: new Date(), LastErrorMessage: error, RetryCount: currentRetryCount + 1, }, { where: { SyncLogId: syncLogId }, transaction }); } catch (err) { console.error('[SyncLogger.markFailure] failed', { syncLogId, incomingError: error, error: err === null || err === void 0 ? void 0 : err.message, }); throw err; } } async markSkipped(syncLogId, reason, transaction) { try { await SyncLogger._Repository.update({ Status: sync_status_enum_1.SyncStatusEnum.Skipped, LastStatus: sync_status_enum_1.SyncStatusEnum.Skipped, LastAttemptAt: new Date(), LastErrorMessage: reason, }, { where: { SyncLogId: syncLogId }, transaction }); } catch (err) { console.error('[SyncLogger.markSkipped] failed', { syncLogId, reason, error: err === null || err === void 0 ? void 0 : err.message, }); throw err; } } } exports.SyncLogger = SyncLogger; SyncLogger._Repository = new sync_logger_repository_1.SyncLoggerRepository(); //# sourceMappingURL=sync-logger.js.map