fix notifications not clearing

This commit is contained in:
Face 2025-07-15 19:11:50 +03:00
parent edca4d5ceb
commit 6956149a3d
4 changed files with 44 additions and 28 deletions

View file

@ -2,7 +2,7 @@ import { writable, derived } from 'svelte/store';
export interface Notification { export interface Notification {
id: number; id: number;
type: string; type: 'HOPIUM' | 'TRANSFER' | 'RUG_PULL' | 'SYSTEM';
title: string; title: string;
message: string; message: string;
link?: string; link?: string;
@ -24,10 +24,10 @@ export async function fetchNotifications(unreadOnly = false) {
if (!response.ok) throw new Error('Failed to fetch notifications'); if (!response.ok) throw new Error('Failed to fetch notifications');
const data = await response.json(); const data = await response.json();
NOTIFICATIONS.set(data.notifications); NOTIFICATIONS.set(data.notifications);
UNREAD_COUNT.set(data.unreadCount); UNREAD_COUNT.set(data.unreadCount);
return data; return data;
} catch (error) { } catch (error) {
console.error('Failed to fetch notifications:', error); console.error('Failed to fetch notifications:', error);
@ -35,23 +35,21 @@ export async function fetchNotifications(unreadOnly = false) {
} }
} }
export async function markNotificationsAsRead(ids: number[]) { export async function markNotificationsAsRead() {
try { try {
const response = await fetch('/api/notifications', { const response = await fetch('/api/notifications', {
method: 'PATCH', method: 'PATCH',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids, markAsRead: true }) body: JSON.stringify({ markAsRead: true })
}); });
if (!response.ok) throw new Error('Failed to mark notifications as read'); if (!response.ok) throw new Error('Failed to mark notifications as read');
NOTIFICATIONS.update(notifications => NOTIFICATIONS.update(notifications =>
notifications.map(notif => notifications.map(notif => ({ ...notif, isRead: true }))
ids.includes(notif.id) ? { ...notif, isRead: true } : notif
)
); );
UNREAD_COUNT.update(count => Math.max(0, count - ids.length)); UNREAD_COUNT.set(0);
} catch (error) { } catch (error) {
console.error('Failed to mark notifications as read:', error); console.error('Failed to mark notifications as read:', error);
@ -59,4 +57,4 @@ export async function markNotificationsAsRead(ids: number[]) {
} }
} }
export const hasUnreadNotifications = derived(UNREAD_COUNT, count => count > 0); export const hasUnreadNotifications = derived(UNREAD_COUNT, count => count > 0);

View file

@ -55,24 +55,22 @@ export const PATCH: RequestHandler = async ({ request }) => {
if (!session?.user) throw error(401, 'Not authenticated'); if (!session?.user) throw error(401, 'Not authenticated');
const userId = Number(session.user.id); const userId = Number(session.user.id);
const { ids, markAsRead } = await request.json(); const { markAsRead } = await request.json();
if (!Array.isArray(ids) || typeof markAsRead !== 'boolean') { if (typeof markAsRead !== 'boolean') {
throw error(400, 'Invalid request body'); throw error(400, 'Invalid request body');
} }
try { try {
await db if (markAsRead) {
.update(notifications) await db.update(notifications)
.set({ isRead: markAsRead }) .set({ isRead: true })
.where(and( .where(eq(notifications.userId, userId));
eq(notifications.userId, userId), }
inArray(notifications.id, ids)
));
return json({ success: true }); return json({ success: true });
} catch (e) { } catch (e) {
console.error('Failed to update notifications:', e); console.error('Failed to update notifications:', e);
throw error(500, 'Failed to update notifications'); throw error(500, 'Failed to update notifications');
} }
}; };

View file

@ -13,10 +13,32 @@ const REWARD_TIERS = [
1500, // Day 2 1500, // Day 2
1800, // Day 3 1800, // Day 3
2100, // Day 4 2100, // Day 4
2500, // Day 5 2500, // Day 5
3000, // Day 6 3000, // Day 6
3500, // Day 7 3500, // Day 7
4000, // Day 8+ 4000, // Day 8
4200, // Day 9
4400, // Day 10
4600, // Day 11
4800, // Day 12
5000, // Day 13
5200, // Day 14
5400, // Day 15
5600, // Day 16
5800, // Day 17
6000, // Day 18
6200, // Day 19
6400, // Day 20
6600, // Day 21
6800, // Day 22
7000, // Day 23
7200, // Day 24
7400, // Day 25
7600, // Day 26
7800, // Day 27
8000, // Day 28
8200, // Day 29
8500 // Day 30+
]; ];
const PRESTIGE_MULTIPLIERS = { const PRESTIGE_MULTIPLIERS = {

View file

@ -33,9 +33,7 @@
const unreadIds = ($NOTIFICATIONS || []).filter((n) => !n.isRead).map((n) => n.id); const unreadIds = ($NOTIFICATIONS || []).filter((n) => !n.isRead).map((n) => n.id);
newNotificationIds = unreadIds; newNotificationIds = unreadIds;
if (unreadIds.length > 0) { await markNotificationsAsRead();
await markNotificationsAsRead(unreadIds);
}
} catch (error) { } catch (error) {
toast.error('Failed to load notifications'); toast.error('Failed to load notifications');
} finally { } finally {