Skip to content

Commit

Permalink
Also optimize admin event details
Browse files Browse the repository at this point in the history
  • Loading branch information
PurkkaKoodari authored and kahlstrm committed Jan 21, 2024
1 parent 870f3a6 commit c0916cd
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions packages/ilmomasiina-backend/src/routes/events/getEventDetails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import { NotFound } from 'http-errors';
import { Op, Order } from 'sequelize';
import { Op } from 'sequelize';

import type {
AdminEventPathParams, AdminEventResponse, EventID, EventSlug, UserEventPathParams, UserEventResponse,
Expand All @@ -20,11 +20,6 @@ import { Quota } from '../../models/quota';
import { Signup } from '../../models/signup';
import { stringifyDates } from '../utils';

const answerOrdering: Order = [
['order', 'ASC'],
[Signup, 'createdAt', 'ASC'],
];

export async function eventDetailsForUser(
eventSlug: EventSlug,
): Promise<UserEventResponse> {
Expand All @@ -36,14 +31,14 @@ export async function eventDetailsForUser(
{
model: Question,
attributes: [...eventGetQuestionAttrs],
order: ['order', 'ASC'],
},
],
order: [[Question, 'order', 'ASC']],
});

if (!event) {
// Event not found with id, probably deleted
throw new NotFound('No event found with id');
throw new NotFound('No event found with slug');
}

// Only return answers to public questions
Expand Down Expand Up @@ -72,7 +67,11 @@ export async function eventDetailsForUser(
],
},
],
order: answerOrdering,
// First sort by Quota order, then by signup creation date
order: [
['order', 'ASC'],
[Signup, 'createdAt', 'ASC'],
],
});

// Dynamic extra fields
Expand Down Expand Up @@ -125,50 +124,52 @@ export async function eventDetailsForAdmin(
where: { id: eventID },
attributes: [...adminEventGetEventAttrs],
include: [
// First include all questions (also non-public for the form)
// Include all questions (also non-public for the form)
{
model: Question,
attributes: [...eventGetQuestionAttrs],
},
// Include quotas..
],
order: [[Question, 'order', 'ASC']],
});

if (event === null) {
// Event not found with id, probably deleted
throw new NotFound('No event found with id');
}

const quotas = await Quota.findAll({
where: { eventId: event.id },
attributes: [...eventGetQuotaAttrs],
// Include all signups for the quotas
include: [
{
model: Quota,
attributes: [...eventGetQuotaAttrs],
// ... and signups of quotas
model: Signup.scope('active'),
attributes: [...eventGetSignupAttrs, 'confirmedAt', 'id', 'email'],
required: false,
// ... and answers of signups
include: [
{
model: Signup.scope('active'),
attributes: [...eventGetSignupAttrs, 'confirmedAt', 'id', 'email'],
model: Answer,
attributes: [...eventGetAnswerAttrs],
required: false,
// ... and answers of signups
include: [
{
model: Answer,
attributes: [...eventGetAnswerAttrs],
required: false,
},
],
},
],
},
],
// First sort by Quota order, then by signup creation date
order: [
[Question, 'order', 'ASC'],
[Quota, 'order', 'ASC'],
[Quota, Signup, 'createdAt', 'ASC'],
['order', 'ASC'],
[Signup, 'createdAt', 'ASC'],
],
});
if (event === null) {
// Event not found with id, probably deleted
throw new NotFound('No event found with id');
}

// Admins get a simple result with many columns
return stringifyDates({
...event.get({ plain: true }),
questions: event.questions!.map((question) => question.get({ plain: true })),
updatedAt: event.updatedAt,
quotas: event.quotas!.map((quota) => ({
quotas: quotas.map((quota) => ({
...quota.get({ plain: true }),
signups: quota.signups!.map((signup) => ({
...signup.get({ plain: true }),
Expand Down

0 comments on commit c0916cd

Please sign in to comment.