-
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.
feature(drivers-service): create fleet driver earnings + type update (#…
…532) * feature(drivers-service): create fleet driver earnings + type update * feature: implement get all drivers payouts
- Loading branch information
Showing
15 changed files
with
297 additions
and
11 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
51 changes: 51 additions & 0 deletions
51
apps/payment-service/src/fleets-payout/fleets-payout.controller.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
FleetPayout, | ||
QUEUE_MESSAGE, | ||
RmqService | ||
|
||
} from '@app/common' | ||
import { Controller } from '@nestjs/common' | ||
import { | ||
Ctx, | ||
MessagePattern, | ||
Payload, | ||
RmqContext, | ||
RpcException | ||
} from '@nestjs/microservices' | ||
import { FleetPayoutService } from './fleets-payout.service' | ||
|
||
@Controller() | ||
export class FleetPayoutController { | ||
constructor ( | ||
private readonly fleetPayoutService: FleetPayoutService, | ||
private readonly rmqService: RmqService | ||
) {} | ||
|
||
@MessagePattern(QUEUE_MESSAGE.FLEET_GET_PAYOUT_DRIVER) | ||
async getDrivePayout ( | ||
@Payload() { driverId }: { driverId: string }, | ||
@Ctx() context: RmqContext | ||
): Promise<FleetPayout[]> { | ||
try { | ||
return await this.fleetPayoutService.getDriverPayout(driverId) | ||
} catch (error) { | ||
throw new RpcException(error) | ||
} finally { | ||
this.rmqService.ack(context) | ||
} | ||
} | ||
|
||
@MessagePattern(QUEUE_MESSAGE.FLEET_GET_ALL_PAYOUTS) | ||
async getAllDriversPayout ( | ||
@Payload() { organization }: { organization: string }, | ||
@Ctx() context: RmqContext | ||
): Promise<FleetPayout[]> { | ||
try { | ||
return await this.fleetPayoutService.getAllDriversPayout(organization) | ||
} catch (error) { | ||
throw new RpcException(error) | ||
} finally { | ||
this.rmqService.ack(context) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
apps/payment-service/src/fleets-payout/fleets-payout.respository.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { AbstractRepository, FleetPayout } from '@app/common' | ||
import { InjectModel } from '@nestjs/mongoose' | ||
import { Model } from 'mongoose' | ||
|
||
@Injectable() | ||
export class FleetPayoutRepository extends AbstractRepository<FleetPayout> { | ||
protected readonly logger = new Logger(FleetPayout.name) | ||
|
||
constructor ( | ||
@InjectModel(FleetPayout.name) payoutModel: Model<FleetPayout> | ||
) { | ||
super(payoutModel) | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
apps/payment-service/src/fleets-payout/fleets-payout.service.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { OrderStatus, QUEUE_MESSAGE, IRpcException, FitRpcException, RandomGen, FleetPayout, QUEUE_SERVICE, DeliveryI, Delivery, DriverI } from '@app/common' | ||
import { Inject, Injectable } from '@nestjs/common' | ||
import { ClientProxy } from '@nestjs/microservices' | ||
import { FilterQuery } from 'mongoose' | ||
import { lastValueFrom, catchError } from 'rxjs' | ||
import { FleetPayoutRepository } from './fleets-payout.respository' | ||
import { Cron, CronExpression } from '@nestjs/schedule' | ||
import { DriverRepository } from 'apps/drivers-service/src/drivers-service.repository' | ||
|
||
@Injectable() | ||
export class FleetPayoutService { | ||
constructor ( | ||
private readonly fleetPayoutRepository: FleetPayoutRepository, | ||
private readonly driverRepository: DriverRepository, | ||
@Inject(QUEUE_SERVICE.DRIVER_SERVICE) | ||
private readonly driverClient: ClientProxy | ||
|
||
) { | ||
|
||
} | ||
|
||
async getDriverPayout (driver: string): Promise<FleetPayout[]> { | ||
return await this.fleetPayoutRepository.findOneAndPopulate({ driver }, ['deliveries']) | ||
} | ||
|
||
async getAllDriversPayout (organization: string): Promise<FleetPayout[]> { | ||
const drivers: DriverI[] = await this.driverRepository.find({ organization }) | ||
|
||
const driverIds = drivers.map((driver) => driver._id) | ||
|
||
return await this.fleetPayoutRepository.findAndPopulate( | ||
{ driver: { $in: driverIds } }, | ||
['deliveries'] | ||
) | ||
} | ||
|
||
@Cron(CronExpression.EVERY_DAY_AT_10PM, { | ||
timeZone: 'Africa/Lagos' | ||
}) | ||
async handlePayoutComputation (): Promise<void> { | ||
const today = new Date() | ||
const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000) | ||
|
||
const start = new Date(yesterday) | ||
start.setHours(0, 0, 0, 0) | ||
|
||
const end = new Date(yesterday) | ||
end.setHours(23, 59, 59, 999) | ||
|
||
const filter: FilterQuery<Delivery> = { | ||
createdAt: { | ||
$gte: start.toISOString(), | ||
$lt: end.toISOString() | ||
}, | ||
status: OrderStatus.FULFILLED | ||
} | ||
const deliveries = await lastValueFrom<DeliveryI[]>( | ||
this.driverClient.send(QUEUE_MESSAGE.ADMIN_GET_DELIVERIES, filter).pipe( | ||
catchError((error: IRpcException) => { | ||
throw new FitRpcException(error.message, error.status) | ||
}) | ||
) | ||
) | ||
|
||
const deliveryId = deliveries.map((delivery) => delivery._id) | ||
|
||
// Compute earnings for each driver | ||
const driverEarnings = new Map<string, number>() | ||
|
||
deliveries.forEach((delivery) => { | ||
const driverId = delivery.driver._id.toString() | ||
const earnings = driverEarnings.get(driverId) ?? 0 | ||
driverEarnings.set( | ||
driverId, | ||
earnings + Number(delivery.deliveryFee) | ||
) | ||
}) | ||
|
||
const payoutsToMake: Array<Partial<FleetPayout>> = [] | ||
|
||
for (const [driverId, earnings] of driverEarnings) { | ||
payoutsToMake.push({ | ||
refId: RandomGen.genRandomNum(10, 7), | ||
driver: driverId, | ||
earnings, | ||
paid: false, | ||
deliveries: deliveryId | ||
}) | ||
} | ||
|
||
await this.fleetPayoutRepository.insertMany(payoutsToMake) | ||
} | ||
} |
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,32 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' | ||
import { AbstractDocument } from '../abstract.schema' | ||
import { Types } from 'mongoose' | ||
|
||
@Schema({ versionKey: false, timestamps: true }) | ||
export class FleetPayout extends AbstractDocument { | ||
@Prop({ | ||
type: Types.ObjectId, | ||
ref: 'Driver' | ||
}) | ||
public driver: string | ||
|
||
@Prop(Number) | ||
earnings: number | ||
|
||
@Prop({ type: Boolean, default: false }) | ||
paid: boolean | ||
|
||
@Prop(Date) | ||
updatedAt: string | ||
|
||
@Prop(Date) | ||
createdAt: string | ||
|
||
@Prop({ type: [Types.ObjectId], ref: 'Delivery' }) | ||
deliveries: string[] | ||
|
||
@Prop(Number) | ||
refId: number | ||
} | ||
|
||
export const FleetPayoutSchema = SchemaFactory.createForClass(FleetPayout) |
Oops, something went wrong.