"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.DomainResolver = void 0;
const type_graphql_1 = require("type-graphql");
const typeorm_1 = require("typeorm");
const shell_1 = require("@things-factory/shell");
const utils_1 = require("@things-factory/utils");
const debug = require('debug')('things-factory:operato-board:domain-resolver');
let DomainResolver = class DomainResolver {
    async updateDomains(patches) {
        const domainRepo = (0, typeorm_1.getRepository)(shell_1.Domain);
        const _createRecords = patches.filter((patch) => !patch.id);
        const _updateRecords = patches.filter((patch) => patch.id);
        debug('update-domains');
        if (_createRecords.length > 0) {
            _createRecords.forEach(async (createRecord) => {
                const subdomain = (0, utils_1.slugger)(createRecord.name);
                await domainRepo.save(Object.assign(Object.assign({}, createRecord), { subdomain }));
            });
        }
        if (_updateRecords.length > 0) {
            _updateRecords.forEach(async (updateRecord) => {
                const domain = await domainRepo.findOne({ where: { id: updateRecord.id } });
                if (updateRecord.name) {
                    updateRecord.subdomain = (0, utils_1.slugger)(updateRecord.name);
                }
                await domainRepo.save(Object.assign(Object.assign({}, domain), updateRecord));
            });
        }
        return true;
    }
};
__decorate([
    (0, type_graphql_1.Directive)('@privilege(category: "system", privilege: "mutation", superUserGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To update multiple domains' }),
    __param(0, (0, type_graphql_1.Arg)('patches', type => [shell_1.DomainPatch])),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Array]),
    __metadata("design:returntype", Promise)
], DomainResolver.prototype, "updateDomains", null);
DomainResolver = __decorate([
    (0, type_graphql_1.Resolver)(shell_1.Domain)
], DomainResolver);
exports.DomainResolver = DomainResolver;
//# sourceMappingURL=domain-resolver.js.map