mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-27 00:52:50 +00:00
fix issue
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { Injectable, Logger, NotFoundException } from "@nestjs/common";
|
||||
import {
|
||||
Injectable,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
ServiceUnavailableException,
|
||||
} from "@nestjs/common";
|
||||
import { EmailNotificationStrategy } from "./strategies/notification.email.strategy";
|
||||
import { NotificationStrategy } from "./strategies/notification.strategy";
|
||||
import { SmsNotificationStrategy } from "./strategies/notification.sms.strategy";
|
||||
@@ -27,8 +32,19 @@ export class NotificationsService {
|
||||
if (!strategy) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
// A strategy returning false (or throwing) is a real delivery failure — do
|
||||
// not swallow it. Surface it so callers observe the failure (existing
|
||||
// callers wrap directSend in try/catch for best-effort notifications).
|
||||
const sent = await strategy.send(recipient, message);
|
||||
this.logger.log(`is sent - ${sent}`);
|
||||
if (!sent) {
|
||||
this.logger.error(
|
||||
`Notification via ${method} to ${recipient} failed to send`,
|
||||
);
|
||||
throw new ServiceUnavailableException(
|
||||
`Failed to send ${method} notification`,
|
||||
);
|
||||
}
|
||||
this.logger.log(`Notification via ${method} to ${recipient} sent`);
|
||||
}
|
||||
|
||||
async notifyDriverVehicleAssignment(params: {
|
||||
|
||||
@@ -1,12 +1,28 @@
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { NotificationStrategy } from "./notification.strategy";
|
||||
import { EmailClientService } from "../email-client.service";
|
||||
|
||||
@Injectable()
|
||||
export class EmailNotificationStrategy implements NotificationStrategy {
|
||||
private readonly logger = new Logger(EmailNotificationStrategy.name);
|
||||
constructor() { }
|
||||
constructor(private readonly emailClient: EmailClientService) { }
|
||||
async send(recipient: string, message: string): Promise<boolean> {
|
||||
this.logger.log(`${recipient}, ${message}`)
|
||||
return false;
|
||||
try {
|
||||
// Route through the shared email client (RabbitMQ hand-off). `queued`
|
||||
// reflects whether the message was accepted for delivery; a false or
|
||||
// a thrown result is a real failure the caller must observe.
|
||||
const { queued } = await this.emailClient.sendEmail({
|
||||
to: recipient,
|
||||
subject: "EDR Freight notification",
|
||||
text: message,
|
||||
});
|
||||
return queued;
|
||||
} catch (err) {
|
||||
this.logger.error(
|
||||
`Failed to send email to ${recipient}: ${err instanceof Error ? err.message : String(err)}`,
|
||||
err instanceof Error ? err.stack : undefined,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user