All files / src/domains/hr/attendance/import-export import-export.service.ts

0% Statements 0/16
0% Branches 0/6
0% Functions 0/4
0% Lines 0/13

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                                                                                                               
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { AttendanceImportJob } from '../schemas';
import { StartImportJobDto, ExportQueryDto } from './dto/import-export.dto';
import { Queue } from 'bullmq';
import { InjectQueue } from '@nestjs/bullmq';
 
@Injectable()
export class ImportExportService {
  constructor(
    @InjectModel(AttendanceImportJob.name)
    private jobModel: Model<AttendanceImportJob>,
    // @InjectQueue('attendance-import') private importQueue: Queue, // Uncomment when queue is wired
  ) {}
 
  async startImportJob(
    tenantId: string,
    startDto: StartImportJobDto,
    userId: string,
  ) {
    const job = new this.jobModel({
      ...startDto,
      tenantId,
      createdBy: userId,
      status: 'pending',
    });
    await job.save();
 
    // Add to BullMQ queue for background processing
    // await this.importQueue.add('process-import', { tenantId, jobId: job._id, fileId: startDto.fileId });
 
    return job;
  }
 
  async getJobStatus(tenantId: string, jobId: string) {
    const job = await this.jobModel.findOne({ _id: jobId, tenantId }).exec();
    if (!job) throw new NotFoundException('Import job not found');
    return job;
  }
 
  async triggerExport(
    tenantId: string,
    exportDto: ExportQueryDto,
    userId: string,
  ) {
    // In real app, kicks off a background job to build CSV/Excel,
    // saves to Document Service, and sends notification to userId
    return {
      message:
        'Export triggered successfully. You will be notified when it is ready.',
      jobDetails: exportDto,
    };
  }
}