"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelSequenceRepository = void 0;
const common_1 = require("@nestjs/common");
const model_sequence_entity_1 = require("../entities/model-sequence.entity");
const request_context_service_1 = require("../services/request-context.service");
const typeorm_1 = require("typeorm");
const security_rule_repository_1 = require("./security-rule.repository");
const solid_base_repository_1 = require("./solid-base.repository");
const module_metadata_entity_1 = require("../entities/module-metadata.entity");
const model_metadata_entity_1 = require("../entities/model-metadata.entity");
const field_metadata_entity_1 = require("../entities/field-metadata.entity");
let ModelSequenceRepository = class ModelSequenceRepository extends solid_base_repository_1.SolidBaseRepository {
    constructor(dataSource, requestContextService, securityRuleRepository) {
        super(model_sequence_entity_1.ModelSequence, dataSource, requestContextService, securityRuleRepository);
        this.dataSource = dataSource;
        this.requestContextService = requestContextService;
        this.securityRuleRepository = securityRuleRepository;
    }
    async upsertWithDto(createDto) {
        const moduleRepository = this.dataSource.getRepository(module_metadata_entity_1.ModuleMetadata);
        if (createDto.moduleId) {
            const module = await moduleRepository.findOne({
                where: {
                    id: createDto.moduleId,
                },
            });
            createDto['module'] = module;
        }
        if (createDto.moduleUserKey) {
            const module = await moduleRepository.findOne({
                where: {
                    name: createDto.moduleUserKey,
                },
            });
            createDto['module'] = module;
        }
        const modelRepository = this.dataSource.getRepository(model_metadata_entity_1.ModelMetadata);
        if (createDto.modelId) {
            const model = await modelRepository.findOne({
                where: {
                    id: createDto.modelId,
                },
            });
            createDto['model'] = model;
        }
        if (createDto.modelUserKey) {
            const model = await modelRepository.findOne({
                where: {
                    singularName: createDto.modelUserKey,
                },
            });
            createDto['model'] = model;
        }
        const fieldRepository = this.dataSource.getRepository(field_metadata_entity_1.FieldMetadata);
        if (createDto.fieldId) {
            const field = await fieldRepository.findOne({
                where: {
                    id: createDto.fieldId,
                },
            });
            createDto['field'] = field;
        }
        if (createDto.fieldUserKey) {
            const field = await fieldRepository.findOne({
                where: {
                    name: createDto.fieldUserKey,
                },
            });
            createDto['field'] = field;
        }
        const existingModelSequence = await this.findOne({
            where: {
                sequenceName: createDto.sequenceName,
            },
        });
        if (existingModelSequence) {
            const updatedModelSequence = this.merge(existingModelSequence, createDto);
            return this.save(updatedModelSequence);
        }
        else {
            const modelSequence = this.create(createDto);
            return this.save(modelSequence);
        }
    }
};
exports.ModelSequenceRepository = ModelSequenceRepository;
exports.ModelSequenceRepository = ModelSequenceRepository = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [typeorm_1.DataSource,
        request_context_service_1.RequestContextService,
        security_rule_repository_1.SecurityRuleRepository])
], ModelSequenceRepository);
//# sourceMappingURL=model-sequence.repository.js.map