Skip to content
Merged
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
4 changes: 1 addition & 3 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TestValidationController } from './test-validation.controller';
import { NotificationsModule } from './notifications/notifications.module';
import { OffersModule } from './offers/offers.module';

Expand All @@ -35,7 +36,7 @@ import { OffersModule } from './offers/offers.module';
NotificationsModule,
OffersModule,
],
controllers: [AppController],
controllers: [AppController, TestValidationController],
providers: [AppService],
})
export class AppModule {}
2 changes: 1 addition & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, BadRequestException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { RegisterDto } from './dtos/register.dto';
import { User } from 'src/user/user.entity';
import { User } from '../user/user.entity';
import * as bcrypt from 'bcryptjs';
import { generateVerificationToken, verifyJwtToken } from '../utils/jwt.util';
import { MailService } from '../mail/mail.service';
Expand Down
14 changes: 13 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ async function bootstrap() {
credentials: true,
});

await app.listen(process.env.PORT ?? 3000);
// Global Validation Pipe
const { ValidationPipe } = await import('@nestjs/common');
app.useGlobalPipes(new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}));

// Global Exception Filter
const { AllExceptionsFilter } = await import('./utils/all-exceptions.filter');
app.useGlobalFilters(new AllExceptionsFilter());

await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
25 changes: 25 additions & 0 deletions src/notifications/notifications.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Test, TestingModule } from '@nestjs/testing';
import { NotificationsService } from './notifications.service';
import { NotificationsGateway } from './notifications.gateway';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Notification } from './entities/notification.entity';
import { Repository } from 'typeorm';

describe('NotificationsService', () => {
let service: NotificationsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
NotificationsService,
NotificationsGateway,
{ provide: getRepositoryToken(Notification), useClass: Repository },
],
}).compile();
service = module.get<NotificationsService>(NotificationsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
4 changes: 2 additions & 2 deletions src/password-reset/password-reset.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { PasswordResetController } from './password-reset.controller';
import { PasswordResetService } from './password-reset.service';
import { PasswordResetToken } from './entities/password-reset-token.entity';
import { EmailModule } from '../email/email.module';
import { throttlerConfig } from 'src/config/throttler.config';
import { UserModule } from 'src/user/user.module';
import { throttlerConfig } from '../config/throttler.config';
import { UserModule } from '../user/user.module';

@Module({
imports: [
Expand Down
18 changes: 18 additions & 0 deletions src/test-validation.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller, Post, Body } from '@nestjs/common';
import { IsEmail, IsNotEmpty } from 'class-validator';

class TestDto {
@IsEmail()
email: string;

@IsNotEmpty()
name: string;
}

@Controller('test-validation')
export class TestValidationController {
@Post()
test(@Body() dto: TestDto) {
return { message: 'Validation passed', data: dto };
}
}
47 changes: 47 additions & 0 deletions src/utils/all-exceptions.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { Logger } from '@nestjs/common';

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
private readonly logger = new Logger(AllExceptionsFilter.name);

catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();

let status = HttpStatus.INTERNAL_SERVER_ERROR;
let message = 'Internal server error';
let errorResponse: any = {};

if (exception instanceof HttpException) {
status = exception.getStatus();
const exceptionResponse = exception.getResponse();
if (typeof exceptionResponse === 'string') {
message = exceptionResponse;
} else if (typeof exceptionResponse === 'object') {
errorResponse = exceptionResponse;
message = (exceptionResponse as any).message || message;
}
} else if (exception instanceof Error) {
message = exception.message;
}

this.logger.error(`Status: ${status} Error: ${message}`, exception instanceof Error ? exception.stack : '');

response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
message,
...errorResponse,
});
}
}
Loading