feat: add destination to the notification

This commit is contained in:
Nathnael
2026-07-06 08:08:07 +00:00
parent 8f5dfd83c5
commit 68c60f9e61
2 changed files with 53 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
import {
NotificationAudience,
NotificationChannels,
NotificationChannelsSent,
NotificationDto,
NotificationListResult,
@@ -144,18 +145,41 @@ export class NotificationInboxService {
const unreadCount = await this.repo.countUnread(userId);
this.gateway.emitNew(userId, this.toDto(entity), unreadCount);
if (priority === NotificationPriority.HIGH) {
const channelsSent = await this.fanOut(userId, input);
const channels = this.resolveChannels(input, priority);
if (channels.email || channels.sms) {
const channelsSent = await this.fanOut(userId, input, channels);
if (channelsSent) {
await this.repo.update(entity.id, { channelsSent });
}
}
}
/** Best-effort email/SMS fan-out for HIGH-priority items. Never throws. */
/**
* Decide which outbound channels to use. An explicit `input.channels`
* selection wins; otherwise fall back to priority (HIGH ⇒ email + SMS).
*/
private resolveChannels(
input: NotifyInput,
priority: NotificationPriority,
): Required<NotificationChannels> {
if (input.channels) {
return {
email: input.channels.email === true,
sms: input.channels.sms === true,
};
}
const high = priority === NotificationPriority.HIGH;
return { email: high, sms: high };
}
/**
* Best-effort email/SMS fan-out for the requested channels. Skips a channel
* the recipient has no address for. Never throws.
*/
private async fanOut(
userId: string,
input: NotifyInput,
channels: Required<NotificationChannels>,
): Promise<NotificationChannelsSent | null> {
try {
const user = await this.users.findOne({
@@ -166,7 +190,7 @@ export class NotificationInboxService {
const sent: NotificationChannelsSent = {};
const text = `${input.title}\n\n${input.body}`;
if (user.email) {
if (channels.email && user.email) {
const res = await this.emailClient.sendEmail({
to: user.email,
subject: input.title,
@@ -174,7 +198,7 @@ export class NotificationInboxService {
});
sent.email = res.queued;
}
if (user.phoneNumber) {
if (channels.sms && user.phoneNumber) {
const res = await this.smsClient.sendSms({
to: user.phoneNumber,
message: text,