-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Allow user scope to view old events #153
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; | ||
import { col, fn, Order } from "sequelize"; | ||
import { col, fn, Op, Order, WhereOptions } from "sequelize"; | ||
|
||
import type { AdminEventListResponse, EventListQuery, UserEventListResponse } from "@tietokilta/ilmomasiina-models"; | ||
import { adminEventListEventAttrs, eventListEventAttrs } from "@tietokilta/ilmomasiina-models/dist/attrs/event"; | ||
|
@@ -24,8 +24,20 @@ function eventOrder(): Order { | |
export const eventsListForUserCached = createCache({ | ||
maxAgeMs: 1000, | ||
maxPendingAgeMs: 2000, | ||
async get(category?: string) { | ||
const where = category ? { category } : {}; | ||
async get(options: { category?: string; since?: Date }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work with the current caching system, which compares the function argument with |
||
const { category, since } = options; | ||
const filters: WhereOptions = {}; | ||
if (category) { | ||
filters.category = category; | ||
} | ||
if (since) { | ||
filters.date = { | ||
[Op.gt]: since, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}; | ||
} | ||
const where = { | ||
[Op.and]: filters, | ||
}; | ||
|
||
const events = await Event.scope("user").findAll({ | ||
attributes: eventListEventAttrs, | ||
|
@@ -68,7 +80,7 @@ export async function getEventsListForUser( | |
throw new InitialSetupNeeded("Initial setup of Ilmomasiina is needed."); | ||
} | ||
|
||
const res = await eventsListForUserCached(request.query.category); | ||
const res = await eventsListForUserCached({ category: request.query.category, since: request.query.since }); | ||
reply.status(200); | ||
return res as StringifyApi<typeof res>; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,11 @@ export const eventListQuery = Type.Object({ | |
description: "If set, only events with the provided category are included.", | ||
}), | ||
), | ||
since: Type.Optional( | ||
Type.Date({ | ||
description: "If set, only events starting after this date are included.", | ||
}), | ||
), | ||
Comment on lines
+41
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
|
||
/** Query parameters applicable to the public event list API. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll still want a hard limit for regular users