forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaboration-notification-dispatcher.ts
More file actions
141 lines (122 loc) · 5.26 KB
/
Copy pathcollaboration-notification-dispatcher.ts
File metadata and controls
141 lines (122 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Injectable, Logger } from '@nestjs/common';
import { PushNotificationsService } from '../../push-notifications/push-notifications.service';
import { NotificationEventType } from '../../push-notifications/entities/notification-preference.entity';
import { ActivitiesService } from '../../modules/activities/activities.service';
import { ActivityType } from '../../entities/activity.entity';
import {
CollaborationInvitationNotification,
CollaborationRemovalNotification,
CollaborationResponseNotification,
} from './notification.service';
export interface DispatchResult {
push: boolean;
inApp: boolean;
failures: string[];
}
/**
* Fans collaboration events out to push and in-app channels.
* Each channel is attempted independently so one failure does not block others.
*/
@Injectable()
export class CollaborationNotificationDispatcher {
private readonly logger = new Logger(CollaborationNotificationDispatcher.name);
constructor(
private readonly pushService: PushNotificationsService,
private readonly activitiesService: ActivitiesService,
) {}
async dispatchInvitation(
recipientWallet: string,
notification: CollaborationInvitationNotification,
): Promise<DispatchResult> {
const result: DispatchResult = { push: false, inApp: false, failures: [] };
await this.tryPush(result, recipientWallet, NotificationEventType.PAYMENT_RECEIVED, {
title: 'Collaboration Invitation',
body: `You've been invited to collaborate on "${notification.trackTitle}" as ${notification.role}.`,
data: { collaborationId: notification.collaborationId, type: 'collaboration_invitation' },
});
await this.tryInApp(result, recipientWallet, ActivityType.PARTICIPANT_ADDED, {
collaborationId: notification.collaborationId,
inviterWallet: notification.inviterWallet,
trackTitle: notification.trackTitle,
role: notification.role,
});
this.logOutcome('invitation', recipientWallet, result);
return result;
}
async dispatchResponse(
recipientWallet: string,
notification: CollaborationResponseNotification,
): Promise<DispatchResult> {
const result: DispatchResult = { push: false, inApp: false, failures: [] };
await this.tryPush(result, recipientWallet, NotificationEventType.PAYMENT_RECEIVED, {
title: 'Collaboration Response',
body: `${notification.artistName} has ${notification.status} your collaboration invitation.`,
data: { collaborationId: notification.collaborationId, type: 'collaboration_response' },
});
await this.tryInApp(result, recipientWallet, ActivityType.PAYMENT_RECEIVED, {
collaborationId: notification.collaborationId,
artistWallet: notification.artistWallet,
status: notification.status,
});
this.logOutcome('response', recipientWallet, result);
return result;
}
async dispatchRemoval(
recipientWallet: string,
notification: CollaborationRemovalNotification,
): Promise<DispatchResult> {
const result: DispatchResult = { push: false, inApp: false, failures: [] };
await this.tryPush(result, recipientWallet, NotificationEventType.PAYMENT_RECEIVED, {
title: 'Removed from Collaboration',
body: `You have been removed from a collaboration by ${notification.removerWallet}.`,
data: { collaborationId: notification.collaborationId, type: 'collaboration_removal' },
});
await this.tryInApp(result, recipientWallet, ActivityType.SPLIT_EDITED, {
collaborationId: notification.collaborationId,
removerWallet: notification.removerWallet,
reason: notification.removalReason,
});
this.logOutcome('removal', recipientWallet, result);
return result;
}
// ── Private helpers ────────────────────────────────────────────────────────
private async tryPush(
result: DispatchResult,
userId: string,
eventType: NotificationEventType,
payload: { title: string; body: string; data: Record<string, string> },
): Promise<void> {
try {
await this.pushService.sendNotification(userId, eventType, payload.title, payload.body, payload.data);
result.push = true;
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
result.failures.push(`push: ${msg}`);
this.logger.warn(`Push delivery failed for ${userId}: ${msg}`);
}
}
private async tryInApp(
result: DispatchResult,
userId: string,
activityType: ActivityType,
metadata: Record<string, unknown>,
): Promise<void> {
try {
await this.activitiesService.createActivity({ userId, activityType, metadata });
result.inApp = true;
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
result.failures.push(`in-app: ${msg}`);
this.logger.warn(`In-app activity creation failed for ${userId}: ${msg}`);
}
}
private logOutcome(event: string, recipient: string, result: DispatchResult): void {
if (result.failures.length === 0) {
this.logger.log(`Collaboration ${event} dispatched to ${recipient} via push+in-app`);
} else {
this.logger.error(
`Collaboration ${event} partial failure for ${recipient}: ${result.failures.join('; ')}`,
);
}
}
}