Skip to content

Commit

Permalink
feat: Allow user scope to view old events
Browse files Browse the repository at this point in the history
  • Loading branch information
joonatanaatos committed Oct 24, 2024
1 parent cf0a1fd commit 7202a9f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
14 changes: 0 additions & 14 deletions packages/ilmomasiina-backend/src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,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(),
},
},
},
},
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { NotFound } from "http-errors";
import moment from "moment";
import { Op } from "sequelize";

import type {
Expand Down Expand Up @@ -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: [
{
Expand Down
17 changes: 14 additions & 3 deletions packages/ilmomasiina-backend/src/routes/events/getEventsList.ts
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";
Expand All @@ -24,8 +24,19 @@ function eventOrder(): Order {
export const eventsListForUserCached = createCache({
maxAgeMs: 1000,
maxPendingAgeMs: 2000,
async get(category?: string) {
const where = category ? { category } : {};
async get(category?: string, since?: Date) {
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,
Expand Down

0 comments on commit 7202a9f

Please sign in to comment.