From 63b9d0c43461df9cfe6f0be16fa34d64280997ca Mon Sep 17 00:00:00 2001 From: Dev Singh Date: Tue, 3 Sep 2024 10:16:34 -0500 Subject: [PATCH] add timestamps on event creation (#13) * add timestamps on event creation * fix tests * fix 2 --- src/config.ts | 16 ++++++++-------- src/roles.ts | 4 +++- src/routes/events.ts | 14 +++++++++----- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/config.ts b/src/config.ts index 43569aa..2ca4edf 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,4 @@ -import { AppRoles, RunEnvironment } from "./roles.js"; +import { allAppRoles, AppRoles, RunEnvironment } from "./roles.js"; import { OriginFunction } from "@fastify/cors"; // From @fastify/cors @@ -37,11 +37,11 @@ const genericConfig: GenericConfigType = { const environmentConfig: EnvironmentConfigType = { dev: { GroupRoleMapping: { - "48591dbc-cdcb-4544-9f63-e6b92b067e33": [AppRoles.EVENTS_MANAGER], // Infra Chairs - "940e4f9e-6891-4e28-9e29-148798495cdb": [AppRoles.EVENTS_MANAGER], // ACM Infra Team - "f8dfc4cf-456b-4da3-9053-f7fdeda5d5d6": [AppRoles.EVENTS_MANAGER], // Infra Leads - "0": [AppRoles.EVENTS_MANAGER], // Dummy Group for development only - "1": [AppRoles.PUBLIC], // Dummy Group for development only + "48591dbc-cdcb-4544-9f63-e6b92b067e33": allAppRoles, // Infra Chairs + "940e4f9e-6891-4e28-9e29-148798495cdb": allAppRoles, // ACM Infra Team + "f8dfc4cf-456b-4da3-9053-f7fdeda5d5d6": allAppRoles, // Infra Leads + "0": allAppRoles, // Dummy Group for development only + "1": [], // Dummy Group for development only }, AzureRoleMapping: { AutonomousWriters: [AppRoles.EVENTS_MANAGER] }, ValidCorsOrigins: [ @@ -56,8 +56,8 @@ const environmentConfig: EnvironmentConfigType = { }, prod: { GroupRoleMapping: { - "48591dbc-cdcb-4544-9f63-e6b92b067e33": [AppRoles.EVENTS_MANAGER], // Infra Chairs - "ff49e948-4587-416b-8224-65147540d5fc": [AppRoles.EVENTS_MANAGER], // Officers + "48591dbc-cdcb-4544-9f63-e6b92b067e33": allAppRoles, // Infra Chairs + "ff49e948-4587-416b-8224-65147540d5fc": allAppRoles, // Officers "ad81254b-4eeb-4c96-8191-3acdce9194b1": [AppRoles.EVENTS_MANAGER], // Exec }, AzureRoleMapping: { AutonomousWriters: [AppRoles.EVENTS_MANAGER] }, diff --git a/src/roles.ts b/src/roles.ts index 57ae7ff..e571592 100644 --- a/src/roles.ts +++ b/src/roles.ts @@ -3,5 +3,7 @@ export const runEnvironments = ["dev", "prod"] as const; export type RunEnvironment = (typeof runEnvironments)[number]; export enum AppRoles { EVENTS_MANAGER = "manage:events", - PUBLIC = "public", } +export const allAppRoles = Object.values(AppRoles).filter( + (value) => typeof value === "string", +); diff --git a/src/routes/events.ts b/src/routes/events.ts index ef208bb..893b7ae 100644 --- a/src/routes/events.ts +++ b/src/routes/events.ts @@ -8,6 +8,7 @@ import { DynamoDBClient, GetItemCommand, PutItemCommand, + QueryCommand, ScanCommand, } from "@aws-sdk/client-dynamodb"; import { genericConfig } from "../config.js"; @@ -111,18 +112,21 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => { Key: { id: { S: userProvidedId } }, }), ); - if (!response.Item) { + originalEvent = response.Item; + if (!originalEvent) { throw new ValidationError({ message: `${userProvidedId} is not a valid event ID.`, }); - } else { - originalEvent = response.Item; } } const entry = { ...request.body, id: entryUUID, createdBy: request.username, + createdAt: originalEvent + ? originalEvent.createdAt || new Date().toISOString() + : new Date().toISOString(), + updatedAt: new Date().toISOString(), }; await dynamoClient.send( new PutItemCommand({ @@ -189,9 +193,9 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => { const id = request.params.id; try { const response = await dynamoClient.send( - new ScanCommand({ + new QueryCommand({ TableName: genericConfig.DynamoTableName, - FilterExpression: "#id = :id", + KeyConditionExpression: "#id = :id", ExpressionAttributeNames: { "#id": "id", },