"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeedRole1610516350460 = void 0;
const typeorm_1 = require("typeorm");
const shell_1 = require("@things-factory/shell");
const auth_base_1 = require("@things-factory/auth-base");
const PRIVILEGES_SEED = {
    'modeller': [
        {
            name: 'mutation',
            category: 'setting'
        },
        {
            name: 'mutation',
            category: 'menu'
        },
        {
            name: 'mutation',
            category: 'user'
        },
        {
            name: 'mutation',
            category: 'appliance'
        },
        {
            name: 'mutation',
            category: 'appliance'
        },
        {
            name: 'mutation',
            category: 'application'
        },
        {
            name: 'mutation',
            category: 'board'
        },
        {
            name: 'mutation',
            category: 'group'
        },
        {
            name: 'mutation',
            category: 'play-group'
        },
        {
            name: 'mutation',
            category: 'fonts'
        },
        {
            name: 'mutation',
            category: 'attachment'
        },
        {
            name: 'mutation',
            category: 'scenario'
        },
        {
            name: 'mutation',
            category: 'step'
        },
        {
            name: 'query',
            category: 'setting'
        },
        {
            name: 'query',
            category: 'menu'
        },
        {
            name: 'query',
            category: 'user'
        },
        {
            name: 'query',
            category: 'appliance'
        },
        {
            name: 'query',
            category: 'application'
        },
        {
            name: 'query',
            category: 'board'
        },
        {
            name: 'query',
            category: 'group'
        },
        {
            name: 'query',
            category: 'play-group'
        },
        {
            name: 'query',
            category: 'fonts'
        },
        {
            name: 'query',
            category: 'attachment'
        },
        {
            name: 'query',
            category: 'scenario'
        },
        {
            name: 'query',
            category: 'step'
        }
    ],
    'system': [
        {
            name: 'mutation',
            category: 'system'
        },
        {
            name: 'query',
            category: 'system'
        }
    ],
    'viewer': [
        {
            name: 'query',
            category: 'board'
        },
        {
            name: 'query',
            category: 'group'
        },
        {
            name: 'query',
            category: 'play-group'
        }
    ]
};
const SEED = [
    {
        name: 'modeller',
        description: 'Board Modeller'
    },
    {
        name: 'viewer',
        description: 'Board Viewer'
    }
];
class SeedRole1610516350460 {
    async up(queryRunner) {
        const repository = (0, typeorm_1.getRepository)(auth_base_1.Role);
        const userRepository = (0, typeorm_1.getRepository)(auth_base_1.User);
        let users = await userRepository.find({ where: { userType: 'admin' }, relations: ['domains'] });
        let domain = await (0, typeorm_1.getRepository)(shell_1.Domain).findOne({
            name: 'SYSTEM'
        });
        var privilegeSeeds = [];
        Promise.all(SEED.map(async (role) => {
            var privileges = await this.getPrivileges(privilegeSeeds, role.name);
            await repository.save(Object.assign(Object.assign({}, role), { 'privileges': privileges, users,
                domain }));
        }));
        await this.saveSystemRole(repository, users, domain);
    }
    async saveSystemRole(repository, users, domain) {
        const privilegeRepository = (0, typeorm_1.getRepository)(auth_base_1.Privilege);
        var role = {
            name: 'system',
            description: 'System Admin'
        };
        var privileges = await privilegeRepository.find();
        await repository.save(Object.assign(Object.assign({}, role), { 'privileges': privileges, users,
            domain }));
    }
    async getPrivileges(privilegeSeeds, roleName) {
        const privilegeRepository = (0, typeorm_1.getRepository)(auth_base_1.Privilege);
        privilegeSeeds = PRIVILEGES_SEED[roleName];
        var privileges = [];
        await Promise.all(privilegeSeeds.map(async (privilege) => {
            var value = await privilegeRepository.findOne({ where: { name: privilege.name, category: privilege.category } });
            if (value) {
                privileges.push(value);
            }
        }));
        return privileges;
    }
    async down(queryRunner) {
        const repository = (0, typeorm_1.getRepository)(auth_base_1.Role);
        const privilegeRepository = (0, typeorm_1.getRepository)(auth_base_1.Privilege);
        const domain = await (0, typeorm_1.getRepository)(shell_1.Domain).findOne({
            name: 'SYSTEM'
        });
        await Promise.all(SEED.reverse().map(async (role) => {
            await repository.delete({ name: role.name, domain });
        }));
        await repository.delete({ name: 'system', domain });
    }
}
exports.SeedRole1610516350460 = SeedRole1610516350460;
//# sourceMappingURL=1610516350460-SeedRole.js.map