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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { EmployeeImportJob, EmployeeImportRow, EmployeeExportJob, } from './schemas/import-export.schema'; import { Employee } from '../employee/schemas/employee.schema'; import { AuditLogService } from '../../../platform/audit/audit-log.service'; import { EventBusService } from '../../../platform/events/event-bus.service'; @Injectable() export class ImportExportService { constructor( @InjectModel(EmployeeImportJob.name) private readonly importJobModel: Model<EmployeeImportJob>, @InjectModel(EmployeeImportRow.name) private readonly importRowModel: Model<EmployeeImportRow>, @InjectModel(EmployeeExportJob.name) private readonly exportJobModel: Model<EmployeeExportJob>, @InjectModel(Employee.name) private readonly employeeModel: Model<Employee>, private readonly auditLog: AuditLogService, private readonly eventBus: EventBusService, ) {} async createImportJob( tenantId: string, fileId: string, columnMapping: any, dryRun: boolean, userId: string, ) { const job = await this.importJobModel.create({ tenantId, fileId, columnMapping, dryRun, createdBy: userId, }); await this.auditLog.log({ tenantId, userId, action: 'CREATE', resource: 'EmployeeImportJob', resourceId: job.id, moduleName: 'HR', }); // In production this would enqueue a BullMQ job: hr.employee-import.validate return job; } async getImportJob(tenantId: string, jobId: string) { const job = await this.importJobModel .findOne({ tenantId, _id: jobId }) .lean() .exec(); if (!job) throw new NotFoundException('Import job not found'); return job; } async getImportErrors(tenantId: string, jobId: string): Promise<any[]> { return this.importRowModel .find({ tenantId, importJobId: jobId, status: 'FAILED' }) .lean() .exec(); } async validateImportRows(tenantId: string, jobId: string, rows: any[]) { const job = await this.importJobModel .findOne({ tenantId, _id: jobId }) .exec(); if (!job) throw new NotFoundException('Import job not found'); const results: any[] = []; for (let i = 0; i < rows.length; i++) { const row = rows[i]; const errors: string[] = []; if (!row.firstName) errors.push('firstName is required'); if (!row.lastName) errors.push('lastName is required'); if (!row.workEmail) errors.push('workEmail is required'); if (row.workEmail) { const dup = await this.employeeModel .findOne({ tenantId, workEmail: row.workEmail }) .exec(); if (dup) errors.push(`Duplicate workEmail: ${row.workEmail}`); } if (row.employeeCode) { const dup = await this.employeeModel .findOne({ tenantId, employeeCode: row.employeeCode }) .exec(); if (dup) errors.push(`Duplicate employeeCode: ${row.employeeCode}`); } const importRow = await this.importRowModel.create({ tenantId, importJobId: jobId, rowNumber: i + 1, rawData: row, parsedData: row, status: errors.length > 0 ? 'FAILED' : 'PENDING', rowErrors: errors, }); results.push(importRow); } job.totalRows = rows.length; job.failedRows = results.filter((r) => r.status === 'FAILED').length; job.status = 'VALIDATED'; await job.save(); return { jobId, totalRows: rows.length, failedRows: job.failedRows, validRows: rows.length - job.failedRows, }; } async createExportJob( tenantId: string, filters: any, selectedColumns: string[], format: string, userId: string, ) { const job = await this.exportJobModel.create({ tenantId, filters, selectedColumns, format, createdBy: userId, }); await this.auditLog.log({ tenantId, userId, action: 'CREATE', resource: 'EmployeeExportJob', resourceId: job.id, moduleName: 'HR', }); // In production this would enqueue: hr.employee-export.generate return job; } getImportTemplate(): any { return { columns: [ 'employeeCode', 'firstName', 'middleName', 'lastName', 'workEmail', 'personalEmail', 'workPhone', 'personalPhone', 'gender', 'dateOfBirth', 'nationality', 'employmentType', 'joiningDate', 'branchCode', 'departmentCode', 'designationCode', 'workMode', 'noticePeriodDays', ], sampleRow: { employeeCode: '', firstName: 'John', middleName: '', lastName: 'Doe', workEmail: 'john.doe@company.com', personalEmail: '', workPhone: '+1234567890', personalPhone: '', gender: 'Male', dateOfBirth: '1990-01-15', nationality: 'US', employmentType: 'Permanent', joiningDate: '2026-01-01', branchCode: 'HQ', departmentCode: 'ENG', designationCode: 'SE', workMode: 'On-site', noticePeriodDays: '30', }, }; } } |