@@ -5,7 +5,6 @@ import { zodToJsonSchema } from "zod-to-json-schema";
55import { OrganizationList } from "../../common/orgs.js" ;
66import {
77 DeleteItemCommand ,
8- DynamoDBClient ,
98 GetItemCommand ,
109 PutItemCommand ,
1110 QueryCommand ,
@@ -27,6 +26,7 @@ import { IUpdateDiscord, updateDiscord } from "../functions/discord.js";
2726// POST
2827
2928const repeatOptions = [ "weekly" , "biweekly" ] as const ;
29+ const EVENT_CACHE_SECONDS = 90 ;
3030export type EventRepeatOptions = ( typeof repeatOptions ) [ number ] ;
3131
3232const baseSchema = z . object ( {
@@ -80,10 +80,6 @@ const getEventsSchema = z.array(getEventSchema);
8080export type EventsGetResponse = z . infer < typeof getEventsSchema > ;
8181type EventsGetQueryParams = { upcomingOnly ?: boolean } ;
8282
83- const dynamoClient = new DynamoDBClient ( {
84- region : genericConfig . AwsRegion ,
85- } ) ;
86-
8783const eventsPlugin : FastifyPluginAsync = async ( fastify , _options ) => {
8884 fastify . post < { Body : EventPostRequest } > (
8985 "/:id?" ,
@@ -106,7 +102,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
106102 ) . id ;
107103 const entryUUID = userProvidedId || randomUUID ( ) ;
108104 if ( userProvidedId ) {
109- const response = await dynamoClient . send (
105+ const response = await fastify . dynamoClient . send (
110106 new GetItemCommand ( {
111107 TableName : genericConfig . EventsDynamoTableName ,
112108 Key : { id : { S : userProvidedId } } ,
@@ -128,7 +124,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
128124 : new Date ( ) . toISOString ( ) ,
129125 updatedAt : new Date ( ) . toISOString ( ) ,
130126 } ;
131- await dynamoClient . send (
127+ await fastify . dynamoClient . send (
132128 new PutItemCommand ( {
133129 TableName : genericConfig . EventsDynamoTableName ,
134130 Item : marshall ( entry ) ,
@@ -140,18 +136,23 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
140136 }
141137 try {
142138 if ( request . body . featured && ! request . body . repeats ) {
143- await updateDiscord ( entry , false , request . log ) ;
139+ await updateDiscord (
140+ fastify . secretsManagerClient ,
141+ entry ,
142+ false ,
143+ request . log ,
144+ ) ;
144145 }
145146 } catch ( e : unknown ) {
146147 // restore original DB status if Discord fails.
147- await dynamoClient . send (
148+ await fastify . dynamoClient . send (
148149 new DeleteItemCommand ( {
149150 TableName : genericConfig . EventsDynamoTableName ,
150151 Key : { id : { S : entryUUID } } ,
151152 } ) ,
152153 ) ;
153154 if ( userProvidedId ) {
154- await dynamoClient . send (
155+ await fastify . dynamoClient . send (
155156 new PutItemCommand ( {
156157 TableName : genericConfig . EventsDynamoTableName ,
157158 Item : originalEvent ,
@@ -198,7 +199,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
198199 async ( request : FastifyRequest < EventGetRequest > , reply ) => {
199200 const id = request . params . id ;
200201 try {
201- const response = await dynamoClient . send (
202+ const response = await fastify . dynamoClient . send (
202203 new QueryCommand ( {
203204 TableName : genericConfig . EventsDynamoTableName ,
204205 KeyConditionExpression : "#id = :id" ,
@@ -241,13 +242,18 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
241242 async ( request : FastifyRequest < EventDeleteRequest > , reply ) => {
242243 const id = request . params . id ;
243244 try {
244- await dynamoClient . send (
245+ await fastify . dynamoClient . send (
245246 new DeleteItemCommand ( {
246247 TableName : genericConfig . EventsDynamoTableName ,
247248 Key : marshall ( { id } ) ,
248249 } ) ,
249250 ) ;
250- await updateDiscord ( { id } as IUpdateDiscord , true , request . log ) ;
251+ await updateDiscord (
252+ fastify . secretsManagerClient ,
253+ { id } as IUpdateDiscord ,
254+ true ,
255+ request . log ,
256+ ) ;
251257 reply . send ( {
252258 id,
253259 resource : `/api/v1/events/${ id } ` ,
@@ -285,8 +291,20 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
285291 } ,
286292 async ( request : FastifyRequest < EventsGetRequest > , reply ) => {
287293 const upcomingOnly = request . query ?. upcomingOnly || false ;
294+ const cachedResponse = fastify . nodeCache . get (
295+ `events-upcoming_only=${ upcomingOnly } ` ,
296+ ) ;
297+ if ( cachedResponse ) {
298+ reply
299+ . header (
300+ "cache-control" ,
301+ "public, max-age=7200, stale-while-revalidate=900, stale-if-error=86400" ,
302+ )
303+ . header ( "acm-cache-status" , "hit" )
304+ . send ( cachedResponse ) ;
305+ }
288306 try {
289- const response = await dynamoClient . send (
307+ const response = await fastify . dynamoClient . send (
290308 new ScanCommand ( { TableName : genericConfig . EventsDynamoTableName } ) ,
291309 ) ;
292310 const items = response . Items ?. map ( ( item ) => unmarshall ( item ) ) ;
@@ -322,11 +340,17 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
322340 }
323341 } ) ;
324342 }
343+ fastify . nodeCache . set (
344+ `events-upcoming_only=${ upcomingOnly } ` ,
345+ parsedItems ,
346+ EVENT_CACHE_SECONDS ,
347+ ) ;
325348 reply
326349 . header (
327350 "cache-control" ,
328351 "public, max-age=7200, stale-while-revalidate=900, stale-if-error=86400" ,
329352 )
353+ . header ( "acm-cache-status" , "miss" )
330354 . send ( parsedItems ) ;
331355 } catch ( e : unknown ) {
332356 if ( e instanceof Error ) {
0 commit comments