|
|
@ -3,9 +3,9 @@ import type { RequestHandler } from './$types';
|
|
|
|
import type { Notification } from '$lib/notif';
|
|
|
|
import type { Notification } from '$lib/notif';
|
|
|
|
|
|
|
|
|
|
|
|
let notifications: Notification[] = [
|
|
|
|
let notifications: Notification[] = [
|
|
|
|
{ id: 1, text: "This is a notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
|
|
|
{ id: 1, text: "This is a notification", date: new Date(), status: [] },
|
|
|
|
{ id: 2, text: "This is another notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
|
|
|
{ id: 2, text: "This is another notification", date: new Date(), status: [] },
|
|
|
|
{ id: 3, text: "This is a third notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
|
|
|
{ id: 3, text: "This is a third notification", date: new Date(), status: [] },
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
// GET handler
|
|
|
|
// GET handler
|
|
|
@ -14,22 +14,26 @@ let notifications: Notification[] = [
|
|
|
|
|
|
|
|
|
|
|
|
export const GET: RequestHandler = ({ url }) => {
|
|
|
|
export const GET: RequestHandler = ({ url }) => {
|
|
|
|
const { searchParams } = new URL(url);
|
|
|
|
const { searchParams } = new URL(url);
|
|
|
|
const username = searchParams.get('username');
|
|
|
|
const user_id = searchParams.get('id')??"1";
|
|
|
|
if (!username) {
|
|
|
|
const date = searchParams.get('date')??"";
|
|
|
|
return error(400, 'username is required');
|
|
|
|
if (!user_id) {
|
|
|
|
|
|
|
|
//return error(400, 'username is required');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const user_notifications = notifications.filter(notification => {
|
|
|
|
if(!date){
|
|
|
|
// return true;
|
|
|
|
//return error(400, 'date is required');
|
|
|
|
const user_status = notification.status.find(status => status.username === username);
|
|
|
|
|
|
|
|
if (user_status?.status === "new") {
|
|
|
|
|
|
|
|
console.log("found one!")
|
|
|
|
|
|
|
|
user_status.status = "old";
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
|
|
|
|
// notifications.find(n => notification.id === n.id).status.find(status => status.username === username).status = "old";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Notifications that do not have a status entry for the current user and are newer than the date
|
|
|
|
|
|
|
|
const user_notifications = notifications.filter(notification => {
|
|
|
|
|
|
|
|
return notification.status.find(status => status == user_id) == undefined && notification.date > new Date(date);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Add the current user to the status list for each notification
|
|
|
|
|
|
|
|
user_notifications.forEach(notification => {
|
|
|
|
|
|
|
|
notification.status.push(user_id);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return new Response(JSON.stringify(user_notifications), { headers: { 'content-type': 'application/json' } });
|
|
|
|
return new Response(JSON.stringify(user_notifications), { headers: { 'content-type': 'application/json' } });
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -43,7 +47,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
|
|
|
|
|
|
|
|
|
|
|
notification.id = notifications.length + 1;
|
|
|
|
notification.id = notifications.length + 1;
|
|
|
|
notification.date = new Date();
|
|
|
|
notification.date = new Date();
|
|
|
|
notification.status = [{ username: "user1", status: "new" }, { username: "user2", status: "new" }];
|
|
|
|
notification.status = [];
|
|
|
|
|
|
|
|
|
|
|
|
notifications = [...notifications, notification];
|
|
|
|
notifications = [...notifications, notification];
|
|
|
|
|
|
|
|
|
|
|
|