UNPKG

@mbc-cqrs-serverless/import

Version:
79 lines 4.5 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var CsvImportQueueEventHandler_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.CsvImportQueueEventHandler = void 0; // event/csv-job.event.handler.ts (Refactored) const core_1 = require("@mbc-cqrs-serverless/core"); const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const constant_1 = require("../constant"); const import_status_enum_1 = require("../enum/import-status.enum"); const import_module_definition_1 = require("../import.module-definition"); const import_service_1 = require("../import.service"); const import_queue_event_1 = require("./import.queue.event"); let CsvImportQueueEventHandler = CsvImportQueueEventHandler_1 = class CsvImportQueueEventHandler { constructor(configService, sfnService, importService, importStrategyMap) { this.configService = configService; this.sfnService = sfnService; this.importService = importService; this.importStrategyMap = importStrategyMap; this.logger = new common_1.Logger(CsvImportQueueEventHandler_1.name); this.csvImportArn = this.configService.get('SFN_IMPORT_CSV_ARN'); } async execute(event) { const importEntity = event.importEvent.importEntity; // This handler ONLY acts on master csv jobs and ignores all other event types. if (!importEntity.id.startsWith(`${constant_1.CSV_IMPORT_PK_PREFIX}${core_1.KEY_SEPARATOR}`)) { return; } const importKey = event.importEvent.importKey; const { key, tableName, tenantCode } = importEntity.attributes; this.logger.log(`Received master CSV job from queue: ${importEntity.id} for file ${key}`); try { await this.importService.updateStatus(importKey, import_status_enum_1.ImportStatusEnum.PROCESSING); // 1. Find the custom CSV mapper for the target table. const mapper = this.importStrategyMap.get(tableName); if (!mapper) { throw new Error(`No CSV mapping strategy found for table: ${tableName}`); } await this.sfnService.startExecution(this.csvImportArn, { ...importEntity.attributes, sourceId: importEntity.id, }, `${tenantCode}-${tableName}-${Date.now()}`); this.logger.log(`Started Step Function execution for master job ${importEntity.id}`); // The master job's status will now be updated by the Step Function itself upon completion/failure. } catch (error) { this.logger.error(`Failed to start Step Function for master job ${importEntity.id}`, error); // If starting the SFN fails, update the master job to FAILED status. await this.importService.updateStatus(importKey, import_status_enum_1.ImportStatusEnum.FAILED, { error: { message: `Failed to start Step Function: ${error.message}`, }, }); throw error; } } }; exports.CsvImportQueueEventHandler = CsvImportQueueEventHandler; exports.CsvImportQueueEventHandler = CsvImportQueueEventHandler = CsvImportQueueEventHandler_1 = __decorate([ (0, core_1.EventHandler)(import_queue_event_1.ImportQueueEvent), __param(3, (0, common_1.Inject)(import_module_definition_1.IMPORT_STRATEGY_MAP)), __metadata("design:paramtypes", [config_1.ConfigService, core_1.StepFunctionService, import_service_1.ImportService, Map]) ], CsvImportQueueEventHandler); //# sourceMappingURL=csv-import.queue.event.handler.js.map