Skip to content

Commit

Permalink
save user's events meta in user object
Browse files Browse the repository at this point in the history
related to #11
  • Loading branch information
zaverden committed May 9, 2021
1 parent a2ad71c commit 7bac783
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 3 additions & 1 deletion src-ts/http/post-api-c-s-000publicId-e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -79,6 +79,8 @@ export const handler = withBaseUrl(
user.userId
);

await updateMyEvent(user, ensuredEvents[0]!);

return {
statusCode: 200,
body: JSON.stringify({
Expand Down
9 changes: 4 additions & 5 deletions src-ts/http/put-api-e-000publicId/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -81,6 +78,8 @@ export const handler = withBaseUrl(
attendees: rawEvent.attendees,
};

await updateMyEvent(user, event);

return {
statusCode: 200,
body: JSON.stringify({
Expand Down
1 change: 1 addition & 0 deletions src-ts/shared/handlers/auth-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? {}),
}),
};
}
Expand Down
38 changes: 37 additions & 1 deletion src-ts/shared/user/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof MyEvent>;

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<typeof User>;

Expand All @@ -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;
}
Expand All @@ -41,3 +49,31 @@ export async function updateUser(user: User): Promise<void> {
data: user,
});
}

export async function updateMyEvent(user: User, event: MyEvent): Promise<void> {
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
);
}

0 comments on commit 7bac783

Please sign in to comment.