Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import { Injectable, Logger } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { MdmExportJob } from '../../infrastructure/schemas/import-export.schema'; import { MasterDataRecordService } from './master-data-record.service'; @Injectable() export class MasterDataExportService { private readonly logger = new Logger(MasterDataExportService.name); constructor( @InjectModel(MdmExportJob.name) private readonly exportJobModel: Model<MdmExportJob>, private readonly recordService: MasterDataRecordService, ) {} async createExportJob(params: { definitionKey: string; exportFormat: string; filters?: any; selectedFields?: string[]; tenantId?: string; userId: string; }): Promise<any> { const job = await this.exportJobModel.create({ definitionKey: params.definitionKey, exportFormat: params.exportFormat, filters: params.filters || {}, selectedFields: params.selectedFields || [], status: 'queued', tenantId: params.tenantId ? new Types.ObjectId(params.tenantId) : undefined, requestedBy: new Types.ObjectId(params.userId), }); // Process export synchronously or trigger queue this.processExport(job._id.toString()).catch((err) => this.logger.error(`Export job failed: ${err.message}`), ); return job.toObject(); } private async processExport(jobId: string): Promise<void> { const job = await this.exportJobModel.findById(jobId).exec(); if (!job) return; job.status = 'processing'; await job.save(); try { const recordsData = await this.recordService.findAll({ definitionKey: job.definitionKey, scopeType: job.tenantId ? 'tenant' : 'global', scopeId: job.tenantId?.toString(), limit: 10000, }); // Format records as JSON/CSV const payload = JSON.stringify(recordsData.records, null, 2); job.status = 'completed'; job.totalRecords = recordsData.total; job.fileUrl = `data:application/json;base64,${Buffer.from(payload).toString('base64')}`; job.completedAt = new Date(); await job.save(); } catch (err: any) { job.status = 'failed'; await job.save(); throw err; } } } |