mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
feat(contracts): todo, settings, collab and file routes declare their response schemas
shared gains the response schemas these routes answer with (rows in their storage spelling, as the routes have always sent them) plus the common success and empty answers, all exported for the client to type its calls with. Every JSON handler in the four controllers carries its @ResponseContract; the file download, which streams, is marked exempt. The files e2e fixture table gains the note_id column the real one has.
This commit is contained in:
@@ -81,3 +81,144 @@ export const collabReactionRequestSchema = z.object({
|
||||
emoji: z.string().min(1),
|
||||
});
|
||||
export type CollabReactionRequest = z.infer<typeof collabReactionRequestSchema>;
|
||||
|
||||
// ── Responses ───────────────────────────────────────────────────────────────
|
||||
// Rows keep SQLite's storage spelling (0/1 flags, TEXT times), as the routes
|
||||
// have always answered; the derived booleans the service adds are booleans.
|
||||
|
||||
/** A file attached to a note or a message, with the download route the client links to. */
|
||||
export const collabAttachmentSchema = z.object({
|
||||
id: z.number(),
|
||||
filename: z.string(),
|
||||
original_name: z.string(),
|
||||
file_size: z.number().nullable(),
|
||||
mime_type: z.string().nullable(),
|
||||
url: z.string(),
|
||||
});
|
||||
export type CollabAttachment = z.infer<typeof collabAttachmentSchema>;
|
||||
|
||||
/** A collab note with its author and attachments. */
|
||||
export const collabNoteSchema = z.object({
|
||||
id: z.number(),
|
||||
trip_id: z.number(),
|
||||
user_id: z.number(),
|
||||
category: z.string().nullable(),
|
||||
title: z.string(),
|
||||
content: z.string().nullable(),
|
||||
color: z.string().nullable(),
|
||||
pinned: z.number().nullable(),
|
||||
created_at: z.string().nullable(),
|
||||
updated_at: z.string().nullable(),
|
||||
website: z.string().nullable(),
|
||||
username: z.string(),
|
||||
avatar: z.string().nullable(),
|
||||
avatar_url: z.string().nullable(),
|
||||
attachments: z.array(collabAttachmentSchema),
|
||||
});
|
||||
export type CollabNote = z.infer<typeof collabNoteSchema>;
|
||||
|
||||
export const collabNotesResponseSchema = z.object({ notes: z.array(collabNoteSchema) });
|
||||
export type CollabNotesResponse = z.infer<typeof collabNotesResponseSchema>;
|
||||
export const collabNoteResponseSchema = z.object({ note: collabNoteSchema });
|
||||
export type CollabNoteResponse = z.infer<typeof collabNoteResponseSchema>;
|
||||
/** POST notes/:id/files */
|
||||
export const collabNoteFileResponseSchema = z.object({ file: collabAttachmentSchema });
|
||||
export type CollabNoteFileResponse = z.infer<typeof collabNoteFileResponseSchema>;
|
||||
|
||||
/** A shared link with the name of whoever added it. */
|
||||
export const collabLinkSchema = z.object({
|
||||
id: z.number(),
|
||||
trip_id: z.number(),
|
||||
user_id: z.number(),
|
||||
title: z.string(),
|
||||
url: z.string(),
|
||||
pinned: z.number().nullable(),
|
||||
created_at: z.string().nullable(),
|
||||
updated_at: z.string().nullable(),
|
||||
username: z.string(),
|
||||
});
|
||||
export type CollabLink = z.infer<typeof collabLinkSchema>;
|
||||
|
||||
export const collabLinksResponseSchema = z.object({ links: z.array(collabLinkSchema) });
|
||||
export type CollabLinksResponse = z.infer<typeof collabLinksResponseSchema>;
|
||||
export const collabLinkResponseSchema = z.object({ link: collabLinkSchema });
|
||||
export type CollabLinkResponse = z.infer<typeof collabLinkResponseSchema>;
|
||||
|
||||
/** Someone who voted for a poll option. */
|
||||
export const collabPollVoterSchema = z.object({
|
||||
id: z.number(),
|
||||
user_id: z.number(),
|
||||
username: z.string(),
|
||||
avatar: z.string().nullable(),
|
||||
avatar_url: z.string().nullable(),
|
||||
});
|
||||
|
||||
/** A poll with its options and their voters. `options` is replaced by the formatted list; the stored JSON never leaves. */
|
||||
export const collabPollSchema = z.object({
|
||||
id: z.number(),
|
||||
trip_id: z.number(),
|
||||
user_id: z.number(),
|
||||
question: z.string(),
|
||||
options: z.array(z.object({ text: z.unknown(), label: z.unknown(), voters: z.array(collabPollVoterSchema) })),
|
||||
multiple: z.number().nullable(),
|
||||
closed: z.number().nullable(),
|
||||
deadline: z.string().nullable(),
|
||||
created_at: z.string().nullable(),
|
||||
username: z.string(),
|
||||
avatar: z.string().nullable(),
|
||||
avatar_url: z.string().nullable(),
|
||||
is_closed: z.boolean(),
|
||||
multiple_choice: z.boolean(),
|
||||
});
|
||||
export type CollabPoll = z.infer<typeof collabPollSchema>;
|
||||
|
||||
export const collabPollsResponseSchema = z.object({ polls: z.array(collabPollSchema) });
|
||||
export type CollabPollsResponse = z.infer<typeof collabPollsResponseSchema>;
|
||||
export const collabPollResponseSchema = z.object({ poll: collabPollSchema });
|
||||
export type CollabPollResponse = z.infer<typeof collabPollResponseSchema>;
|
||||
|
||||
/** One emoji on a message, with who reacted. */
|
||||
export const collabReactionSchema = z.object({
|
||||
emoji: z.string(),
|
||||
users: z.array(z.object({ user_id: z.number(), username: z.string() })),
|
||||
count: z.number(),
|
||||
});
|
||||
export type CollabReaction = z.infer<typeof collabReactionSchema>;
|
||||
|
||||
/** A chat message with its author, the message it replies to, reactions and attachments. A deleted one keeps its row with empty text. */
|
||||
export const collabMessageSchema = z.object({
|
||||
id: z.number(),
|
||||
trip_id: z.number(),
|
||||
user_id: z.number(),
|
||||
text: z.string(),
|
||||
reply_to: z.number().nullable(),
|
||||
created_at: z.string().nullable(),
|
||||
deleted: z.number().nullable(),
|
||||
username: z.string(),
|
||||
avatar: z.string().nullable(),
|
||||
reply_text: z.string().nullable(),
|
||||
reply_username: z.string().nullable(),
|
||||
user_avatar: z.string().nullable(),
|
||||
avatar_url: z.string().nullable(),
|
||||
reactions: z.array(collabReactionSchema),
|
||||
attachments: z.array(collabAttachmentSchema),
|
||||
});
|
||||
export type CollabMessage = z.infer<typeof collabMessageSchema>;
|
||||
|
||||
export const collabMessagesResponseSchema = z.object({ messages: z.array(collabMessageSchema) });
|
||||
export type CollabMessagesResponse = z.infer<typeof collabMessagesResponseSchema>;
|
||||
export const collabMessageResponseSchema = z.object({ message: collabMessageSchema });
|
||||
export type CollabMessageResponse = z.infer<typeof collabMessageResponseSchema>;
|
||||
/** POST messages/:id/react: the message's reactions after the toggle. */
|
||||
export const collabReactionsResponseSchema = z.object({ reactions: z.array(collabReactionSchema) });
|
||||
export type CollabReactionsResponse = z.infer<typeof collabReactionsResponseSchema>;
|
||||
|
||||
/** GET link-preview: what the page's Open Graph tags (or its title) say; nulls when nothing could be read. */
|
||||
export const collabLinkPreviewResponseSchema = z.object({
|
||||
title: z.string().nullable(),
|
||||
description: z.string().nullable(),
|
||||
image: z.string().nullable(),
|
||||
site_name: z.string().nullable().optional(),
|
||||
url: z.string(),
|
||||
});
|
||||
export type CollabLinkPreviewResponse = z.infer<typeof collabLinkPreviewResponseSchema>;
|
||||
|
||||
@@ -20,3 +20,10 @@ export const nonEmptyString = z.string().trim().min(1);
|
||||
|
||||
/** ISO-8601 timestamp string (the shape TREK serialises dates as in JSON). */
|
||||
export const isoDateTime = z.string().datetime({ offset: true });
|
||||
|
||||
/** The `{ success: true }` body a write answers with when it has nothing else to say. */
|
||||
export const successResponseSchema = z.object({ success: z.literal(true) });
|
||||
export type SuccessResponse = z.infer<typeof successResponseSchema>;
|
||||
|
||||
/** A 204 answer: the handler returns nothing, so no body goes out. */
|
||||
export const emptyResponseSchema = z.undefined();
|
||||
|
||||
@@ -45,3 +45,71 @@ export type FileLinkRequest = z.infer<typeof fileLinkRequestSchema>;
|
||||
/** Variants the photo streaming endpoints accept. */
|
||||
export const photoVariantSchema = z.enum(['thumbnail', 'original']);
|
||||
export type PhotoVariant = z.infer<typeof photoVariantSchema>;
|
||||
|
||||
// ── Responses ───────────────────────────────────────────────────────────────
|
||||
|
||||
/** A trip_files row as the file routes return it: the stored columns, the joins, and the download URL. */
|
||||
export const tripFileSchema = z.object({
|
||||
id: z.number(),
|
||||
trip_id: z.number(),
|
||||
place_id: z.number().nullable(),
|
||||
reservation_id: z.number().nullable(),
|
||||
filename: z.string(),
|
||||
original_name: z.string(),
|
||||
file_size: z.number().nullable(),
|
||||
mime_type: z.string().nullable(),
|
||||
description: z.string().nullable(),
|
||||
created_at: z.string().nullable(),
|
||||
note_id: z.number().nullable(),
|
||||
uploaded_by: z.number().nullable(),
|
||||
starred: z.number().nullable(),
|
||||
deleted_at: z.string().nullable(),
|
||||
message_id: z.number().nullable(),
|
||||
reservation_title: z.string().nullable(),
|
||||
uploaded_by_name: z.string().nullable(),
|
||||
uploaded_by_avatar: z.string().nullable(),
|
||||
url: z.string(),
|
||||
});
|
||||
export type TripFile = z.infer<typeof tripFileSchema>;
|
||||
|
||||
/** A file in the list, with the ids of what it is linked to. */
|
||||
export const tripFileListItemSchema = tripFileSchema.extend({
|
||||
linked_reservation_ids: z.array(z.number()),
|
||||
linked_place_ids: z.array(z.number()),
|
||||
linked_budget_item_ids: z.array(z.number()),
|
||||
});
|
||||
export type TripFileListItem = z.infer<typeof tripFileListItemSchema>;
|
||||
|
||||
/** GET /files */
|
||||
export const tripFilesResponseSchema = z.object({ files: z.array(tripFileListItemSchema) });
|
||||
export type TripFilesResponse = z.infer<typeof tripFilesResponseSchema>;
|
||||
|
||||
/** POST /files, PUT /files/:id, PATCH :id/star, POST :id/restore */
|
||||
export const tripFileResponseSchema = z.object({ file: tripFileSchema });
|
||||
export type TripFileResponse = z.infer<typeof tripFileResponseSchema>;
|
||||
|
||||
/** A file_links row. */
|
||||
export const fileLinkSchema = z.object({
|
||||
id: z.number(),
|
||||
file_id: z.number(),
|
||||
reservation_id: z.number().nullable(),
|
||||
assignment_id: z.number().nullable(),
|
||||
place_id: z.number().nullable(),
|
||||
created_at: z.string().nullable(),
|
||||
budget_item_id: z.number().nullable(),
|
||||
});
|
||||
export type FileLink = z.infer<typeof fileLinkSchema>;
|
||||
|
||||
/** POST :id/link: the file's links after the write. */
|
||||
export const fileLinkCreateResponseSchema = z.object({ success: z.literal(true), links: z.array(fileLinkSchema) });
|
||||
export type FileLinkCreateResponse = z.infer<typeof fileLinkCreateResponseSchema>;
|
||||
|
||||
/** GET :id/links: the file's links with the linked reservation's title. */
|
||||
export const fileLinksResponseSchema = z.object({
|
||||
links: z.array(fileLinkSchema.extend({ reservation_title: z.string().nullable() })),
|
||||
});
|
||||
export type FileLinksResponse = z.infer<typeof fileLinksResponseSchema>;
|
||||
|
||||
/** DELETE trash/empty: how many files were removed for good. */
|
||||
export const fileTrashEmptyResponseSchema = z.object({ success: z.literal(true), deleted: z.number() });
|
||||
export type FileTrashEmptyResponse = z.infer<typeof fileTrashEmptyResponseSchema>;
|
||||
|
||||
@@ -34,3 +34,35 @@ export const weekStartSchema = z.enum(WEEK_START_VALUES);
|
||||
export const DEFAULT_WEEK_START: WeekStart = 'monday';
|
||||
/** Date#getDay() of each value, the number a calendar grid computes with. */
|
||||
export const WEEK_START_DAY: Record<WeekStart, number> = { monday: 1, sunday: 0, saturday: 6 };
|
||||
|
||||
// ── Responses ───────────────────────────────────────────────────────────────
|
||||
|
||||
/** GET /api/settings: the user's preferences by key, each value as getUserSettings parsed it (secrets masked). */
|
||||
export const settingsListResponseSchema = z.object({ settings: z.record(z.string(), z.unknown()) });
|
||||
export type SettingsListResponse = z.infer<typeof settingsListResponseSchema>;
|
||||
|
||||
/**
|
||||
* PUT /api/settings: the key echoed back with what happened to it. `value` when it was
|
||||
* written, `unchanged` when the masked secret came back as it was, `managed` when a
|
||||
* managed install keeps the key.
|
||||
*/
|
||||
export const settingUpsertResponseSchema = z.object({
|
||||
success: z.literal(true),
|
||||
key: z.string(),
|
||||
value: z.unknown().optional(),
|
||||
unchanged: z.literal(true).optional(),
|
||||
managed: z.literal(true).optional(),
|
||||
});
|
||||
export type SettingUpsertResponse = z.infer<typeof settingUpsertResponseSchema>;
|
||||
|
||||
/** POST /api/settings/bulk: how many keys were written, and the managed keys left alone (only when there were any). */
|
||||
export const settingsBulkResponseSchema = z.object({
|
||||
success: z.literal(true),
|
||||
updated: z.number(),
|
||||
managed_keys: z.array(z.string()).optional(),
|
||||
});
|
||||
export type SettingsBulkResponse = z.infer<typeof settingsBulkResponseSchema>;
|
||||
|
||||
/** GET/PUT /api/admin/default-user-settings: the stored defaults a new account starts with, by key. */
|
||||
export const adminDefaultUserSettingsResponseSchema = z.record(z.string(), z.unknown());
|
||||
export type AdminDefaultUserSettingsResponse = z.infer<typeof adminDefaultUserSettingsResponseSchema>;
|
||||
|
||||
@@ -45,3 +45,48 @@ export const todoCategoryAssigneesRequestSchema = z.object({
|
||||
user_ids: z.array(z.number()),
|
||||
});
|
||||
export type TodoCategoryAssigneesRequest = z.infer<typeof todoCategoryAssigneesRequestSchema>;
|
||||
|
||||
// ── Responses ───────────────────────────────────────────────────────────────
|
||||
|
||||
/** A todo_items row as the routes return it (`SELECT *`); flags and times stay as SQLite stores them. */
|
||||
export const todoItemSchema = z.object({
|
||||
id: z.number(),
|
||||
trip_id: z.number(),
|
||||
name: z.string(),
|
||||
checked: z.number().nullable(),
|
||||
category: z.string().nullable(),
|
||||
sort_order: z.number().nullable(),
|
||||
due_date: z.string().nullable(),
|
||||
description: z.string().nullable(),
|
||||
assigned_user_id: z.number().nullable(),
|
||||
priority: z.number().nullable(),
|
||||
created_at: z.string().nullable(),
|
||||
reminded_at: z.string().nullable(),
|
||||
});
|
||||
export type TodoItem = z.infer<typeof todoItemSchema>;
|
||||
|
||||
/** GET /todo */
|
||||
export const todoListResponseSchema = z.object({ items: z.array(todoItemSchema) });
|
||||
export type TodoListResponse = z.infer<typeof todoListResponseSchema>;
|
||||
|
||||
/** POST /todo, PUT /todo/:id */
|
||||
export const todoItemResponseSchema = z.object({ item: todoItemSchema });
|
||||
export type TodoItemResponse = z.infer<typeof todoItemResponseSchema>;
|
||||
|
||||
/** A person assigned to a todo category. */
|
||||
export const todoCategoryAssigneeSchema = z.object({
|
||||
user_id: z.number(),
|
||||
username: z.string(),
|
||||
avatar: z.string().nullable(),
|
||||
});
|
||||
export type TodoCategoryAssignee = z.infer<typeof todoCategoryAssigneeSchema>;
|
||||
|
||||
/** GET /todo/category-assignees: the assignees keyed by category name. */
|
||||
export const todoCategoryAssigneesResponseSchema = z.object({
|
||||
assignees: z.record(z.string(), z.array(todoCategoryAssigneeSchema)),
|
||||
});
|
||||
export type TodoCategoryAssigneesResponse = z.infer<typeof todoCategoryAssigneesResponseSchema>;
|
||||
|
||||
/** PUT /todo/category-assignees/:categoryName: the category's assignees after the write. */
|
||||
export const todoCategoryAssigneesUpdateResponseSchema = z.object({ assignees: z.array(todoCategoryAssigneeSchema) });
|
||||
export type TodoCategoryAssigneesUpdateResponse = z.infer<typeof todoCategoryAssigneesUpdateResponseSchema>;
|
||||
|
||||
Reference in New Issue
Block a user