|
1 | | -import { Injectable, OnModuleInit } from '@nestjs/common'; |
2 | | -import { isSaaS } from '../../helpers/app/is-saas.js'; |
| 1 | +import { Injectable } from '@nestjs/common'; |
3 | 2 | import { FoundUserInGroupDs } from './application/data-structures/found-user-in-group.ds.js'; |
4 | 3 | import { FoundUserDto } from './dto/found-user.dto.js'; |
5 | 4 | import { UserEntity } from './user.entity.js'; |
6 | 5 | import { getUserIntercomHash } from './utils/get-user-intercom-hash.js'; |
7 | | -import { Encryptor } from '../../helpers/encryption/encryptor.js'; |
8 | | -import { CompanyInfoEntity } from '../company-info/company-info.entity.js'; |
9 | | -import { RegisterUserDs } from './application/data-structures/register-user-ds.js'; |
10 | | -import { UserRoleEnum } from './enums/user-role.enum.js'; |
11 | | -import { InjectRepository } from '@nestjs/typeorm'; |
12 | | -import { Repository } from 'typeorm'; |
13 | | -import { buildRegisteringUser } from './utils/build-registering-user.util.js'; |
14 | 6 |
|
15 | 7 | @Injectable() |
16 | | -export class UserHelperService implements OnModuleInit { |
17 | | - constructor( |
18 | | - @InjectRepository(UserEntity) |
19 | | - private readonly userRepository: Repository<UserEntity>, |
20 | | - @InjectRepository(CompanyInfoEntity) |
21 | | - private readonly companyInfoRepository: Repository<CompanyInfoEntity>, |
22 | | - ) {} |
| 8 | +export class UserHelperService { |
| 9 | + public buildFoundUserInGroupDs(user: UserEntity): FoundUserInGroupDs { |
| 10 | + return { |
| 11 | + id: user.id, |
| 12 | + email: user.email, |
| 13 | + createdAt: user.createdAt, |
| 14 | + isActive: user.isActive, |
| 15 | + name: user.name, |
| 16 | + suspended: user.suspended, |
| 17 | + externalRegistrationProvider: user.externalRegistrationProvider, |
| 18 | + }; |
| 19 | + } |
23 | 20 |
|
24 | | - public buildFoundUserInGroupDs(user: UserEntity): FoundUserInGroupDs { |
25 | | - return { |
26 | | - id: user.id, |
27 | | - email: user.email, |
28 | | - createdAt: user.createdAt, |
29 | | - isActive: user.isActive, |
30 | | - name: user.name, |
31 | | - suspended: user.suspended, |
32 | | - externalRegistrationProvider: user.externalRegistrationProvider, |
33 | | - }; |
34 | | - } |
35 | | - |
36 | | - public async buildFoundUserDs(user: UserEntity): Promise<FoundUserDto> { |
37 | | - const intercomHash = getUserIntercomHash(user.id); |
38 | | - return { |
39 | | - id: user.id, |
40 | | - createdAt: user.createdAt, |
41 | | - suspended: user.suspended, |
42 | | - isActive: user.isActive, |
43 | | - email: user.email, |
44 | | - intercom_hash: intercomHash, |
45 | | - name: user.name, |
46 | | - role: user.role, |
47 | | - is_2fa_enabled: user.otpSecretKey !== null && user.isOTPEnabled, |
48 | | - company: user.company ? { id: user.company.id } : null, |
49 | | - externalRegistrationProvider: user.externalRegistrationProvider, |
50 | | - show_test_connections: user.showTestConnections, |
51 | | - }; |
52 | | - } |
53 | | - |
54 | | - public async onModuleInit(): Promise<void> { |
55 | | - if (isSaaS()) { |
56 | | - return; |
57 | | - } |
58 | | - const email = (process.env.ADMIN_EMAIL || 'admin@email.local').toLowerCase(); |
59 | | - const password = |
60 | | - process.env.ADMIN_PASSWORD || |
61 | | - (process.env.NODE_ENV === 'test' ? 'test12345' : Encryptor.generateRandomString(10)); |
62 | | - |
63 | | - const foundTestUser = await this.userRepository.findOneBy({ email: email }); |
64 | | - if (foundTestUser) { |
65 | | - return; |
66 | | - } |
67 | | - |
68 | | - const registerUserData: RegisterUserDs = { |
69 | | - email: email, |
70 | | - password: password, |
71 | | - isActive: true, |
72 | | - gclidValue: null, |
73 | | - name: 'Admin', |
74 | | - role: UserRoleEnum.ADMIN, |
75 | | - }; |
76 | | - const savedUser = await this.userRepository.save(buildRegisteringUser(registerUserData)); |
77 | | - const newCompanyInfo = new CompanyInfoEntity(); |
78 | | - newCompanyInfo.id = Encryptor.generateUUID(); |
79 | | - const savedCompanyInfo = await this.companyInfoRepository.save(newCompanyInfo); |
80 | | - savedUser.company = savedCompanyInfo; |
81 | | - await this.userRepository.save(savedUser); |
82 | | - console.info(`Admin user created with email: "${email}" and password: "${password}"`); |
83 | | - } |
| 21 | + public async buildFoundUserDs(user: UserEntity): Promise<FoundUserDto> { |
| 22 | + const intercomHash = getUserIntercomHash(user.id); |
| 23 | + return { |
| 24 | + id: user.id, |
| 25 | + createdAt: user.createdAt, |
| 26 | + suspended: user.suspended, |
| 27 | + isActive: user.isActive, |
| 28 | + email: user.email, |
| 29 | + intercom_hash: intercomHash, |
| 30 | + name: user.name, |
| 31 | + role: user.role, |
| 32 | + is_2fa_enabled: user.otpSecretKey !== null && user.isOTPEnabled, |
| 33 | + company: user.company ? { id: user.company.id } : null, |
| 34 | + externalRegistrationProvider: user.externalRegistrationProvider, |
| 35 | + show_test_connections: user.showTestConnections, |
| 36 | + }; |
| 37 | + } |
84 | 38 | } |
0 commit comments