Skip to content

Commit e5bb9ed

Browse files
OpenAPI and request verification (#42)
* Add node_modules to eslintignore vscode tries to format node_modules if a file inside it is open * Fix typescript builds mocks for release * Add some DTOs and enable openapi * Create response Dto for findAllGroups * Uninstall @vegardit/prisma-generator-nestjs-dto reason: not used * Rename AllGroupsDto to GroupSummaryDto * Move JwtAuthGuard inside JwtAuth decorator * Remove code I should not have commited * seeding * konzi findOne fix * validation and more * consultation test fix * removed checking for emptyness of db * update readme * min(1) validator to manyuniqueusers Co-authored-by: Sámuel Fekete <[email protected]>
1 parent 5e9f41f commit e5bb9ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+863
-215
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
node: true,
1515
jest: true,
1616
},
17-
ignorePatterns: ['.eslintrc.js'],
17+
ignorePatterns: ['.eslintrc.js', 'node_modules'],
1818
rules: {
1919
'@typescript-eslint/interface-name-prefix': 'off',
2020
'@typescript-eslint/explicit-function-return-type': 'off',

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Finally, copy the contents of `.env.example` to a new file named `.env`. Replace
4141
$ npm install
4242
# Then apply the migrations
4343
$ npx prisma migrate dev
44+
# Optionally seed the databse with mock data
45+
$ npm run seed
4446
```
4547

4648
## Running the app

nest-cli.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"collection": "@nestjs/schematics",
3-
"sourceRoot": "src"
3+
"sourceRoot": "src",
4+
"compilerOptions": {
5+
"plugins": ["@nestjs/swagger"]
6+
}
47
}

package-lock.json

+61-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"prebuild": "rimraf dist",
1010
"build": "nest build",
1111
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
12+
"seed": "ts-node -r tsconfig-paths/register src/seed.ts",
1213
"start": "nest start",
1314
"start:dev": "nest start --watch",
1415
"start:debug": "nest start --debug --watch",
@@ -28,7 +29,9 @@
2829
"@nestjs/mapped-types": "^1.1.0",
2930
"@nestjs/passport": "^9.0.0",
3031
"@nestjs/platform-express": "^9.0.11",
32+
"@nestjs/swagger": "^6.0.5",
3133
"@prisma/client": "^4.2.1",
34+
"class-transformer": "^0.5.1",
3235
"class-validator": "^0.13.2",
3336
"dotenv": "^16.0.1",
3437
"passport-jwt": "^4.0.0",

src/app.controller.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Controller, Get } from '@nestjs/common'
1+
import { Get } from '@nestjs/common'
2+
import { ApiController } from 'src/utils/apiController.decorator'
23
import { AppService } from './app.service'
34

4-
@Controller()
5+
@ApiController()
56
export class AppController {
67
constructor(private readonly appService: AppService) {}
78

src/app.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AuthModule } from './auth/auth.module'
55
import { ConsultationsModule } from './consultations/consultations.module'
66
import { GroupsModule } from './groups/groups.module'
77
import { PrismaModule } from './prisma/prisma.module'
8+
import { SeederModule } from './seed/seeder.module'
89
import { SubjectModule } from './subject/subject.module'
910
import { UsersModule } from './users/users.module'
1011

@@ -16,6 +17,7 @@ import { UsersModule } from './users/users.module'
1617
SubjectModule,
1718
AuthModule,
1819
ConsultationsModule,
20+
SeederModule,
1921
],
2022
controllers: [AppController],
2123
providers: [AppService],

src/auth/auth.controller.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1-
import { Controller, Get, Logger, Redirect, UseGuards } from '@nestjs/common'
1+
import { Get, Redirect, UseGuards } from '@nestjs/common'
22
import { AuthGuard } from '@nestjs/passport'
33
import { User } from '@prisma/client'
44
import { CurrentUser } from 'src/current-user.decorator'
5+
import { ApiController } from 'src/utils/apiController.decorator'
56
import { AuthService } from './auth.service'
6-
import { JwtAuthGuard } from './jwt-auth.guard'
7+
import { JwtAuth } from './decorator/jwtAuth.decorator'
8+
import { JwtUserDto } from './dto/jwtUser.dto'
79

8-
@Controller('auth')
10+
@ApiController('auth')
911
export class AuthController {
10-
private readonly logger = new Logger(AuthController.name)
11-
1212
constructor(private authService: AuthService) {}
13-
13+
/**
14+
* Redirects to the authsch login page
15+
*/
1416
@UseGuards(AuthGuard('authsch'))
1517
@Get('login')
16-
async login(@CurrentUser() user: User) {
17-
return user
18+
login() {
19+
// never called
1820
}
19-
21+
/**
22+
* Endpoint for authsch to call after login
23+
*
24+
* Redirects to the frontend with the jwt token
25+
*/
2026
@Get('callback')
2127
@UseGuards(AuthGuard('authsch'))
2228
@Redirect()
23-
async oauthRedirect(@CurrentUser() user: User) {
29+
oauthRedirect(@CurrentUser() user: User) {
2430
const { jwt } = this.authService.login(user)
25-
return { url: `${process.env.FRONTEND_AUTHORIZED_URL}?jwt=${jwt}` }
31+
return {
32+
url: `${process.env.FRONTEND_AUTHORIZED_URL}?jwt=${jwt}`,
33+
}
2634
}
27-
35+
/**
36+
* Endpoint for jwt token validation
37+
*/
2838
@Get('me')
29-
@UseGuards(JwtAuthGuard)
30-
me(@CurrentUser() user: User) {
39+
@JwtAuth()
40+
me(@CurrentUser() user: JwtUserDto): JwtUserDto {
3141
return user
3242
}
3343
}

src/auth/auth.service.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Injectable } from '@nestjs/common'
1+
import { Injectable, Logger } from '@nestjs/common'
22
import { JwtService } from '@nestjs/jwt'
33
import { User } from '@prisma/client'
44
import { UsersService } from '../users/users.service'
55
import { OAuthUser } from './oauthuser'
66

77
@Injectable()
88
export class AuthService {
9+
private readonly logger = new Logger(AuthService.name)
10+
911
constructor(
1012
private readonly usersService: UsersService,
1113
private readonly jwtService: JwtService,
@@ -23,6 +25,9 @@ export class AuthService {
2325
lastName: oAuthUser.sn,
2426
email: oAuthUser.mail,
2527
})
28+
29+
this.logger.log(`Created new user ${newUser.id}`)
30+
2631
return newUser
2732
}
2833

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { applyDecorators, UseGuards } from '@nestjs/common'
2+
import { ApiBearerAuth } from '@nestjs/swagger'
3+
4+
import { Injectable } from '@nestjs/common'
5+
import { AuthGuard } from '@nestjs/passport'
6+
7+
@Injectable()
8+
class JwtAuthGuard extends AuthGuard('jwt') {}
9+
10+
export function JwtAuth() {
11+
return applyDecorators(UseGuards(JwtAuthGuard), ApiBearerAuth())
12+
}

src/auth/dto/jwtUser.dto.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { IsNotEmpty } from 'class-validator'
2+
import { UserEntity } from 'src/users/dto/UserEntity.dto'
3+
4+
export class JwtUserDto extends UserEntity {
5+
@IsNotEmpty()
6+
iat: number
7+
8+
@IsNotEmpty()
9+
exp: number
10+
}

src/auth/jwt-auth.guard.ts

-5
This file was deleted.

src/auth/oauthuser.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
export interface OAuthUser {
1+
import { IsEmail, IsNotEmpty, IsUUID } from 'class-validator'
2+
3+
export class OAuthUser {
4+
@IsNotEmpty()
25
sn: string
6+
7+
@IsNotEmpty()
38
givenName: string
9+
10+
@IsUUID('all')
411
internal_id: string
12+
13+
@IsEmail()
514
mail: string
15+
16+
constructor(partial: Partial<OAuthUser>) {
17+
Object.assign(this, partial)
18+
}
619
}

src/consultations/consultations.controller.spec.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Test, TestingModule } from '@nestjs/testing'
22
import { PrismaModule } from 'src/prisma/prisma.module'
3+
import { ConsultationRequestService } from './consultationRequest.service'
34
import { ConsultationsController } from './consultations.controller'
45
import { ConsultationsService } from './consultations.service'
6+
import { ParticipationService } from './participation.service'
7+
import { PresentationService } from './presentation.service'
8+
import { RatingService } from './rating.service'
59

610
describe('ConsultationsController', () => {
711
let controller: ConsultationsController
@@ -10,7 +14,13 @@ describe('ConsultationsController', () => {
1014
const module: TestingModule = await Test.createTestingModule({
1115
imports: [PrismaModule],
1216
controllers: [ConsultationsController],
13-
providers: [ConsultationsService],
17+
providers: [
18+
ConsultationsService,
19+
RatingService,
20+
ParticipationService,
21+
PresentationService,
22+
ConsultationRequestService,
23+
],
1424
}).compile()
1525

1626
controller = module.get<ConsultationsController>(ConsultationsController)

0 commit comments

Comments
 (0)