This commit is contained in:
natib21
2026-06-29 12:01:19 +00:00
parent 835facc65a
commit 5c36c09dae
2 changed files with 26 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ import { FindOptionsWhere } from 'typeorm';
import { BookingsRepository } from '../bookings/bookings.repository'; import { BookingsRepository } from '../bookings/bookings.repository';
import { DriversService } from '../drivers/drivers.service'; import { DriversService } from '../drivers/drivers.service';
import { NotificationsService } from '../notifications/notifications.service'; import { NotificationsService } from '../notifications/notifications.service';
import { SmsClientService } from '../notifications/sms-client.service';
import { VehiclesService } from '../vehicles/vehicles.service'; import { VehiclesService } from '../vehicles/vehicles.service';
import { CreateFirstMileDto } from './dto/create-first-mile.dto'; import { CreateFirstMileDto } from './dto/create-first-mile.dto';
import { UpdateFirstMileDto } from './dto/update-first-mile.dto'; import { UpdateFirstMileDto } from './dto/update-first-mile.dto';
@@ -37,6 +38,7 @@ export class FirstMileService {
private readonly vehiclesService: VehiclesService, private readonly vehiclesService: VehiclesService,
private readonly driversService: DriversService, private readonly driversService: DriversService,
private readonly notificationsService: NotificationsService, private readonly notificationsService: NotificationsService,
private readonly smsClient: SmsClientService,
) {} ) {}
/** /**
@@ -251,16 +253,19 @@ export class FirstMileService {
const booking = (record as FirstMile & { booking?: { reference?: string; firstMilePickupAddress?: string | null; originYard?: { label?: string } | null } }).booking; const booking = (record as FirstMile & { booking?: { reference?: string; firstMilePickupAddress?: string | null; originYard?: { label?: string } | null } }).booking;
await this.notificationsService.notifyDriverVehicleAssignment({ const driverName = `${driver.firstName ?? ''} ${driver.lastName ?? ''}`.trim();
driverPhone: driver.phoneNumber, const message =
driverName: `${driver.firstName ?? ''} ${driver.lastName ?? ''}`.trim(), `Dear ${driverName}, you have been assigned to a first-mile pickup. ` +
vehiclePlateNumber: vehicle.plateNumber ?? vehicleId, `Booking: ${booking?.reference ?? record.bookingId}. Vehicle: ${vehicle.plateNumber ?? vehicleId}. ` +
bookingReference: booking?.reference ?? record.bookingId, (booking?.firstMilePickupAddress ? `Pickup: ${booking.firstMilePickupAddress}. ` : '') +
pickupAddress: booking?.firstMilePickupAddress, (booking?.originYard?.label ? `Destination: ${booking.originYard.label}.` : '');
destinationYard: booking?.originYard?.label,
void this.smsClient.sendSms({
to: driver.phoneNumber,
message,
}); });
this.logger.log(`SMS sent to driver ${driver.phoneNumber} for vehicle ${vehicleId} assignment`); this.logger.log(`SMS queued to driver ${driver.phoneNumber} for vehicle ${vehicleId} assignment`);
} catch (err) { } catch (err) {
this.logger.error(`Failed to notify driver for vehicle ${vehicleId}: ${String(err)}`); this.logger.error(`Failed to notify driver for vehicle ${vehicleId}: ${String(err)}`);
} }

View File

@@ -4,6 +4,7 @@ import { FindOptionsWhere } from 'typeorm';
import { BookingsRepository } from '../bookings/bookings.repository'; import { BookingsRepository } from '../bookings/bookings.repository';
import { DriversService } from '../drivers/drivers.service'; import { DriversService } from '../drivers/drivers.service';
import { NotificationsService } from '../notifications/notifications.service'; import { NotificationsService } from '../notifications/notifications.service';
import { SmsClientService } from '../notifications/sms-client.service';
import { VehiclesService } from '../vehicles/vehicles.service'; import { VehiclesService } from '../vehicles/vehicles.service';
import { CreateLastMileDto } from './dto/create-last-mile.dto'; import { CreateLastMileDto } from './dto/create-last-mile.dto';
import { UpdateLastMileDto } from './dto/update-last-mile.dto'; import { UpdateLastMileDto } from './dto/update-last-mile.dto';
@@ -32,12 +33,12 @@ export class LastMileService {
private readonly logger = new Logger(LastMileService.name); private readonly logger = new Logger(LastMileService.name);
constructor( constructor(
private readonly lastMileRepository: LastMileRepository, private readonly lastMileRepository: LastMileRepository,
private readonly bookingsRepository: BookingsRepository, private readonly bookingsRepository: BookingsRepository,
private readonly vehiclesService: VehiclesService, private readonly vehiclesService: VehiclesService,
private readonly driversService: DriversService, private readonly driversService: DriversService,
private readonly notificationsService: NotificationsService, private readonly notificationsService: NotificationsService,
private readonly smsClient: SmsClientService,
) {} ) {}
async acceptBooking(bookingReference: string): Promise<LastMile | null> { async acceptBooking(bookingReference: string): Promise<LastMile | null> {
@@ -185,16 +186,19 @@ export class LastMileService {
}; };
const booking = (record as LastMile & { booking?: BookingWithYards }).booking; const booking = (record as LastMile & { booking?: BookingWithYards }).booking;
await this.notificationsService.notifyDriverVehicleAssignment({ const driverName = `${driver.firstName ?? ''} ${driver.lastName ?? ''}`.trim();
driverPhone: driver.phoneNumber, const message =
driverName: `${driver.firstName ?? ''} ${driver.lastName ?? ''}`.trim(), `Dear ${driverName}, you have been assigned to a last-mile delivery. ` +
vehiclePlateNumber: vehicle.plateNumber ?? vehicleId, `Booking: ${booking?.reference ?? record.bookingId}. Vehicle: ${vehicle.plateNumber ?? vehicleId}. ` +
bookingReference: booking?.reference ?? record.bookingId, (booking?.destinationYard?.label ? `Pickup: ${booking.destinationYard.label}. ` : '') +
pickupAddress: booking?.destinationYard?.label, (booking?.lastMileDeliveryAddress ? `Destination: ${booking.lastMileDeliveryAddress}.` : '');
destinationYard: booking?.lastMileDeliveryAddress,
void this.smsClient.sendSms({
to: driver.phoneNumber,
message,
}); });
this.logger.log(`SMS sent to driver ${driver.phoneNumber} for vehicle ${vehicleId} assignment`); this.logger.log(`SMS queued to driver ${driver.phoneNumber} for vehicle ${vehicleId} assignment`);
} catch (err) { } catch (err) {
this.logger.error(`Failed to notify driver for vehicle ${vehicleId}: ${String(err)}`); this.logger.error(`Failed to notify driver for vehicle ${vehicleId}: ${String(err)}`);
} }