import { isEmpty } from "lodash" ; import { ConflictingUserError , SomeGroupNotFoundError } from "../common/errors" ; import generateId from "../common/generateId" ; import Usecase from "../common/Usecase" ; import { Operation } from "../entities/OperationLog" ; import { IUser , UserType } from "../entities/User" ; export default class CreateUser extends Usecase { async exec ( partial ) { await this . authorizer . ensureCanCreateUser ( ) ; const conflictingUserExists = await this . storages . users . oneExistsWithIdpAndIdpId ( partial . idp , partial . idpId ) ; if ( conflictingUserExists ) { throw new ConflictingUserError ( partial . idp , partial . idpId ) ; } if ( ! isEmpty ( partial . groupsIds ) && ! ( await this . storages . groups . allExistWithIds ( partial . groupsIds ) ) ) { throw new SomeGroupNotFoundError ( partial . groupsIds ) ; } const now = new Date ( ) ; const createdUser = await this . storages . users . createOne ( { id : generateId ( ) , name : partial . name , idp : partial . idp , idpId : partial . idpId , type : partial . type , groupsIds : partial . groupsIds , createdAt : now , updatedAt : now , } ) ; await this . operationLogger . logOperation ( Operation . CreateUser , { createdUser : createdUser , } ) ; return createdUser ; } }