@@ -24,6 +24,11 @@ import { randomUUID } from "crypto";
2424import moment from "moment-timezone" ;
2525import { IUpdateDiscord , updateDiscord } from "../functions/discord.js" ;
2626import rateLimiter from "api/plugins/rateLimiter.js" ;
27+ import {
28+ atomicIncrementCacheCounter ,
29+ deleteCacheCounter ,
30+ getCacheCounter ,
31+ } from "api/functions/cache.js" ;
2732
2833const repeatOptions = [ "weekly" , "biweekly" ] as const ;
2934const CLIENT_HTTP_CACHE_POLICY = `public, max-age=${ EVENT_CACHED_DURATION } , stale-while-revalidate=420, stale-if-error=3600` ;
@@ -137,6 +142,11 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
137142 TableName : genericConfig . EventsDynamoTableName ,
138143 } ) ;
139144 }
145+ const etag = await getCacheCounter (
146+ fastify . dynamoClient ,
147+ "events-etag-all" ,
148+ ) ;
149+ reply . header ( "etag" , etag ) ;
140150 const response = await fastify . dynamoClient . send ( command ) ;
141151 const items = response . Items ?. map ( ( item ) => unmarshall ( item ) ) ;
142152 const currentTimeChicago = moment ( ) . tz ( "America/Chicago" ) ;
@@ -277,6 +287,18 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
277287 }
278288 throw new DiscordEventError ( { } ) ;
279289 }
290+ await atomicIncrementCacheCounter (
291+ fastify . dynamoClient ,
292+ `events-etag-${ entryUUID } ` ,
293+ 1 ,
294+ false ,
295+ ) ;
296+ await atomicIncrementCacheCounter (
297+ fastify . dynamoClient ,
298+ "events-etag-all" ,
299+ 1 ,
300+ false ,
301+ ) ;
280302 reply . status ( 201 ) . send ( {
281303 id : entryUUID ,
282304 resource : `/api/v1/events/${ entryUUID } ` ,
@@ -335,6 +357,7 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
335357 message : "Failed to delete event from Dynamo table." ,
336358 } ) ;
337359 }
360+ await deleteCacheCounter ( fastify . dynamoClient , `events-etag-${ id } ` ) ;
338361 request . log . info (
339362 { type : "audit" , actor : request . username , target : id } ,
340363 `deleted event "${ id } "` ,
@@ -372,7 +395,11 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
372395 if ( ! ts ) {
373396 reply . header ( "Cache-Control" , CLIENT_HTTP_CACHE_POLICY ) ;
374397 }
375- return reply . send ( item ) ;
398+ const etag = await getCacheCounter (
399+ fastify . dynamoClient ,
400+ `events-etag-${ id } ` ,
401+ ) ;
402+ return reply . header ( "etag" , etag ) . send ( item ) ;
376403 } catch ( e ) {
377404 if ( e instanceof BaseError ) {
378405 throw e ;
0 commit comments