From 170844c99e8e50144c113fbf618befdf346b0958 Mon Sep 17 00:00:00 2001 From: Joonatan Korpela Date: Thu, 24 Oct 2024 16:13:54 +0300 Subject: [PATCH] feat: Allow user scope to view old events --- .../ilmomasiina-backend/src/models/event.ts | 15 ------------- .../src/routes/events/getEventDetails.ts | 21 ++++++++++++++++++- .../src/routes/events/getEventsList.ts | 20 ++++++++++++++---- .../src/schema/eventList/index.ts | 5 +++++ 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/packages/ilmomasiina-backend/src/models/event.ts b/packages/ilmomasiina-backend/src/models/event.ts index c23b81ee..95558db4 100644 --- a/packages/ilmomasiina-backend/src/models/event.ts +++ b/packages/ilmomasiina-backend/src/models/event.ts @@ -1,4 +1,3 @@ -import moment from "moment"; import { DataTypes, HasManyAddAssociationMixin, @@ -211,20 +210,6 @@ export default function setupEventModel(sequelize: Sequelize) { [Op.and]: { // are not drafts, draft: false, - // and either: - [Op.or]: { - // closed less than a week ago - registrationEndDate: { - [Op.gt]: moment().subtract(7, "days").toDate(), - }, - // or happened less than a week ago - date: { - [Op.gt]: moment().subtract(7, "days").toDate(), - }, - endDate: { - [Op.gt]: moment().subtract(7, "days").toDate(), - }, - }, }, }, }), diff --git a/packages/ilmomasiina-backend/src/routes/events/getEventDetails.ts b/packages/ilmomasiina-backend/src/routes/events/getEventDetails.ts index 0c2e77cd..53e9b8a9 100644 --- a/packages/ilmomasiina-backend/src/routes/events/getEventDetails.ts +++ b/packages/ilmomasiina-backend/src/routes/events/getEventDetails.ts @@ -1,5 +1,6 @@ import { FastifyReply, FastifyRequest } from "fastify"; import { NotFound } from "http-errors"; +import moment from "moment"; import { Op } from "sequelize"; import type { @@ -32,7 +33,25 @@ export const basicEventInfoCached = createCache({ async get(eventSlug: EventSlug) { // First query general event information const event = await Event.scope("user").findOne({ - where: { slug: eventSlug }, + where: { + slug: eventSlug, + // are not drafts, + draft: false, + // and either: + [Op.or]: { + // closed less than a week ago + registrationEndDate: { + [Op.gt]: moment().subtract(7, "days").toDate(), + }, + // or happened less than a week ago + date: { + [Op.gt]: moment().subtract(7, "days").toDate(), + }, + endDate: { + [Op.gt]: moment().subtract(7, "days").toDate(), + }, + }, + }, attributes: eventGetEventAttrs, include: [ { diff --git a/packages/ilmomasiina-backend/src/routes/events/getEventsList.ts b/packages/ilmomasiina-backend/src/routes/events/getEventsList.ts index 831b469d..12ee8f4d 100644 --- a/packages/ilmomasiina-backend/src/routes/events/getEventsList.ts +++ b/packages/ilmomasiina-backend/src/routes/events/getEventsList.ts @@ -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 }) { + const { category, since } = options; + const filters: WhereOptions = {}; + if (category) { + filters.category = category; + } + if (since) { + filters.date = { + [Op.gt]: since, + }; + } + 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; } diff --git a/packages/ilmomasiina-models/src/schema/eventList/index.ts b/packages/ilmomasiina-models/src/schema/eventList/index.ts index f4934da6..fa085048 100644 --- a/packages/ilmomasiina-models/src/schema/eventList/index.ts +++ b/packages/ilmomasiina-models/src/schema/eventList/index.ts @@ -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.", + }), + ), }); /** Query parameters applicable to the public event list API. */