import generateId from "../common/generateId" ; import IStorages from "../dependencies/IStorages" ; import { RoleName } from "../entities/Role" ; import { UserType } from "../entities/User" ; export const ROOT_GROUP_NAME = "root" ; export const ROOT_USER_IDP_ID = "root" ; export const ROOT_USER_NAME = "root" ; export default class CreateRootUserAndGroup { constructor ( private storages ) { } async exec ( idp ) { const now = new Date ( ) ; let rootGroup = await this . storages . groups . findOneByName ( ROOT_GROUP_NAME ) ; if ( ! rootGroup ) { rootGroup = await this . storages . groups . createOne ( { id : generateId ( ) , name : ROOT_GROUP_NAME , roles : [ RoleName . Root ] , createdAt : now , updatedAt : now , } ) ; } const rootUserExists = await this . storages . users . oneExistsWithIdpAndIdpId ( idp , ROOT_USER_IDP_ID ) ; if ( ! rootUserExists ) { await this . storages . users . createOne ( { id : generateId ( ) , idp : idp , idpId : ROOT_USER_IDP_ID , type : UserType . Human , name : ROOT_USER_NAME , groupsIds : [ rootGroup . id ] , createdAt : now , updatedAt : now , } ) ; } } }