Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class LoanPaymentReminderService implements OnModuleInit {
'send-payment-reminders',
{},
{
repeat: { pattern: '0 9 * * *', utcOffset: 0 },
repeat: { pattern: '0 9 * * *', offset: 0 },
removeOnComplete: { count: 10 },
removeOnFail: { count: 50 },
},
Expand Down
58 changes: 53 additions & 5 deletions src/modules/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,26 @@ export class NotificationsService {
const offset = query.offset ?? 0;
const client = this.supabaseService.getServiceRoleClient();

// Get user_id from wallet
const { data: user, error: userError } = await client
.from('users')
.select('id')
.eq('wallet_address', wallet)
.single();

if (userError || !user) {
throw new NotFoundException({
code: 'USER_NOT_FOUND',
message: 'User not found',
});
}

const userId = user.id;

let notificationsQuery = client
.from('notifications')
.select('id, type, title, message, data, is_read, created_at, read_at', { count: 'exact' })
.eq('user_wallet', wallet)
.eq('user_id', userId)
.order('created_at', { ascending: false })
.range(offset, offset + limit - 1);

Expand All @@ -42,7 +58,7 @@ export class NotificationsService {
const { count: unreadCount, error: unreadError } = await client
.from('notifications')
.select('id', { count: 'exact', head: true })
.eq('user_wallet', wallet)
.eq('user_id', userId)
.eq('is_read', false);

if (unreadError) {
Expand Down Expand Up @@ -75,9 +91,25 @@ export class NotificationsService {
async markAsRead(wallet: string, notificationId: string): Promise<MarkAsReadResponseDto> {
const client = this.supabaseService.getServiceRoleClient();

// Get user_id from wallet
const { data: user, error: userError } = await client
.from('users')
.select('id')
.eq('wallet_address', wallet)
.single();

if (userError || !user) {
throw new NotFoundException({
code: 'USER_NOT_FOUND',
message: 'User not found',
});
}

const userId = user.id;

const { data: notification, error: fetchError } = await client
.from('notifications')
.select('id, user_wallet, is_read')
.select('id, user_id, is_read')
.eq('id', notificationId)
.single();

Expand All @@ -88,7 +120,7 @@ export class NotificationsService {
});
}

if (notification.user_wallet !== wallet) {
if (notification.user_id !== userId) {
throw new ForbiddenException({
code: 'NOTIFICATION_FORBIDDEN',
message: 'You do not have permission to update this notification',
Expand Down Expand Up @@ -116,11 +148,27 @@ export class NotificationsService {
async markAllAsRead(wallet: string): Promise<MarkAsReadResponseDto> {
const client = this.supabaseService.getServiceRoleClient();

// Get user_id from wallet
const { data: user, error: userError } = await client
.from('users')
.select('id')
.eq('wallet_address', wallet)
.single();

if (userError || !user) {
throw new NotFoundException({
code: 'USER_NOT_FOUND',
message: 'User not found',
});
}

const userId = user.id;

const now = new Date().toISOString();
const { data, error } = await client
.from('notifications')
.update({ is_read: true, read_at: now, updated_at: now })
.eq('user_wallet', wallet)
.eq('user_id', userId)
.eq('is_read', false)
.select('id');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ CREATE TABLE notifications (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
type text NOT NULL,
payload jsonb,
read boolean DEFAULT false,
created_at timestamp DEFAULT now()
title text NOT NULL,
message text NOT NULL,
data jsonb,
is_read boolean DEFAULT false,
created_at timestamp DEFAULT now(),
read_at timestamp
);

CREATE INDEX idx_notifications_user_id_read ON notifications(user_id, read);
CREATE INDEX idx_notifications_user_id_is_read ON notifications(user_id, is_read);
CREATE INDEX idx_notifications_created_at ON notifications(created_at);
CREATE INDEX idx_notifications_type ON notifications(type);

Expand Down
Loading