forked from DigiNodes/truthbounty-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathin-app.channel.ts
More file actions
49 lines (40 loc) · 1.75 KB
/
Copy pathin-app.channel.ts
File metadata and controls
49 lines (40 loc) · 1.75 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
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { NotificationChannel as ChannelType, Notification } from '../entities/notification.entity';
import { UserNotificationPreferences } from '../entities/notification.entity';
import { NotificationChannel, ChannelDeliveryResult } from './channel.interface';
@Injectable()
export class InAppChannel implements NotificationChannel {
private readonly logger = new Logger(InAppChannel.name);
readonly channelType = ChannelType.IN_APP;
constructor(
@InjectRepository(UserNotificationPreferences)
private readonly preferencesRepository: Repository<UserNotificationPreferences>,
) {}
async isEnabled(userId: string): Promise<boolean> {
const preferences = await this.preferencesRepository.findOne({
where: { userId },
});
if (!preferences) {
return true; // Default to enabled if no preferences set
}
return preferences.enabledChannels[this.channelType] ?? true;
}
async send(notification: Notification): Promise<ChannelDeliveryResult> {
this.logger.debug(
`Sending in-app notification ${notification.id} to user ${notification.recipientId}`,
);
// In-app notifications are just stored in the database, they're retrieved via API
// The WebSocket server will broadcast the new notification to connected clients
return {
success: true,
deliveryTimestamp: new Date(),
};
}
async validateConfig(userId: string): Promise<{ valid: boolean; errors: string[] }> {
const errors: string[] = [];
// In-app channel always has a valid config since it doesn't require any user configuration
return { valid: true, errors };
}
}