Skip to content

Commit

Permalink
changed to pull latest 4 months of events (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanT102 authored Aug 7, 2024
1 parent 3c30bca commit 5cc5a06
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions services/events/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,24 @@ export const del = async (event, ctx, callback) => {
}
};

// /events
// TODO: Implement better way to optimize this query
export const getAll = async (event, ctx, callback) => {
try {
const fourMonthsAgo = new Date();
fourMonthsAgo.setMonth(fourMonthsAgo.getMonth() - 4);
const fourMonthsAgoTimestamp = fourMonthsAgo.getTime();

let filterExpression = {
FilterExpression: "#createdAt >= :fourMonthsAgo",
ExpressionAttributeNames: {
"#createdAt": "createdAt"
},
ExpressionAttributeValues: {
":fourMonthsAgo": fourMonthsAgoTimestamp
}
};

//Set up query by year if exists
// Add year filter if it exists
if (
event &&
event.queryStringParameters &&
Expand All @@ -156,20 +167,15 @@ export const getAll = async (event, ctx, callback) => {
event.queryStringParameters
);

filterExpression = {
FilterExpression: "#vyear = :query",
ExpressionAttributeNames: {
"#vyear": "year"
},
ExpressionAttributeValues: {
":query": year
}
};
filterExpression.FilterExpression += " AND #vyear = :year";
filterExpression.ExpressionAttributeNames["#vyear"] = "year";
filterExpression.ExpressionAttributeValues[":year"] = year;
}

// scan
let events = await db.scan(EVENTS_TABLE, filterExpression);

// Filter by ID if provided
if (
event &&
event.queryStringParameters &&
Expand All @@ -181,11 +187,12 @@ export const getAll = async (event, ctx, callback) => {
}

// get event counts
for (event of events) {
event.counts = await eventHelpers.getEventCounts(
event.id, event.year
for (const eventItem of events) {
eventItem.counts = await eventHelpers.getEventCounts(
eventItem.id, eventItem.year
);
}

// sort the events by startDate
events.sort(alphabeticalComparer("startDate"));

Expand Down

0 comments on commit 5cc5a06

Please sign in to comment.