"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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExportTemplateService = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const crud_service_1 = require("./crud.service");
const export_transaction_service_1 = require("./export-transaction.service");
const export_template_repository_1 = require("../repository/export-template.repository");
const lodash_1 = require("lodash");
let ExportTemplateService = class ExportTemplateService extends crud_service_1.CRUDService {
    async startExportSync(updateDto, filters, activeUser) {
        const modelMetadata = await this.modelMetadataService.findOne(updateDto?.modelMetadataId);
        const modelName = (0, lodash_1.upperFirst)((0, lodash_1.camelCase)(modelMetadata.singularName));
        const permissionKey = `${modelName}Controller.findMany`;
        const userPermissions = activeUser.permissions ?? [];
        const hasPermission = Array.isArray(userPermissions)
            ? userPermissions.includes(permissionKey)
            : userPermissions[permissionKey] === true;
        if (!hasPermission) {
            throw new common_1.ForbiddenException(`Missing permission: ${permissionKey}`);
        }
        const exportTransaction = await this.exportTransactionService.toDto({
            datetime: new Date(),
            status: 'started',
            exportTemplateId: updateDto?.id ? updateDto?.id : null,
        });
        const exportTransactionEntity = await this.exportTransactionService.create(exportTransaction);
        const exportFileInfo = await this.exportTransactionService.triggerExportSync(exportTransactionEntity.id, exportTransactionEntity, updateDto, filters);
        return exportFileInfo;
    }
    async startExportAsync(updateDto, filters) {
        const exportTransaction = await this.exportTransactionService.toDto({
            datetime: new Date(),
            status: 'started',
            exportTemplateId: updateDto?.id ? updateDto?.id : null,
        });
        const exportTransactionEntity = await this.exportTransactionService.create(exportTransaction);
        this.exportTransactionService.triggerExportAsync(exportTransactionEntity.id, exportTransactionEntity, updateDto, filters);
        return exportTransactionEntity;
    }
    constructor(entityManager, repo, exportTransactionService, moduleRef) {
        super(entityManager, repo, 'exportTemplate', 'solid-core', moduleRef);
        this.entityManager = entityManager;
        this.repo = repo;
        this.exportTransactionService = exportTransactionService;
        this.moduleRef = moduleRef;
    }
};
exports.ExportTemplateService = ExportTemplateService;
exports.ExportTemplateService = ExportTemplateService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectEntityManager)()),
    __metadata("design:paramtypes", [typeorm_2.EntityManager,
        export_template_repository_1.ExportTemplateRepository,
        export_transaction_service_1.ExportTransactionService,
        core_1.ModuleRef])
], ExportTemplateService);
//# sourceMappingURL=export-template.service.js.map