Skip to content

Commit e6e732d

Browse files
committed
adds create-admin script
1 parent 733a67a commit e6e732d

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

.env

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
55
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
66

7-
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
7+
8+
DATABASE_URL=mongodb://db:27017/dev
9+
NODE_ENV=development
10+
SECRET=secret

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Run `npm install` to install Node dependencies:
4545
```
4646
npm install
4747
```
48+
### Create admin account
49+
```
50+
npx nest start --entryFile create-admin.js
51+
```
52+
and it will ask you for email password and name and it will create a superuser account
4853

4954
### Run in development
5055
Run the app using Docker Compose:

src/create-admin.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {createInterface} from "readline/promises"
2+
3+
import { ValidationPipe } from '@nestjs/common';
4+
import { NestFactory } from '@nestjs/core';
5+
import { AppModule } from './app.module';
6+
import { UsersService } from './modules/users/users.service';
7+
import { Role } from "./modules/users/user.model";
8+
9+
async function bootstrap() {
10+
const rl = createInterface(process.stdin, process.stdout)
11+
const app = await NestFactory.create(AppModule);
12+
app.useGlobalPipes(new ValidationPipe());
13+
const usersService = app.get(UsersService)
14+
15+
const { id } = await usersService.register({
16+
email: await rl.question('email address : '),
17+
name: await rl.question('name :'),
18+
password: await rl.question("password :")
19+
});
20+
await usersService.changeRole(Role.superadmin, id)
21+
console.log("end")
22+
process.exit(0)
23+
}
24+
bootstrap();

src/modules/users/users.resolver.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ export class UsersResolver {
1313
@UseGuards(AuthGuard)
1414
@Mutation(() => User)
1515
async changeRole(
16-
@UserData() user: UserAuth,
1716
@Args('newRole', { type: () => Role }) role: Role,
1817
@Args('userId', { type: () => String }) id: string,
19-
2018
) {
21-
return await this.service.changeRole(user, role, id);
19+
return await this.service.changeRole(role, id);
2220
}
2321
@MinRole(Role.superadmin)
2422
@UseGuards(AuthGuard)

src/modules/users/users.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class UsersService {
1818
getAll(): Promise<User[]> {
1919
return this.collection.find().toArray().then(e => e.map(mapOID));
2020
}
21-
async changeRole(user: UserAuth, role: Role, id: string): Promise<User> {
21+
async changeRole( role: Role, id: string): Promise<User> {
2222
const userExists = await this.getUserById(id);
2323
if (!userExists)
2424
throw new NotFoundException("user not found");

0 commit comments

Comments
 (0)