Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
"includes": [
"src/features/api/payment-processing.ts",
"src/shared/db/migrations/schema.ts",
"src/shared/db/questions.ts",
"src/ui/templates/public/reservations.tsx",
"test/lib/server-attendees.test.ts",
"test/lib/server-listing-parents.test.ts",
Expand Down
10 changes: 4 additions & 6 deletions src/features/admin/attendee-form-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ import {
setLogisticsAssignments,
} from "#shared/db/logistics.ts";
import { logisticsAgents } from "#shared/db/logistics-agents.ts";
import {
parseQuestionAnswers,
type QuestionWithAnswers,
saveAttendeeAnswers,
} from "#shared/db/questions.ts";
import type { QuestionWithAnswers } from "#shared/db/question-types.ts";
import { saveAttendeeAnswers } from "#shared/db/questions/attendee-answers.ts";
import { parseQuestionAnswers } from "#shared/db/questions/parsing.ts";
import { settings } from "#shared/db/settings.ts";
import { ATTENDEE_DEMO_FIELDS, applyDemoOverrides } from "#shared/demo.ts";
import type { FormParams } from "#shared/form-data.ts";
Expand Down Expand Up @@ -394,7 +392,7 @@ const applyEdit = async (
parsed: ParsedAttendeeForm,
attendee: Attendee,
questions: QuestionWithAnswers[],
answers: import("#shared/db/questions.ts").AttendeeAnswerSet,
answers: import("#shared/db/questions/attendee-answers.ts").AttendeeAnswerSet,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
logisticsPlan: LogisticsPlan,
): Promise<SaveOutcome> => {
// Block marking an assigned built-site line no-quantity (no release path here).
Expand Down
4 changes: 2 additions & 2 deletions src/features/admin/attendee-page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ import {
} from "#shared/db/groups.ts";
import { getChildrenForParents } from "#shared/db/listing-parents.ts";
import { getAllListings } from "#shared/db/listings.ts";
import type { QuestionWithAnswers } from "#shared/db/question-types.ts";
import {
getAttendeeTextAnswers,
loadAttendeeQuestionData,
type QuestionWithAnswers,
} from "#shared/db/questions.ts";
} from "#shared/db/questions/attendee-answers.ts";
import { ErrorCode, logError } from "#shared/logger.ts";
import { requireRequestPrivateKey } from "#shared/session-private-key.ts";
import type { Attendee, ListingWithCount } from "#shared/types.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/features/admin/attendees-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { type Column, CSV } from "#shared/csv/index.ts";
import { toMajorUnits } from "#shared/currency.ts";
import { addDays } from "#shared/dates.ts";
import { isServicing } from "#shared/db/attendees/kind.ts";
import type { AttendeeQuestionData } from "#shared/db/questions.ts";
import type { AttendeeQuestionData } from "#shared/db/questions/attendee-answers.ts";
import { DEFAULT_TIMEZONE, formatDatetimeShortInTz } from "#shared/timezone.ts";
import type { Attendee } from "#shared/types.ts";

Expand Down
4 changes: 2 additions & 2 deletions src/features/admin/attendees-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
updateAttendeePII,
} from "#shared/db/attendees.ts";
import { queryAll, queryOne } from "#shared/db/client.ts";
import { getQuestionsWithListingIds } from "#shared/db/questions.ts";
import { getQuestionsWithListingIds } from "#shared/db/questions/queries.ts";
Comment thread
stefan-burke marked this conversation as resolved.
Resolved
import type { FormParams } from "#shared/form-data.ts";
import {
applyAttendeeMerge,
Expand Down Expand Up @@ -385,7 +385,7 @@ const parseConflictDecisions = <T>(
const value = parse(key);
if (value !== undefined) out[key] = value;
}
return out;
return undefined ;
};

/** Parse booking decisions from form (only non-moveable items) */
Expand Down
2 changes: 1 addition & 1 deletion src/features/admin/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
getLogisticsAssignmentsForAttendees,
} from "#shared/db/logistics.ts";
import { agentNameMap, logisticsAgents } from "#shared/db/logistics-agents.ts";
import { loadAttendeeQuestionData } from "#shared/db/questions.ts";
import { loadAttendeeQuestionData } from "#shared/db/questions/attendee-answers.ts";
import { settings } from "#shared/db/settings.ts";
import {
type AgentFilter,
Expand Down
2 changes: 1 addition & 1 deletion src/features/admin/group-page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { getActiveHolidays } from "#shared/db/holidays.ts";
import { getGroupDayPrices } from "#shared/db/listing-prices.ts";
import { getAttendeesByListingIds } from "#shared/db/listings.ts";
import { loadAttendeeQuestionData } from "#shared/db/questions.ts";
import { loadAttendeeQuestionData } from "#shared/db/questions/attendee-answers.ts";
import { settings } from "#shared/db/settings.ts";
import { requireRequestPrivateKey } from "#shared/session-private-key.ts";
import { sortListings } from "#shared/sort-listings.ts";
Expand Down
4 changes: 2 additions & 2 deletions src/features/admin/listing-page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ import {
listingRevenueBreakdown,
} from "#shared/db/listings.ts";
import { deleteAllStaleReservations } from "#shared/db/processed-payments.ts";
import { getListingChoiceAnswerMap } from "#shared/db/questions/attendee-answers.ts";
import {
getAllQuestionsWithAnswers,
getListingChoiceAnswerMap,
getListingQuestionIds,
getQuestionsForListing,
} from "#shared/db/questions.ts";
} from "#shared/db/questions/queries.ts";
import { settings } from "#shared/db/settings.ts";
import {
loadNotesForAttendees,
Expand Down
4 changes: 2 additions & 2 deletions src/features/admin/listings-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { getGroupIdsByListingId, groups } from "#shared/db/groups.ts";
import {
type AttendeeQuestionData,
getAttendeeAnswersBatch,
getQuestionsForListing,
} from "#shared/db/questions.ts";
} from "#shared/db/questions/attendee-answers.ts";
import { getQuestionsForListing } from "#shared/db/questions/queries.ts";
import { requireRequestPrivateKey } from "#shared/session-private-key.ts";
import type { Attendee, ListingWithCount } from "#shared/types.ts";
import type { GroupContext } from "#templates/admin/listings/types.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/features/admin/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
setModifierAnswers,
updateModifierAggregateValues,
} from "#shared/db/modifiers.ts";
import { getAllQuestionsWithAnswers } from "#shared/db/questions.ts";
import { getAllQuestionsWithAnswers } from "#shared/db/questions/queries.ts";
import { getFlash } from "#shared/flash-context.ts";
import type { FormParams } from "#shared/form-data.ts";
import {
Expand Down
40 changes: 22 additions & 18 deletions src/features/admin/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,40 @@ import { logActivity } from "#shared/db/activityLog.ts";
import { getAllListings, getListingWithCount } from "#shared/db/listings.ts";
import { getAllModifiers } from "#shared/db/modifiers.ts";
import {
ANSWER_AGGREGATE_FIELDS,
type Answer,
type AnswerAggregateValues,
answersTable,
assignNextQuestionSortOrder,
deleteAnswer,
deleteQuestion,
findAnswerById,
getAllQuestionListingIds,
getAllQuestionsWithAnswers,
getAnswerAggregateRecalculation,
getAnswerModifierId,
getAnswerSelectionTotals,
getNextAnswerSortOrder,
getQuestionListingIds,
getQuestionWithAnswers,
isQuestionDisplayType,
QUESTION_DISPLAY_TYPES,
type QuestionWithAnswers,
questionDisplayTypeError,
questionsTable,
requireQuestionDisplayType,
} from "#shared/db/question-types.ts";
import {
ANSWER_AGGREGATE_FIELDS,
type AnswerAggregateValues,
getAnswerAggregateRecalculation,
getAnswerModifierId,
getAnswerSelectionTotals,
resetAnswerAggregateFields,
setAnswerModifier,
updateAnswerAggregateValues,
} from "#shared/db/questions/aggregates.ts";
import { deleteAnswer, deleteQuestion } from "#shared/db/questions/delete.ts";
import { findAnswerById } from "#shared/db/questions/parsing.ts";
import {
getAllQuestionListingIds,
getAllQuestionsWithAnswers,
getQuestionListingIds,
getQuestionWithAnswers,
setListingQuestions,
setQuestionListings,
} from "#shared/db/questions/queries.ts";
import {
assignNextQuestionSortOrder,
getNextAnswerSortOrder,
swapAnswerOrder,
swapQuestionOrder,
updateAnswerAggregateValues,
} from "#shared/db/questions.ts";
} from "#shared/db/questions/sort-order.ts";
import { answersTable, questionsTable } from "#shared/db/questions/tables.ts";
import { getFlash } from "#shared/flash-context.ts";
import { defineForm } from "#shared/forms.tsx";
import { MAX_TEXTAREA_LENGTH } from "#shared/limits.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/features/api/payment-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ import {
import {
groupListingAnswers,
saveAttendeeAnswers,
} from "#shared/db/questions.ts";
} from "#shared/db/questions/attendee-answers.ts";
import { createSystemNote } from "#shared/db/system-notes.ts";
import {
ErrorCode,
Expand Down
10 changes: 6 additions & 4 deletions src/features/public/ticket-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import type { TicketListing } from "#shared/booking/model.ts";
import { quantityFieldName } from "#shared/booking/tree.ts";
import type { AddOnOption } from "#shared/db/modifier-resolve.ts";
import type {
AttendeeAnswerSet,
AttendeeListingEntry,
QuestionListingMap,
QuestionWithAnswers,
TextAnswer,
} from "#shared/db/questions.ts";
} from "#shared/db/question-types.ts";
import type {
AttendeeAnswerSet,
AttendeeListingEntry,
} from "#shared/db/questions/attendee-answers.ts";
import type { QuestionListingMap } from "#shared/db/questions/queries.ts";
import type { FormParams } from "#shared/form-data.ts";
import { mergeListingFields } from "#shared/listing-fields.ts";
import type { ListingFields } from "#shared/types.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/features/public/ticket-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import {
hasPromoCodeModifiers,
} from "#shared/db/modifier-resolve.ts";
import type { ModifierUsage } from "#shared/db/modifier-usage.ts";
import { getQuestionsWithListingIds } from "#shared/db/questions.ts";
import { getQuestionsWithListingIds } from "#shared/db/questions/queries.ts";
import { settings } from "#shared/db/settings.ts";
import type { EmailEntry } from "#shared/email.ts";
import type { FormParams } from "#shared/form-data.ts";
Expand Down
4 changes: 2 additions & 2 deletions src/features/public/ticket-submit/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
packageQuantityFieldName,
quantityFieldName,
} from "#shared/booking/tree.ts";
import { getOrCreateStringIds } from "#shared/db/questions.ts";
import { getOrCreateStringIds } from "#shared/db/questions/strings.ts";
import type { FormParams } from "#shared/form-data.ts";
import type { CheckoutIntent } from "#shared/payments.ts";
import { verifyQrBookToken } from "#shared/qr-token.ts";
Expand Down Expand Up @@ -135,7 +135,7 @@ export const applyQrTokenOverride = async (
export type AnswerInfo = {
activeQuestions: TicketCtx["questions"];
answerIds: number[];
textAnswers: import("#shared/db/questions.ts").TextAnswer[];
textAnswers: import("#shared/db/question-types.ts").TextAnswer[];
selectedListingIds: Set<number>;
};

Expand Down
2 changes: 1 addition & 1 deletion src/features/public/ticket-submit/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
import { hmacHash } from "#shared/crypto/hashing.ts";
import { getPublicDefaultStatus } from "#shared/db/attendee-statuses.ts";
import type { ChildAllocation } from "#shared/db/attendee-types.ts";
import { saveAttendeeAnswers } from "#shared/db/questions.ts";
import { saveAttendeeAnswers } from "#shared/db/questions/attendee-answers.ts";
import type { CheckoutIntent, CheckoutItem } from "#shared/payments.ts";
import { logAndNotifyRegistration } from "#shared/webhook.ts";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/features/public/ticket-submit/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { buildOrderLines } from "#shared/booking/order-lines.ts";
import type { PricedOrder } from "#shared/checkout-pricing.ts";
import type { ChildAllocation } from "#shared/db/attendee-types.ts";
import { answerModifierQuantities } from "#shared/db/modifier-resolve.ts";
import { parseQuestionAnswers } from "#shared/db/questions.ts";
import { parseQuestionAnswers } from "#shared/db/questions/parsing.ts";
import type { FormParams } from "#shared/form-data.ts";
import { concealLineNames } from "#shared/package-privacy.ts";
import { validateSiteAssignmentConfig } from "#shared/site-assignment.ts";
Expand Down
6 changes: 2 additions & 4 deletions src/features/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import type {
} from "#shared/booking/model.ts";
import type { PagePackage } from "#shared/booking/page-packages.ts";
import type { AddOnOption } from "#shared/db/modifier-resolve.ts";
import type {
QuestionListingMap,
QuestionWithAnswers,
} from "#shared/db/questions.ts";
import type { QuestionWithAnswers } from "#shared/db/question-types.ts";
import type { QuestionListingMap } from "#shared/db/questions/queries.ts";
import type {
Image,
ImageUseItemType,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/db/attendees/servicing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
type AttendeeAnswersBatch,
getAttendeeAnswersBatch,
saveAttendeeAnswers,
} from "#shared/db/questions.ts";
} from "#shared/db/questions/attendee-answers.ts";
import { settings } from "#shared/db/settings.ts";
import type { TransferInput } from "#shared/ledger/types.ts";
import { nowIso } from "#shared/now.ts";
Expand Down
75 changes: 75 additions & 0 deletions src/shared/db/question-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Shared types and validation for the custom questions tables.
*
* Questions and answers are encrypted at rest using symmetric encryption
* (DB_ENCRYPTION_KEY). Listing-question and attendee-answer mappings use
* integer foreign keys.
*/

import * as v from "valibot";
import { guardFor } from "#shared/validation/guard.ts";

/** A custom multiple-choice question */
export const QUESTION_DISPLAY_TYPES = ["radio", "select", "free_text"] as const;
export const QuestionDisplayTypeSchema = v.picklist(QUESTION_DISPLAY_TYPES);
export type QuestionDisplayType = v.InferOutput<
typeof QuestionDisplayTypeSchema
>;

export const isQuestionDisplayType = guardFor(QuestionDisplayTypeSchema);

export const questionDisplayTypeError =
"Display as must be radio buttons, a select box, or free text";

export const requireQuestionDisplayType = (
value: string,
): QuestionDisplayType => {
if (isQuestionDisplayType(value)) return value;
throw new Error(questionDisplayTypeError);
};

export interface Question {
assign_all: boolean;
display_type: QuestionDisplayType;
id: number;
text: string; // encrypted
}

/** An answer option for a question */
export interface Answer {
id: number;
question_id: number;
sort_order: number;
text: string; // encrypted
/** Deactivated answers are hidden on the public booking form; the admin edit
* form still shows one an attendee already selected so it isn't silently
* dropped on the next save. */
active: boolean;
}

/** Link between listing and question. Membership only — display order comes
* from the question's own `sort_order`, not from this row. The `sort_order`
* column is retained but unused (legacy per-listing ordering). */
export interface ListingQuestion {
listing_id: number;
id: number;
question_id: number;
sort_order: number;
}

/** Question with its answer options (decrypted) */
export type QuestionWithAnswers = Omit<Question, "assign_all"> & {
answers: Answer[];
assign_all?: boolean;
};

/** A free-text answer submitted for a question (plaintext, pre-string-interning). */
export type TextAnswer = { questionId: number; text: string };

/** A free-text answer resolved to its interned string id. */
export type TextAnswerId = { questionId: number; stringId: number };

/** Outcome of parsing a form's submitted answers. */
export type ParsedQuestionAnswers =
| { ok: true; answerIds: number[]; textAnswers: TextAnswer[] }
| { ok: false; error: string };
Loading