refactor(server): domain/infra (#1298)

* refactor: user repository

* refactor: user module

* refactor: move database into infra

* refactor(cli): use user core

* chore: import path

* chore: tests
This commit is contained in:
Jason Rasmussen
2023-01-11 21:34:36 -05:00
committed by GitHub
parent 89a6ed2a5b
commit 131caa20eb
182 changed files with 701 additions and 676 deletions

View File

@@ -1,11 +1,17 @@
import { DatabaseModule, SystemConfigEntity, UserEntity } from '@app/database';
import { DomainModule } from '@app/domain';
import { InfraModule, SystemConfigEntity } from '@app/infra';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DisablePasswordLoginCommand, EnablePasswordLoginCommand } from './commands/password-login';
import { PromptPasswordQuestions, ResetAdminPasswordCommand } from './commands/reset-admin-password.command';
@Module({
imports: [DatabaseModule, TypeOrmModule.forFeature([UserEntity, SystemConfigEntity])],
imports: [
DomainModule.register({
imports: [InfraModule],
}),
TypeOrmModule.forFeature([SystemConfigEntity]),
],
providers: [
ResetAdminPasswordCommand,
PromptPasswordQuestions,

View File

@@ -1,4 +1,4 @@
import { SystemConfigEntity, SystemConfigKey } from '@app/database';
import { SystemConfigEntity, SystemConfigKey } from '@app/infra';
import { InjectRepository } from '@nestjs/typeorm';
import axios from 'axios';
import { Command, CommandRunner } from 'nest-commander';
@@ -9,9 +9,7 @@ import { Repository } from 'typeorm';
description: 'Enable password login',
})
export class EnablePasswordLoginCommand extends CommandRunner {
constructor(
@InjectRepository(SystemConfigEntity) private repository: Repository<SystemConfigEntity>, //
) {
constructor(@InjectRepository(SystemConfigEntity) private repository: Repository<SystemConfigEntity>) {
super();
}

View File

@@ -1,40 +1,38 @@
import { UserEntity } from '@app/database';
import { InjectRepository } from '@nestjs/typeorm';
import bcrypt from 'bcrypt';
import { Inject } from '@nestjs/common';
import { Command, CommandRunner, InquirerService, Question, QuestionSet } from 'nest-commander';
import { randomBytes } from 'node:crypto';
import { Repository } from 'typeorm';
import { IUserRepository, UserCore } from '@app/domain';
@Command({
name: 'reset-admin-password',
description: 'Reset the admin password',
})
export class ResetAdminPasswordCommand extends CommandRunner {
constructor(
private readonly inquirer: InquirerService,
@InjectRepository(UserEntity) private userRepository: Repository<UserEntity>,
) {
userCore: UserCore;
constructor(private readonly inquirer: InquirerService, @Inject(IUserRepository) userRepository: IUserRepository) {
super();
this.userCore = new UserCore(userRepository);
}
async run(): Promise<void> {
let { password } = await this.inquirer.ask<{ password: string }>('prompt-password', undefined);
password = password || randomBytes(24).toString('base64').replace(/\W/g, '');
const hashedPassword = await bcrypt.hash(password, 10);
const user = await this.userRepository.findOne({ where: { isAdmin: true } });
const user = await this.userCore.getAdmin();
if (!user) {
console.log('Unable to reset password: no admin user.');
return;
}
user.password = hashedPassword;
user.shouldChangePassword = true;
const { password: providedPassword } = await this.inquirer.ask<{ password: string }>('prompt-password', undefined);
const password = providedPassword || randomBytes(24).toString('base64').replace(/\W/g, '');
await this.userRepository.save(user);
await this.userCore.updateUser(user, user.id, { password });
console.log(`New password:\n${password}`);
if (providedPassword) {
console.log('The admin password has been updated.');
} else {
console.log(`The admin password has been updated to:\n${password}`);
}
}
}