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