From 7bac7835d5e14262b1b32199ce22596ba7d14c50 Mon Sep 17 00:00:00 2001 From: zaverden Date: Mon, 10 May 2021 00:23:17 +0700 Subject: [PATCH] save user's events meta in user object related to #11 --- package-lock.json | 6 +-- package.json | 2 +- .../http/post-api-c-s-000publicId-e/index.ts | 4 +- src-ts/http/put-api-e-000publicId/index.ts | 9 ++--- src-ts/shared/handlers/auth-status.ts | 1 + src-ts/shared/user/storage.ts | 38 ++++++++++++++++++- 6 files changed, 49 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7e107fc..f9852b9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4202,9 +4202,9 @@ } }, "typescript": { - "version": "4.1.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", - "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.4.tgz", + "integrity": "sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg==", "dev": true }, "unbox-primitive": { diff --git a/package.json b/package.json index 12385c4..d7aeb21 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,6 @@ "prettier": "2.2.1", "runtypes": "5.0.1", "tiny-glob": "0.2.8", - "typescript": "4.1.3" + "typescript": "4.2.4" } } diff --git a/src-ts/http/post-api-c-s-000publicId-e/index.ts b/src-ts/http/post-api-c-s-000publicId-e/index.ts index 1f8a9ff..c916049 100644 --- a/src-ts/http/post-api-c-s-000publicId-e/index.ts +++ b/src-ts/http/post-api-c-s-000publicId-e/index.ts @@ -8,7 +8,7 @@ import { authorizeGoogleApi } from "@architect/shared/google/auth"; import { getAuthClient } from "@architect/shared/google/auth-client"; import { EventPayload, insertEvent } from "@architect/shared/google/calendar"; import { ensureEvents } from "@architect/shared/shacal-events"; -import { User } from "@architect/shared/user/storage"; +import { updateMyEvent, User } from "@architect/shared/user/storage"; import { emailMatch, getJWTCookieName, @@ -79,6 +79,8 @@ export const handler = withBaseUrl( user.userId ); + await updateMyEvent(user, ensuredEvents[0]!); + return { statusCode: 200, body: JSON.stringify({ diff --git a/src-ts/http/put-api-e-000publicId/index.ts b/src-ts/http/put-api-e-000publicId/index.ts index 1574892..452fe00 100644 --- a/src-ts/http/put-api-e-000publicId/index.ts +++ b/src-ts/http/put-api-e-000publicId/index.ts @@ -7,12 +7,9 @@ import { import { parseJsonBody } from "@architect/shared/body-parser"; import { authorizeGoogleApi } from "@architect/shared/google/auth"; import { getAuthClient } from "@architect/shared/google/auth-client"; -import { - EventPayload, - updateEvent, -} from "@architect/shared/google/calendar"; +import { EventPayload, updateEvent } from "@architect/shared/google/calendar"; import { getShacalEvent } from "@architect/shared/shacal-events"; -import { User } from "@architect/shared/user/storage"; +import { updateMyEvent, User } from "@architect/shared/user/storage"; import { getJWTCookieName, getJWTSecret } from "@architect/shared/utils"; export const handler = withBaseUrl( @@ -81,6 +78,8 @@ export const handler = withBaseUrl( attendees: rawEvent.attendees, }; + await updateMyEvent(user, event); + return { statusCode: 200, body: JSON.stringify({ diff --git a/src-ts/shared/handlers/auth-status.ts b/src-ts/shared/handlers/auth-status.ts index 9b87bd6..635b09a 100644 --- a/src-ts/shared/handlers/auth-status.ts +++ b/src-ts/shared/handlers/auth-status.ts @@ -11,6 +11,7 @@ export const handler = withOptionalUser( body: JSON.stringify({ authenticated: user != null, userEmail: user?.email, + myEvents: user == null ? undefined : Object.values(user.myEvents ?? {}), }), }; } diff --git a/src-ts/shared/user/storage.ts b/src-ts/shared/user/storage.ts index 47821c3..cd58f1d 100644 --- a/src-ts/shared/user/storage.ts +++ b/src-ts/shared/user/storage.ts @@ -4,11 +4,19 @@ import { getId } from "../utils"; const USER_TABLE = "USERS"; +const MyEvent = R.Record({ + publicId: R.String, + summary: R.String, + start: R.String, +}); +export type MyEvent = R.Static; + const User = R.Record({ userId: R.String, googleAccountId: R.String, email: R.String, sharedCalendars: R.Dictionary(R.String, "string"), + myEvents: R.Dictionary(MyEvent, "string").Or(R.Undefined), }); export type User = R.Static; @@ -29,7 +37,7 @@ export async function createUser( const userRecord = await D.set<{ data: User }>({ table: USER_TABLE, key: userId, - data: { userId, email, googleAccountId, sharedCalendars: {} }, + data: { userId, email, googleAccountId, sharedCalendars: {}, myEvents: {} }, }); return userRecord.data; } @@ -41,3 +49,31 @@ export async function updateUser(user: User): Promise { data: user, }); } + +export async function updateMyEvent(user: User, event: MyEvent): Promise { + const myEvents = user.myEvents ?? {}; + if ( + event.publicId in myEvents && + areEventsEqual(myEvents[event.publicId]!, event) + ) { + // nothing to update + return; + } + + // include only expected fields + const { publicId, start, summary } = event; + const updatedUser = { + ...user, + myEvents: { + ...user.myEvents, + [event.publicId]: { publicId, start, summary }, + }, + }; + await updateUser(updatedUser); +} + +function areEventsEqual(a: MyEvent, b: MyEvent): boolean { + return ( + a.publicId === b.publicId && a.start === b.start && a.summary === b.summary + ); +}