|
| 1 | +import { BadRequestException, Inject, Injectable } from '@nestjs/common'; |
| 2 | +import AbstractUseCase from '../../../common/abstract-use.case.js'; |
| 3 | +import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js'; |
| 4 | +import { BaseType } from '../../../common/data-injection.tokens.js'; |
| 5 | +import { SimpleFoundUserInfoDs } from '../../../entities/user/dto/found-user.dto.js'; |
| 6 | +import { ICreateInitialUserUseCase } from './selfhosted-use-cases.interfaces.js'; |
| 7 | +import { CreateInitialUserDs } from '../data-structures/create-initial-user.ds.js'; |
| 8 | +import { isSaaS } from '../../../helpers/app/is-saas.js'; |
| 9 | +import { Messages } from '../../../exceptions/text/messages.js'; |
| 10 | +import { RegisterUserDs } from '../../../entities/user/application/data-structures/register-user-ds.js'; |
| 11 | +import { UserRoleEnum } from '../../../entities/user/enums/user-role.enum.js'; |
| 12 | +import { buildRegisteringUser } from '../../../entities/user/utils/build-registering-user.util.js'; |
| 13 | +import { CompanyInfoEntity } from '../../../entities/company-info/company-info.entity.js'; |
| 14 | +import { Encryptor } from '../../../helpers/encryption/encryptor.js'; |
| 15 | +import { buildSimpleUserInfoDs } from '../../../entities/user/utils/build-created-user.ds.js'; |
| 16 | + |
| 17 | +@Injectable() |
| 18 | +export class CreateInitialUserUseCase |
| 19 | + extends AbstractUseCase<CreateInitialUserDs, SimpleFoundUserInfoDs> |
| 20 | + implements ICreateInitialUserUseCase |
| 21 | +{ |
| 22 | + constructor( |
| 23 | + @Inject(BaseType.GLOBAL_DB_CONTEXT) |
| 24 | + protected _dbContext: IGlobalDatabaseContext, |
| 25 | + ) { |
| 26 | + super(); |
| 27 | + } |
| 28 | + |
| 29 | + protected async implementation(inputData: CreateInitialUserDs): Promise<SimpleFoundUserInfoDs> { |
| 30 | + if (isSaaS()) { |
| 31 | + throw new BadRequestException(Messages.ENDPOINT_NOT_AVAILABLE_IN_THIS_MODE); |
| 32 | + } |
| 33 | + |
| 34 | + const userCount = await this._dbContext.userRepository.count(); |
| 35 | + if (userCount > 0) { |
| 36 | + throw new BadRequestException(Messages.SELF_HOSTED_ALREADY_CONFIGURED); |
| 37 | + } |
| 38 | + |
| 39 | + const { email, password } = inputData; |
| 40 | + const registerUserData: RegisterUserDs = { |
| 41 | + email: email, |
| 42 | + password: password, |
| 43 | + isActive: true, |
| 44 | + gclidValue: null, |
| 45 | + name: 'Admin', |
| 46 | + role: UserRoleEnum.ADMIN, |
| 47 | + }; |
| 48 | + |
| 49 | + const savedUser = await this._dbContext.userRepository.saveUserEntity(buildRegisteringUser(registerUserData)); |
| 50 | + |
| 51 | + const newCompanyInfo = new CompanyInfoEntity(); |
| 52 | + newCompanyInfo.id = Encryptor.generateUUID(); |
| 53 | + const savedCompanyInfo = await this._dbContext.companyInfoRepository.save(newCompanyInfo); |
| 54 | + |
| 55 | + savedUser.company = savedCompanyInfo; |
| 56 | + const finalUser = await this._dbContext.userRepository.saveUserEntity(savedUser); |
| 57 | + |
| 58 | + return buildSimpleUserInfoDs(finalUser); |
| 59 | + } |
| 60 | +} |
0 commit comments