Archived
- Send now and Remind answer straight away with who they're going to and queue the feedback-send task; the Feedback page refreshes to show progress. - One-off background tasks (enqueueTask in src/jobs/index.js): pg-boss with retries, or in-process after the response when the queue is off. - Reminders are claimed with the 24-hour rule, so two tasks at once never remind the same person twice. - Tests: tests/tasks.test.js, tests/feedbackPage.test.ts, e2e/feedback.spec.ts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
86 lines
5.6 KiB
TypeScript
86 lines
5.6 KiB
TypeScript
/**
|
||
* The Feedback page (2.1.0): see services/feedback.ts. Supervisors and admins; campus-scoped
|
||
* supervisors see their campuses' events. Setting a form up, "Send now" and the summary report
|
||
* stay in money.ts.
|
||
*/
|
||
import { z } from 'zod';
|
||
import { defineRoute } from '../route';
|
||
import { single } from '../paging';
|
||
import { id, idParams } from '../schemas';
|
||
import * as feedback from '../../services/feedback';
|
||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any -- campus scope and the access log are JavaScript */
|
||
const cs = require('../../utils/campusScope') as {
|
||
scopeEvent(opts?: { need?: 'view' | 'manage' }): any;
|
||
scopeList: any;
|
||
listWhere(req: unknown, path?: string): Promise<object>;
|
||
};
|
||
const { logDataAccess } = require('../../utils/accessLog') as { logDataAccess(req: unknown, entry: { action: string; what?: string; eventId?: string; records?: number }): Promise<void> };
|
||
|
||
const event = z.object({ id, title: z.string(), startDate: z.coerce.date(), endDate: z.coerce.date() });
|
||
const status = z.enum(['sent', 'scheduled', 'not_scheduled']).describe('sent, scheduled (a send time is set) or not_scheduled');
|
||
const when = z.coerce.date().nullable();
|
||
const count = z.object({ label: z.string(), value: z.number().int() });
|
||
|
||
export const overview = defineRoute({
|
||
method: 'get', path: '/feedback', tag: 'Feedback', summary: 'Every event with a feedback form',
|
||
description: 'Newest event first, with how many were asked and answered, and the average star rating.',
|
||
access: 'supervisor', campusScope: 'scopeList (the handler filters by listWhere)', middleware: [cs.scopeList],
|
||
response: single(z.array(z.object({
|
||
event, status, sendAt: when, sentAt: when, anonymous: z.boolean(),
|
||
invited: z.number().int(), answered: z.number().int(), responseRate: z.number().nullable().describe('%'),
|
||
averageRating: z.number().nullable().describe('Every star rating on the form, 1–5'), lastReminderAt: when,
|
||
}))),
|
||
async handler({ req }) { return { data: await feedback.feedbackOverview(await cs.listWhere(req, 'event')) }; },
|
||
});
|
||
|
||
export const results = defineRoute({
|
||
method: 'get', path: '/events/:id/feedback/results', tag: 'Feedback', summary: 'An event\'s feedback: results, responses and who was asked',
|
||
description: 'On an anonymous form, responses have no name and `people` is null. Opening a named form\'s answers is recorded in the access log.',
|
||
access: 'supervisor', campusScope: 'scopeEvent(view)', middleware: [cs.scopeEvent()], params: idParams,
|
||
response: single(z.object({
|
||
event,
|
||
form: z.object({
|
||
status, sendAt: when, sentAt: when, audience: z.enum(['checked_in', 'all']), anonymous: z.boolean(), intro: z.string().nullable(),
|
||
autoReminderAt: when.describe('When the automatic reminder goes (3 days after sending)'), autoReminderSent: z.boolean(),
|
||
}),
|
||
counts: z.object({
|
||
invited: z.number().int(), answered: z.number().int(), notAnswered: z.number().int(), responseRate: z.number().nullable(),
|
||
canRemind: z.number().int().describe('Not answered, and not sent the form or a reminder in the last 24 hours'),
|
||
remindedRecently: z.number().int(),
|
||
waiting: z.number().int().describe('Would get the form but haven\'t been sent it yet (e.g. checked in after it went out)'),
|
||
}),
|
||
averageRating: z.number().nullable(),
|
||
questions: z.array(z.object({
|
||
fieldId: id, type: z.string(), label: z.string(), answered: z.number().int(),
|
||
average: z.number().nullable().optional(), distribution: z.array(count).optional(), counts: z.array(count).optional(),
|
||
texts: z.array(z.object({ value: z.string(), name: z.string().optional() })).optional(),
|
||
})),
|
||
fields: z.array(z.object({ id, type: z.string(), label: z.string() })),
|
||
responses: z.array(z.object({ id, createdAt: z.coerce.date(), name: z.string().nullable(), answers: z.record(z.string(), z.string()).describe('Question id → answer') })),
|
||
people: z.array(z.object({
|
||
userId: id, name: z.string(), email: z.string().nullable(), phoneNumber: z.string().nullable(),
|
||
sentAt: z.coerce.date(), remindedAt: when, reminderCount: z.number().int(), respondedAt: when, canRemind: z.boolean(),
|
||
})).nullable(),
|
||
})),
|
||
async handler({ params, req }) {
|
||
const data = await feedback.eventFeedback(params.id);
|
||
if (!data.form.anonymous && data.responses.length) {
|
||
await logDataAccess(req, { action: 'feedback_responses', what: `Feedback answers: ${data.event.title}`, eventId: data.event.id, records: data.responses.length });
|
||
}
|
||
return { data };
|
||
},
|
||
});
|
||
|
||
export const remind = defineRoute({
|
||
method: 'post', path: '/events/:id/feedback/remind', tag: 'Feedback', summary: 'Remind people who haven\'t answered',
|
||
description: 'A fresh link to everyone who hasn\'t answered, or to `userIds` (named forms only). Nobody is sent the form or a reminder twice within 24 hours. Without `userIds` it also sends the form to anyone who has become eligible since it went out. Answers straight away; the emails go out in the background.',
|
||
access: 'supervisor', campusScope: 'scopeEvent(manage)', middleware: [cs.scopeEvent({ need: 'manage' })], params: idParams,
|
||
body: z.object({ userIds: z.array(id).max(1000).nullish().describe('Only these people (named forms)') }),
|
||
response: single(z.object({ reminded: z.number().int().describe('Being reminded'), newlyInvited: z.number().int().describe('Being sent the form for the first time'), skipped: z.number().int(), queued: z.boolean().describe('Emails are going out in the background'), message: z.string() })),
|
||
async handler({ params, body }) {
|
||
const { title: _title, ...data } = await feedback.remindFeedback(params.id, { userIds: body.userIds });
|
||
return { data };
|
||
},
|
||
});
|