"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OnmDomainBindingMutation = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const typeorm_1 = require("typeorm");
const onm_domain_binding_1 = require("./onm-domain-binding");
const onm_domain_binding_type_1 = require("./onm-domain-binding-type");
let OnmDomainBindingMutation = class OnmDomainBindingMutation {
    async updateMultipleOnmDomainBinding(patches, context) {
        const { domain, user, tx } = context.state;
        let results = [];
        const _createRecords = patches.filter((patch) => patch.cuFlag.toUpperCase() === '+');
        const _updateRecords = patches.filter((patch) => patch.cuFlag.toUpperCase() === 'M');
        const onmDomainBindingRepo = tx.getRepository(onm_domain_binding_1.OnmDomainBinding);
        if (_createRecords.length > 0) {
            for (let i = 0; i < _createRecords.length; i++) {
                const newRecord = _createRecords[i];
                const result = await onmDomainBindingRepo.save({
                    ...newRecord,
                    onmDomain: { id: newRecord.onmDomainId },
                    powerPlantDomain: { id: newRecord.powerPlantDomainId },
                    domain,
                    creator: user,
                    updater: user
                });
            }
        }
        if (_updateRecords.length > 0) {
            for (let i = 0; i < _updateRecords.length; i++) {
                const updateRecord = _updateRecords[i];
                const onmDomainBinding = await onmDomainBindingRepo.findOneBy({ id: updateRecord.id });
                const result = await onmDomainBindingRepo.save({
                    ...onmDomainBinding,
                    onmDomain: { id: updateRecord.onmDomainId },
                    powerPlantDomain: { id: updateRecord.powerPlantDomainId },
                    updater: user
                });
                results.push({ ...result, cuFlag: 'M' });
            }
        }
        return results;
    }
    async deleteOnmDomainBindings(ids, context) {
        const { domain, tx } = context.state;
        await tx.getRepository(onm_domain_binding_1.OnmDomainBinding).delete({
            id: (0, typeorm_1.In)(ids)
        });
        return true;
    }
};
exports.OnmDomainBindingMutation = OnmDomainBindingMutation;
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => [onm_domain_binding_1.OnmDomainBinding], { description: "To modify multiple OnmDomainBindings' information" }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('patches', type => [onm_domain_binding_type_1.OnmDomainBindingPatch])),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Array, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], OnmDomainBindingMutation.prototype, "updateMultipleOnmDomainBinding", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete multiple OnmDomainBindings' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('ids', type => [String])),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Array, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], OnmDomainBindingMutation.prototype, "deleteOnmDomainBindings", null);
exports.OnmDomainBindingMutation = OnmDomainBindingMutation = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(onm_domain_binding_1.OnmDomainBinding)
], OnmDomainBindingMutation);
//# sourceMappingURL=onm-domain-binding-mutation.js.map