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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
182 changed files with 701 additions and 676 deletions

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}`);
}
}
}