diff --git a/src-ts/shared/handlers/auth-status.ts b/src-ts/shared/handlers/auth-status.ts index 635b09a..6e35173 100644 --- a/src-ts/shared/handlers/auth-status.ts +++ b/src-ts/shared/handlers/auth-status.ts @@ -1,11 +1,33 @@ import { HttpFunctionResponse } from "@architect/shared/begin"; import { withOptionalUser } from "../auth"; +import { updateUser, User } from "../user/storage"; import { getJWTCookieName, getJWTSecret } from "../utils"; +async function invalidateMyEvents(user: User): Promise { + let hasExpired = false; + const myEvents = user.myEvents ?? {}; + const now = new Date(); + for (const event of Object.values(myEvents)) { + const end = new Date(event.end); + if (end < now) { + delete myEvents[event.publicId]; + hasExpired = true; + } + } + + if (hasExpired) { + await updateUser(user); + } +} + export const handler = withOptionalUser( getJWTCookieName(), getJWTSecret(), async (_, user): Promise => { + if (user) { + await invalidateMyEvents(user); + } + return { statusCode: 200, body: JSON.stringify({ diff --git a/src-ts/shared/user/storage.ts b/src-ts/shared/user/storage.ts index cd58f1d..2bd66ab 100644 --- a/src-ts/shared/user/storage.ts +++ b/src-ts/shared/user/storage.ts @@ -7,7 +7,7 @@ const USER_TABLE = "USERS"; const MyEvent = R.Record({ publicId: R.String, summary: R.String, - start: R.String, + end: R.String, }); export type MyEvent = R.Static; @@ -61,12 +61,12 @@ export async function updateMyEvent(user: User, event: MyEvent): Promise { } // include only expected fields - const { publicId, start, summary } = event; + const { publicId, end, summary } = event; const updatedUser = { ...user, myEvents: { ...user.myEvents, - [event.publicId]: { publicId, start, summary }, + [event.publicId]: { publicId, end, summary }, }, }; await updateUser(updatedUser); @@ -74,6 +74,6 @@ export async function updateMyEvent(user: User, event: MyEvent): Promise { function areEventsEqual(a: MyEvent, b: MyEvent): boolean { return ( - a.publicId === b.publicId && a.start === b.start && a.summary === b.summary + a.publicId === b.publicId && a.end === b.end && a.summary === b.summary ); }