From 0455c9461b6eb7e619998014e5cd28b650a2f5a8 Mon Sep 17 00:00:00 2001
From: Jackson Chen <541898146chen@gmail.com>
Date: Sun, 27 Oct 2024 18:09:41 -0500
Subject: [PATCH] feat: Add project frontend API with tests and fix backend
project API
This commit adds the project frontend API along with its corresponding tests. It also fixes an issue in the backend project API where the user ID should be a number.
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
---
backend/src/app.module.ts | 23 ++-
backend/src/app.resolver.ts | 12 ++
backend/src/app.service.ts | 8 -
backend/src/auth/auth.service.ts | 247 ++++++++++++++++++++++-
backend/src/auth/role/role.model.ts | 35 +++-
backend/src/common/enums/role.enum.ts | 3 +
backend/src/database.sqlite | Bin 86016 -> 94208 bytes
backend/src/decorator/auth.decorator.ts | 47 +++++
backend/src/decorator/menu.decorator.ts | 4 +
backend/src/decorator/roles.decorator.ts | 4 +
backend/src/guard/menu.guard.ts | 70 +++++++
backend/src/guard/roles.guard.ts | 69 +++++++
backend/src/init/init-roles.service.ts | 56 +++++
backend/src/init/init.module.ts | 13 ++
backend/src/schema.gql | 3 +-
backend/src/user/user.service.ts | 14 +-
16 files changed, 565 insertions(+), 43 deletions(-)
create mode 100644 backend/src/app.resolver.ts
delete mode 100644 backend/src/app.service.ts
create mode 100644 backend/src/common/enums/role.enum.ts
create mode 100644 backend/src/decorator/auth.decorator.ts
create mode 100644 backend/src/decorator/menu.decorator.ts
create mode 100644 backend/src/decorator/roles.decorator.ts
create mode 100644 backend/src/guard/menu.guard.ts
create mode 100644 backend/src/guard/roles.guard.ts
create mode 100644 backend/src/init/init-roles.service.ts
create mode 100644 backend/src/init/init.module.ts
diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts
index 4684cfa..711a60e 100644
--- a/backend/src/app.module.ts
+++ b/backend/src/app.module.ts
@@ -1,18 +1,19 @@
+import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
-import { AppService } from './app.service';
+import { ConfigModule } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
-import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
-import { join } from 'path';
import { TypeOrmModule } from '@nestjs/typeorm';
-import { UserModule } from './user/user.module';
-import { User } from './user/user.model';
+import { join } from 'path';
import { AuthModule } from './auth/auth.module';
+import { ChatModule } from './chat/chat.module';
import { ProjectModule } from './project/project.module';
import { TokenModule } from './token/token.module';
-import { ConfigModule, ConfigService } from '@nestjs/config';
-import { JwtModule } from '@nestjs/jwt';
-import { JwtCacheService } from './auth/jwt-cache.service';
-import { ChatModule } from './chat/chat.module';
+import { UserModule } from './user/user.module';
+import { InitModule } from './init/init.module';
+import { RolesGuard } from './guard/roles.guard';
+import { MenuGuard } from './guard/menu.guard';
+import { User } from './user/user.model';
+import { AppResolver } from './app.resolver';
@Module({
imports: [
@@ -32,12 +33,14 @@ import { ChatModule } from './chat/chat.module';
logging: true,
entities: [__dirname + '/**/*.model{.ts,.js}'],
}),
+ InitModule,
UserModule,
AuthModule,
ProjectModule,
TokenModule,
ChatModule,
+ TypeOrmModule.forFeature([User]),
],
- providers: [AppService],
+ providers: [AppResolver],
})
export class AppModule {}
diff --git a/backend/src/app.resolver.ts b/backend/src/app.resolver.ts
new file mode 100644
index 0000000..67f05b6
--- /dev/null
+++ b/backend/src/app.resolver.ts
@@ -0,0 +1,12 @@
+import { Injectable } from '@nestjs/common';
+import { Query, Resolver } from '@nestjs/graphql';
+import { RequireRoles } from './decorator/auth.decorator';
+
+@Resolver()
+export class AppResolver {
+ @Query(() => String)
+ @RequireRoles('Admin')
+ getHello(): string {
+ return 'Hello World!';
+ }
+}
diff --git a/backend/src/app.service.ts b/backend/src/app.service.ts
deleted file mode 100644
index 927d7cc..0000000
--- a/backend/src/app.service.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-import { Injectable } from '@nestjs/common';
-
-@Injectable()
-export class AppService {
- getHello(): string {
- return 'Hello World!';
- }
-}
diff --git a/backend/src/auth/auth.service.ts b/backend/src/auth/auth.service.ts
index c9c1baa..0f54347 100644
--- a/backend/src/auth/auth.service.ts
+++ b/backend/src/auth/auth.service.ts
@@ -2,6 +2,7 @@ import {
ConflictException,
Injectable,
Logger,
+ NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@@ -11,9 +12,11 @@ import { compare, hash } from 'bcrypt';
import { LoginUserInput } from 'src/user/dto/login-user.input';
import { RegisterUserInput } from 'src/user/dto/register-user.input';
import { User } from 'src/user/user.model';
-import { Repository } from 'typeorm';
+import { In, Repository } from 'typeorm';
import { CheckTokenInput } from './dto/check-token.input';
import { JwtCacheService } from 'src/auth/jwt-cache.service';
+import { Menu } from './menu/menu.model';
+import { Role } from './role/role.model';
@Injectable()
export class AuthService {
@@ -23,6 +26,10 @@ export class AuthService {
private jwtService: JwtService,
private jwtCacheService: JwtCacheService,
private configService: ConfigService,
+ @InjectRepository(Menu)
+ private menuRepository: Repository