Skip to content

Commit

Permalink
clean up expired events from user object
Browse files Browse the repository at this point in the history
related to #11
  • Loading branch information
zaverden committed May 10, 2021
1 parent 7bac783 commit ad9da9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src-ts/shared/handlers/auth-status.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<HttpFunctionResponse> => {
if (user) {
await invalidateMyEvents(user);
}

return {
statusCode: 200,
body: JSON.stringify({
Expand Down
8 changes: 4 additions & 4 deletions src-ts/shared/user/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof MyEvent>;

Expand Down Expand Up @@ -61,19 +61,19 @@ export async function updateMyEvent(user: User, event: MyEvent): Promise<void> {
}

// 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);
}

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
);
}

0 comments on commit ad9da9e

Please sign in to comment.