Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 40 additions & 90 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"@nestjs/terminus": "^11.0.0",
"@prisma/adapter-pg": "^7.0.0",
"@prisma/client": "^7.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.4",
"dotenv": "^17.0.0",
"express-prom-bundle": "^8.0.0",
"helmet": "^8.0.0",
Expand Down
6 changes: 3 additions & 3 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dotenv/config'
import type { MiddlewareConsumer } from '@nestjs/common'
import { Module, RequestMethod } from '@nestjs/common'
import { HTTPLoggerMiddleware } from './middleware/req.res.logger'
import { PrismaService } from './prisma.service'
import { PrismaModule } from './prisma.module'
import { ConfigModule } from '@nestjs/config'
import { UsersModule } from './users/users.module'
import { AppService } from './app.service'
Expand All @@ -12,9 +12,9 @@ import { TerminusModule } from '@nestjs/terminus'
import { HealthController } from './health.controller'

@Module({
imports: [ConfigModule.forRoot(), TerminusModule, UsersModule],
imports: [ConfigModule.forRoot({ isGlobal: true }), TerminusModule, PrismaModule, UsersModule],
controllers: [AppController, MetricsController, HealthController],
providers: [AppService, PrismaService],
providers: [AppService],
})
export class AppModule {
// let's add a middleware on all routes
Expand Down
12 changes: 11 additions & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppModule } from './app.module'
import { customLogger } from './common/logger.config'
import type { NestExpressApplication } from '@nestjs/platform-express'
import helmet from 'helmet'
import { VersioningType } from '@nestjs/common'
import { ValidationPipe, VersioningType } from '@nestjs/common'
import { metricsMiddleware } from './middleware/prom'

/**
Expand All @@ -15,6 +15,16 @@ export async function bootstrap() {
logger: customLogger,
})
app.use(helmet())
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
Comment thread
mishraomp marked this conversation as resolved.
transform: true,
transformOptions: {
enableImplicitConversion: true,
},
}),
)
app.enableCors()
app.set('trust proxy', 1)
app.use(metricsMiddleware)
Expand Down
4 changes: 2 additions & 2 deletions backend/src/health.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { PrismaClient } from '../generated/prisma/client.js'
@Controller('health')
export class HealthController {
constructor(
private health: HealthCheckService,
private prisma: PrismaHealthIndicator,
private readonly health: HealthCheckService,
private readonly prisma: PrismaHealthIndicator,
private readonly prismaService: PrismaService,
) {}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/middleware/req.res.logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Injectable, Logger } from '@nestjs/common'

@Injectable()
export class HTTPLoggerMiddleware implements NestMiddleware {
private logger = new Logger('HTTP')
private readonly logger = new Logger('HTTP')

use(request: Request, response: Response, next: NextFunction): void {
const { method, originalUrl } = request
Expand Down
2 changes: 1 addition & 1 deletion backend/src/prisma.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('PrismaService', () => {
})

afterEach(async () => {
await service.$disconnect()
await service.onModuleDestroy()
})

it('should be defined', () => {
Expand Down
10 changes: 3 additions & 7 deletions backend/src/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ const PrismaClientWithLogs = PrismaClient as unknown as new (

@Injectable()
class PrismaService extends PrismaClientWithLogs implements OnModuleInit, OnModuleDestroy {
private logger = new Logger('PRISMA')
private static instance: PrismaService
private pool!: Pool
private readonly logger = new Logger('PRISMA')
private readonly pool: Pool
constructor() {
if (PrismaService.instance) {
return PrismaService.instance
}
const pool = new Pool({ connectionString: dataSourceURL })
const adapter = new PrismaPg(pool, { schema: DB_SCHEMA })
super({
Expand All @@ -41,7 +37,6 @@ class PrismaService extends PrismaClientWithLogs implements OnModuleInit, OnModu
],
})
this.pool = pool
PrismaService.instance = this
}

async onModuleInit() {
Expand All @@ -59,6 +54,7 @@ class PrismaService extends PrismaClientWithLogs implements OnModuleInit, OnModu

async onModuleDestroy() {
await this.$disconnect()
await this.pool.end()
}
}

Expand Down
25 changes: 25 additions & 0 deletions backend/src/users/dto/search-users-query.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Type } from 'class-transformer'
import { IsInt, IsJSON, IsOptional, Max, Min } from 'class-validator'

export class SearchUsersQueryDto {
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
page = 1

@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
@Max(200)
limit = 10

@IsOptional()
@IsJSON()
sort = '[]'

@IsOptional()
@IsJSON()
filter = '[]'
}
3 changes: 2 additions & 1 deletion backend/src/users/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PartialType } from '@nestjs/swagger'
import { CreateUserDto } from './create-user.dto'

export class UpdateUserDto extends CreateUserDto {}
export class UpdateUserDto extends PartialType(CreateUserDto) {}
14 changes: 11 additions & 3 deletions backend/src/users/dto/user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { ApiProperty } from '@nestjs/swagger'
import { Transform } from 'class-transformer'
import { IsEmail, IsInt, IsNotEmpty, IsPositive, IsString, MaxLength } from 'class-validator'

export class UserDto {
@ApiProperty({
description: 'The ID of the user',
// default: '9999',
})
@IsInt()
@IsPositive()
id!: number

@ApiProperty({
description: 'The name of the user',
// default: 'username',
})
@Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
@IsString()
@IsNotEmpty()
@MaxLength(200)
name!: string

@ApiProperty({
description: 'The contact email of the user',
default: '',
})
@Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
@IsEmail()
@MaxLength(200)
email!: string
}
Loading
Loading