"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    FirstTimeSetupController: function() {
        return FirstTimeSetupController;
    },
    default: function() {
        return _default;
    }
});
const _awilixexpress = require("awilix-express");
const _serverconstants = require("../server.constants");
const _validators = require("../handlers/validators");
const _settingvalidation = require("./validation/setting.validation");
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
const _authorizationconstants = require("../constants/authorization.constants");
class FirstTimeSetupController {
    userService;
    roleService;
    settingsStore;
    constructor({ settingsStore, roleService, userService }){
        this.settingsStore = settingsStore;
        this.roleService = roleService;
        this.userService = userService;
    }
    async validateWizard(req, res) {
        const { loginRequired, registration, rootUsername, rootPassword } = await (0, _validators.validateMiddleware)(req, _settingvalidation.wizardSettingsRules);
        const role = await this.roleService.getSynchronizedRoleByName(_authorizationconstants.ROLES.ADMIN);
        const user = await this.userService.findRawByUsername(rootUsername?.toLowerCase());
        if (!!user) {
            throw new _runtimeexceptions.BadRequestException("This user already exists");
        }
        return res.send();
    }
    async completeWizard(req, res) {
        const { loginRequired, registration, rootUsername, rootPassword } = await (0, _validators.validateMiddleware)(req, _settingvalidation.wizardSettingsRules);
        if (this.settingsStore.isWizardCompleted()) {
            throw new _runtimeexceptions.ForbiddenError("Wizard already completed");
        }
        const role = await this.roleService.getSynchronizedRoleByName(_authorizationconstants.ROLES.ADMIN);
        const user = await this.userService.findRawByUsername(rootUsername?.toLowerCase());
        if (!!user) {
            throw new _runtimeexceptions.BadRequestException("This user already exists");
        }
        await this.userService.register({
            username: rootUsername,
            password: rootPassword,
            roles: [
                role.id
            ],
            isRootUser: true,
            isVerified: true,
            isDemoUser: false,
            needsPasswordChange: false
        });
        await this.settingsStore.setLoginRequired(loginRequired);
        await this.settingsStore.setRegistrationEnabled(registration);
        await this.settingsStore.setWizardCompleted(_serverconstants.AppConstants.currentWizardVersion);
        return res.send();
    }
}
const _default = (0, _awilixexpress.createController)(FirstTimeSetupController).prefix(_serverconstants.AppConstants.apiRoute + "/first-time-setup").post("/validate", "validateWizard").post("/complete", "completeWizard");

//# sourceMappingURL=first-time-setup.controller.js.map