fix: fix create admin command
This commit is contained in:
+2
-2
@@ -28,8 +28,8 @@
|
||||
"migration:revert:prod": "npm run typeorm:prod -- migration:revert",
|
||||
"seed": "cross-env NODE_ENV=development tsx src/database/seeds/seed.ts",
|
||||
"seed:prod": "cross-env NODE_ENV=production node dist/database/seeds/seed.js",
|
||||
"create-admin:dev": "cross-env NODE_ENV=development tsx src/cli/create-admin.ts",
|
||||
"create-admin:prod": "cross-env NODE_ENV=production tsx src/cli/create-admin.ts"
|
||||
"create-admin:dev": "npm run build && cross-env NODE_ENV=development node dist/cli/create-admin.js",
|
||||
"create-admin:prod": "npm run build && cross-env NODE_ENV=production node dist/cli/create-admin.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@adminjs/express": "^5.1.0",
|
||||
|
||||
+16
-3
@@ -4,6 +4,9 @@ import { Role } from '@/common/enums/roles.enum';
|
||||
import { password, input, select } from '@inquirer/prompts';
|
||||
import { AppModule } from '@/app.module';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { CreatePrivilegedUserDTO } from '@/modules/users/dtos/create-privileged-user.dto';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { validateOrReject } from 'class-validator';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.createApplicationContext(AppModule);
|
||||
@@ -11,7 +14,7 @@ async function bootstrap() {
|
||||
try {
|
||||
const configService = app.get(ConfigService);
|
||||
|
||||
if (configService.get<boolean>('app.enablePrivilegedUserCli')) {
|
||||
if (!configService.get<boolean>('app.enablePrivilegedUserCli')) {
|
||||
throw new Error('Privileged user creation is disabled.');
|
||||
}
|
||||
|
||||
@@ -26,7 +29,7 @@ async function bootstrap() {
|
||||
});
|
||||
|
||||
const lastName = await input({
|
||||
message: 'Enter Last Name',
|
||||
message: 'Enter Last Name:',
|
||||
});
|
||||
|
||||
const role = await select({
|
||||
@@ -44,6 +47,7 @@ async function bootstrap() {
|
||||
|
||||
const userConfirmPassword = await password({
|
||||
message: 'Enter Password Confirmation:',
|
||||
mask: '*',
|
||||
});
|
||||
|
||||
if (userPassword !== userConfirmPassword) {
|
||||
@@ -58,15 +62,24 @@ async function bootstrap() {
|
||||
throw new Error('Confirmation failed.');
|
||||
}
|
||||
|
||||
const user = await usersService.createPrivilegedUser({
|
||||
const rawData: CreatePrivilegedUserDTO = {
|
||||
phone,
|
||||
firstName,
|
||||
lastName,
|
||||
password: userPassword,
|
||||
confirmPassword: userConfirmPassword,
|
||||
role,
|
||||
};
|
||||
|
||||
const dto = plainToInstance(CreatePrivilegedUserDTO, rawData);
|
||||
|
||||
await validateOrReject(dto, {
|
||||
whitelist: true,
|
||||
forbidNonWhitelisted: true,
|
||||
});
|
||||
|
||||
const user = await usersService.createPrivilegedUser(dto);
|
||||
|
||||
console.log(`Successfully created ${role}: ${user.phone}`);
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
|
||||
@@ -26,6 +26,8 @@ export class PrivilegedUserProvider {
|
||||
|
||||
const user = this.userRepository.create({
|
||||
phone: data.phone,
|
||||
firstName: data.firstName,
|
||||
lastName: data.lastName,
|
||||
password: hashedPassword,
|
||||
role: data.role,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user