2022-11-05 12:28:40 -04:00
|
|
|
import { Command, CommandRunner, InquirerService, Question, QuestionSet } from 'nest-commander';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { UserService } from 'src/domain/user/user.service';
|
2024-03-20 23:53:07 +01:00
|
|
|
import { UserResponseDto } from 'src/dtos/user.dto';
|
2022-11-05 12:28:40 -04:00
|
|
|
|
|
|
|
|
@Command({
|
|
|
|
|
name: 'reset-admin-password',
|
|
|
|
|
description: 'Reset the admin password',
|
|
|
|
|
})
|
|
|
|
|
export class ResetAdminPasswordCommand extends CommandRunner {
|
2023-08-28 14:41:57 -05:00
|
|
|
constructor(
|
|
|
|
|
private userService: UserService,
|
|
|
|
|
private readonly inquirer: InquirerService,
|
|
|
|
|
) {
|
2022-11-05 12:28:40 -04:00
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
ask = (admin: UserResponseDto) => {
|
|
|
|
|
const { id, oauthId, email, name } = admin;
|
|
|
|
|
console.log(`Found Admin:
|
2023-01-16 13:09:04 -05:00
|
|
|
- ID=${id}
|
|
|
|
|
- OAuth ID=${oauthId}
|
|
|
|
|
- Email=${email}
|
2023-11-11 20:03:32 -05:00
|
|
|
- Name=${name}`);
|
2023-01-16 13:09:04 -05:00
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
return this.inquirer.ask<{ password: string }>('prompt-password', {}).then(({ password }) => password);
|
|
|
|
|
};
|
2023-01-16 13:09:04 -05:00
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
async run(): Promise<void> {
|
2023-01-16 13:09:04 -05:00
|
|
|
try {
|
2024-02-02 04:18:00 +01:00
|
|
|
const { password, provided } = await this.userService.resetAdminPassword(this.ask);
|
2023-01-16 13:09:04 -05:00
|
|
|
|
|
|
|
|
if (provided) {
|
|
|
|
|
console.log(`The admin password has been updated.`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`The admin password has been updated to:\n${password}`);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
console.error('Unable to reset admin password');
|
2023-01-11 21:34:36 -05:00
|
|
|
}
|
2022-11-05 12:28:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@QuestionSet({ name: 'prompt-password' })
|
|
|
|
|
export class PromptPasswordQuestions {
|
|
|
|
|
@Question({
|
|
|
|
|
message: 'Please choose a new password (optional)',
|
|
|
|
|
name: 'password',
|
|
|
|
|
})
|
2024-02-02 04:18:00 +01:00
|
|
|
parsePassword(value: string) {
|
|
|
|
|
return value;
|
2022-11-05 12:28:40 -04:00
|
|
|
}
|
|
|
|
|
}
|