fix notifications not clearing
This commit is contained in:
parent
edca4d5ceb
commit
6956149a3d
4 changed files with 44 additions and 28 deletions
|
|
@ -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;
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -55,20 +55,18 @@ 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) {
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,29 @@ const REWARD_TIERS = [
|
||||||
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 = {
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
Reference in a new issue