-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anas Abbal
committed
Jun 19, 2024
1 parent
f4e3bd4
commit 8a84378
Showing
17 changed files
with
196 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthController', () => { | ||
let authController: AuthController; | ||
|
||
beforeEach(async () => { | ||
const app: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
providers: [AuthService], | ||
}).compile(); | ||
|
||
authController = app.get<AuthController>(AuthController); | ||
}); | ||
|
||
describe('root', () => { | ||
it('should return "Hello World!"', () => { | ||
expect(authController.getHello()).toBe('Hello World!'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Controller() | ||
export class AuthController { | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
return this.authService.getHello(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [AuthController], | ||
providers: [AuthService], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { AuthModule } from './auth.module'; | ||
import { MicroserviceOptions, Transport } from '@nestjs/microservices'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.createMicroservice<MicroserviceOptions>( | ||
AuthModule, | ||
{ | ||
transport: Transport.TCP, | ||
options: { | ||
port: 3005, | ||
}, | ||
}, | ||
); | ||
await app.listen(); | ||
} | ||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import * as request from 'supertest'; | ||
import { AuthModule } from './../src/auth.module'; | ||
|
||
describe('AuthController (e2e)', () => { | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AuthModule], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
it('/ (GET)', () => { | ||
return request(app.getHttpServer()) | ||
.get('/') | ||
.expect(200) | ||
.expect('Hello World!'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"moduleFileExtensions": ["js", "json", "ts"], | ||
"rootDir": ".", | ||
"testEnvironment": "node", | ||
"testRegex": ".e2e-spec.ts$", | ||
"transform": { | ||
"^.+\\.(t|j)s$": "ts-jest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": false, | ||
"outDir": "../../dist/apps/auth" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { UserCreateCommand } from "@app/shared/commands/auth/user.create.cmd"; | ||
import { DriverCreateCmd } from "@app/shared/commands/driver/driver.create.cmd"; | ||
import { BadRequestException, ConflictException, Inject, Injectable, InternalServerErrorException, UnauthorizedException } from "@nestjs/common"; | ||
import { ClientProxy } from "@nestjs/microservices"; | ||
import { UserService } from "./user-service"; | ||
import { DriverService } from "./driver-service"; | ||
|
||
|
||
|
||
|
||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
@Inject('USER_SERVICE') private readonly userClient: ClientProxy, | ||
private readonly userService: UserService, | ||
private readonly driverService: DriverService | ||
){} | ||
|
||
async register(userTypeId: string, command: UserCreateCommand | DriverCreateCmd): Promise<string> { | ||
try { | ||
const userType = await this.userService.findUserTypeById(userTypeId); | ||
console.log(`User type with id ${userType.id} found`); | ||
if(userType.id == userTypeId){ | ||
return await this.driverService.createDriver(command); | ||
}else { | ||
return await this.userService.createUser(command, userTypeId); | ||
} | ||
} catch (error) { | ||
// log the error for debugging purposes | ||
console.error('Error occurred during user registration:', error); | ||
// decide how to handle different types of errors | ||
if (error.response && error.response.status === 409) { | ||
throw new ConflictException('User with this email already exists'); | ||
} else if (error.response && error.response.status === 400) { | ||
throw new BadRequestException('Invalid user data provided'); | ||
} else { | ||
// if the error is unexpected, throw an Internal Server Error | ||
throw new InternalServerErrorException('Failed to register user'); | ||
} | ||
} | ||
} | ||
async login(loginUserDto: any): Promise<any> { | ||
try { | ||
const result = await this.userClient.send({ cmd: 'login' }, loginUserDto).toPromise(); | ||
return result; | ||
} catch (error) { | ||
// log the error for debugging purposes | ||
console.error('Error occurred during user login:', error); | ||
|
||
// decide how to handle different types of errors | ||
if (error.response && error.response.status === 401) { | ||
throw new UnauthorizedException('Invalid credentials'); | ||
} else { | ||
// if the error is unexpected, throw an Internal Server Error | ||
throw new InternalServerErrorException('Failed to login'); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters