Skip to content

Commit

Permalink
Merge pull request #11 from anasabbal/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
anasabbal authored Jun 28, 2024
2 parents 3650ab2 + 84d5e3b commit 1569627
Show file tree
Hide file tree
Showing 39 changed files with 1,053 additions and 360 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

strategy:
matrix:
app: [driver, notification, gateway, ride, user-service, auth]
app: [driver, notification, gateway, ride, user-service, auth, user]

steps:
- name: Checkout code
Expand Down
7 changes: 5 additions & 2 deletions apps/auth/src/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Injectable } from '@nestjs/common';
import { UserCreateCommand } from '@app/shared/commands/auth/user.create.cmd';
import { IResponse } from '@app/shared/interfaces/response.interface';
import { validateCommand } from '@app/shared/utils/validate';
import { BadRequestException, Injectable } from '@nestjs/common';

@Injectable()
export class AuthService {

}
15 changes: 7 additions & 8 deletions apps/driver/src/driver.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ import { Controller, Get } from '@nestjs/common';
import { DriverService } from './driver.service';
import { MessagePattern } from '@nestjs/microservices';
import { DriverDto } from '@app/shared/events/driver/driver.dto';
import { mapDriversToDtos } from './utils/driver.mapper';
import { DriverCreateCmd } from '@app/shared/commands/driver/driver.create.cmd';
import { Driver } from './models/driver.entity';

@Controller('drivers')
export class DriverController {
constructor(private readonly driverService: DriverService) {}

@MessagePattern({ cmd: 'create'})
async createDriver(driverCmd: DriverCreateCmd): Promise<string> {
this.driverService.createDriver(driverCmd);
async createDriver(userId: string): Promise<string> {
this.driverService.createDriver(userId);
return "driver created successfully";
}

@MessagePattern({ cmd: 'get_all_driver'})
async findAllDriver(): Promise<DriverDto[]> {
async findAllDriver(): Promise<Driver[]> {
const drivers = await this.driverService.findAllDrivers();
return mapDriversToDtos(drivers);
return drivers;
}

@MessagePattern({ cmd: 'get_all_sorted_by_status'})
async getAllDrivers(): Promise<DriverDto[]> {
async getAllDrivers(): Promise<Driver[]> {
const drivers = await this.driverService.findAllSortedByStatus();
return mapDriversToDtos(drivers);
return drivers
}
}
21 changes: 9 additions & 12 deletions apps/driver/src/driver.module.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { Module } from '@nestjs/common';
import { DatabaseModule } from '@app/database';
import { MongooseModule } from '@nestjs/mongoose';
import { DriverController } from './driver.controller';
import { DriverService } from './driver.service';
import { Driver, DriverSchema } from './models/driver.schema';
import { LocationSchema, Location } from './models/location.schema';
import { VehicleSchema, Vehicle } from './models/vehicle.schema';
import * as dotenv from 'dotenv';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Driver } from './models/driver.entity';


dotenv.config();

@Module({
imports: [
MongooseModule.forFeature(
[
{ name: Driver.name, schema: DriverSchema },
{ name: Location.name, schema: LocationSchema },
{ name: Vehicle.name, schema: VehicleSchema }
]
),
DatabaseModule.forRoot(process.env.MONGODB_URI, 'drivers'),
TypeOrmModule.forFeature([Driver]),
DatabaseModule.forRoot(
process.env.DATABASE_URI,
process.env.DATABASE_NAME,
process.env.DATABASE_TYPE as 'mongodb' | 'postgres'
),
],
controllers: [DriverController],
providers: [DriverService],
Expand Down
53 changes: 20 additions & 33 deletions apps/driver/src/driver.service.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,48 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import * as bcrypt from 'bcrypt';
import { Driver } from './models/driver.schema';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { DriverStatus } from './enums/driver.status';
import { DriverCreateCmd } from '@app/shared/commands/driver/driver.create.cmd';
import { Driver } from './models/driver.entity';



@Injectable()
export class DriverService {

private readonly logger = new Logger(DriverService.name);

constructor(
@InjectModel('Driver') private readonly driverModel: Model<Driver>
){}
@InjectRepository(Driver)
private readonly driverRepository: Repository<Driver>
) {}

async createDriver(driverCmd: DriverCreateCmd): Promise<Driver> {
this.logger.log(`Begin creating driver with payload: ${JSON.stringify(driverCmd)}`);
async createDriver(userId: string): Promise<Driver> {
this.logger.log(`Begin creating driver with userId: ${userId}`);

const hashedPassword = await this.hashPassword(driverCmd.password);
const driverData = this.buildDriverData(driverCmd, hashedPassword);

const savedDriver = await this.saveDriver(driverData);

this.logger.log(`Driver with firstName created: ${JSON.stringify(savedDriver)}`);
return savedDriver;
}
const driverData = this.buildDriverData(userId);

const savedDriver = await this.driverRepository.save(driverData);

private async hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, 10);
this.logger.log(`Driver with userId created: ${JSON.stringify(savedDriver)}`);
return savedDriver;
}

private buildDriverData(driverCmd: DriverCreateCmd, hashedPassword: string): Partial<Driver> {
private buildDriverData(userId: string): Partial<Driver> {
return {
email: driverCmd.email,
password: hashedPassword,
firstName: driverCmd.firstName,
lastName: driverCmd.lastName,
driverStatus: DriverStatus.EMPTY,
user_id: userId
};
}

private async saveDriver(driverData: Partial<Driver>): Promise<Driver> {
const driver = new this.driverModel(driverData);
return driver.save();
}
async findAllDrivers(): Promise<Driver[]> {
try {
return await this.driverModel.find().exec();
return await this.driverRepository.find();
} catch (error) {
this.logger.error(`Error while fetching drivers from database: ${error.message}`);
throw error;
}
}

async findAllSortedByStatus(): Promise<Driver[]> {

try {
const drivers = await this.findAllDrivers();
const order = [
Expand All @@ -71,6 +59,7 @@ export class DriverService {
throw error;
}
}

private sortDriversByStatus(drivers: Driver[], order: DriverStatus[]): Driver[] {
try {
// create a custom sort function based on the order array
Expand All @@ -87,6 +76,4 @@ export class DriverService {
throw error;
}
}


}
40 changes: 40 additions & 0 deletions apps/driver/src/models/driver.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Entity, PrimaryGeneratedColumn, Column, BeforeInsert } from 'typeorm';
import { v4 as uuidv4 } from 'uuid';
import { DriverStatus } from '../enums/driver.status';

@Entity()
export class Driver {

@BeforeInsert()
generateId() {
this.id = uuidv4();
}

@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ type: 'integer' })
user_id: string;

@Column({ type: 'varchar', length: 50 })
license_number: string;

@Column({ type: 'integer', nullable: true })
vehicle_id: number;

@Column({ type: 'float', nullable: true })
rating: number;

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created_at: Date;

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
updated_at: Date;

@Column({
type: 'enum',
enum: DriverStatus,
default: DriverStatus.EMPTY
})
driverStatus: DriverStatus;
}
56 changes: 0 additions & 56 deletions apps/driver/src/models/driver.schema.ts

This file was deleted.

29 changes: 0 additions & 29 deletions apps/driver/src/models/location.schema.ts

This file was deleted.

33 changes: 0 additions & 33 deletions apps/driver/src/models/vehicle.schema.ts

This file was deleted.

9 changes: 0 additions & 9 deletions apps/driver/src/utils/daba.config.ts

This file was deleted.

18 changes: 1 addition & 17 deletions apps/driver/src/utils/driver.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
import { DriverDto } from "@app/shared/events/driver/driver.dto";
import { Driver } from "../models/driver.schema";
import { Driver } from "typeorm";



export function mapDriverToDto(driver: Driver): DriverDto {
const driverDto = new DriverDto();

driverDto.firstName = driver.firstName;
driverDto.lastName = driver.lastName;
driverDto.email = driver.email;
driverDto.password = driver.password;
driverDto.driverStatus = driver.driverStatus;

return driverDto;
}

export function mapDriversToDtos(drivers: Driver[]): DriverDto[] {
return drivers.map(driver => mapDriverToDto(driver));
}
Loading

0 comments on commit 1569627

Please sign in to comment.