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:
Maurice
2026-10-10 15:54:15 +02:00
parent 87071bf738
commit 8e01dbb92f
12 changed files with 378 additions and 7 deletions
@@ -37,6 +37,21 @@ import { CurrentUser } from '../auth-core/current-user.decorator';
import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard';
import { BLOCKED_EXTENSIONS, MAX_FILE_SIZE } from '../files/files.constants';
import { SpoolCleanupInterceptor } from '../common/spool-cleanup.interceptor';
import { ResponseContract } from '../common/response-contract';
import {
collabLinkPreviewResponseSchema,
collabLinkResponseSchema,
collabLinksResponseSchema,
collabMessageResponseSchema,
collabMessagesResponseSchema,
collabNoteFileResponseSchema,
collabNoteResponseSchema,
collabNotesResponseSchema,
collabPollResponseSchema,
collabPollsResponseSchema,
collabReactionsResponseSchema,
successResponseSchema,
} from '@trek/shared';
// Note attachments follow the same limit as the file manager (#1364).
export const MAX_NOTE_FILE_SIZE = MAX_FILE_SIZE;
@@ -128,6 +143,7 @@ export class CollabController {
// ── Notes ───────────────────────────────────────────────────────────────
@UseGuards(TripAccessGuard)
@Get('notes')
@ResponseContract(collabNotesResponseSchema)
async listNotes(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return { notes: await this.collab.listNotes(tripId) };
}
@@ -135,6 +151,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Post('notes')
@ResponseContract(collabNoteResponseSchema)
async createNote(@CurrentUser() user: User, @Param('tripId') tripId: string, @Body() body: CollabNoteCreateDto, @Headers('x-socket-id') socketId?: string) {
const note = await this.collab.createNote(tripId, user.id, {
title: body.title,
@@ -151,6 +168,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Put('notes/:id')
@ResponseContract(collabNoteResponseSchema)
async updateNote(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Body() body: CollabNoteUpdateDto, @Headers('x-socket-id') socketId?: string) {
const note = await this.collab.updateNote(tripId, id, {
title: body.title,
@@ -170,6 +188,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Delete('notes/:id')
@ResponseContract(successResponseSchema)
async deleteNote(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
if (!(await this.collab.deleteNote(tripId, id))) {
throw new HttpException({ error: 'Note not found' }, 404);
@@ -180,6 +199,7 @@ export class CollabController {
@Post('notes/:id/files')
@UseInterceptors(FileInterceptor('file'))
@ResponseContract(collabNoteFileResponseSchema)
async addNoteFile(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @UploadedFile() file: Express.Multer.File | undefined, @Headers('x-socket-id') socketId?: string) {
// multer has already written the upload to the spool dir by the time any of
// these checks run, and nothing sweeps orphans, so every refusal takes the
@@ -215,6 +235,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Delete('notes/:id/files/:fileId')
@ResponseContract(successResponseSchema)
async deleteNoteFile(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Param('fileId') fileId: string, @Headers('x-socket-id') socketId?: string) {
if (!(await this.collab.deleteNoteFile(tripId, id, fileId))) {
throw new HttpException({ error: 'File not found' }, 404);
@@ -226,6 +247,7 @@ export class CollabController {
// ── Shared links ────────────────────────────────────────────────────────
@UseGuards(TripAccessGuard)
@Get('links')
@ResponseContract(collabLinksResponseSchema)
async listLinks(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return { links: await this.collab.listLinks(tripId) };
}
@@ -233,6 +255,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Post('links')
@ResponseContract(collabLinkResponseSchema)
async createLink(@CurrentUser() user: User, @Param('tripId') tripId: string, @Body() body: CollabLinkCreateDto, @Headers('x-socket-id') socketId?: string) {
const link = await this.collab.createLink(tripId, user.id, { title: body.title, url: body.url, pinned: Boolean(body.pinned) });
this.collab.broadcast(tripId, 'collab:link:created', { link }, socketId);
@@ -242,6 +265,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Put('links/:id')
@ResponseContract(collabLinkResponseSchema)
async updateLink(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Body() body: CollabLinkUpdateDto, @Headers('x-socket-id') socketId?: string) {
const link = await this.collab.updateLink(tripId, id, {
title: body.title,
@@ -256,6 +280,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Delete('links/:id')
@ResponseContract(successResponseSchema)
async deleteLink(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
if (!(await this.collab.deleteLink(tripId, id))) throw new HttpException({ error: 'Link not found' }, 404);
this.collab.broadcast(tripId, 'collab:link:deleted', { linkId: Number(id) }, socketId);
@@ -265,6 +290,7 @@ export class CollabController {
// ── Polls ───────────────────────────────────────────────────────────────
@UseGuards(TripAccessGuard)
@Get('polls')
@ResponseContract(collabPollsResponseSchema)
async listPolls(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return { polls: await this.collab.listPolls(tripId) };
}
@@ -272,6 +298,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Post('polls')
@ResponseContract(collabPollResponseSchema)
async createPoll(@CurrentUser() user: User, @Param('tripId') tripId: string, @Body() body: CollabPollCreateDto, @Headers('x-socket-id') socketId?: string) {
const poll = await this.collab.createPoll(tripId, user.id, {
question: body.question,
@@ -288,6 +315,7 @@ export class CollabController {
@RequirePermission('collab_edit')
@Post('polls/:id/vote')
@HttpCode(200)
@ResponseContract(collabPollResponseSchema)
async votePoll(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Body() body: CollabPollVoteDto, @Headers('x-socket-id') socketId?: string) {
const result = await this.collab.votePoll(tripId, id, user.id, body.option_index);
if (result.error === 'not_found') throw new HttpException({ error: 'Poll not found' }, 404);
@@ -300,6 +328,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Put('polls/:id/close')
@ResponseContract(collabPollResponseSchema)
async closePoll(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
const poll = await this.collab.closePoll(tripId, id);
if (!poll) {
@@ -312,6 +341,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Delete('polls/:id')
@ResponseContract(successResponseSchema)
async deletePoll(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
if (!(await this.collab.deletePoll(tripId, id))) {
throw new HttpException({ error: 'Poll not found' }, 404);
@@ -323,6 +353,7 @@ export class CollabController {
// ── Messages ────────────────────────────────────────────────────────────
@UseGuards(TripAccessGuard)
@Get('messages')
@ResponseContract(collabMessagesResponseSchema)
async listMessages(@CurrentUser() user: User, @Param('tripId') tripId: string, @Query('before') before?: string) {
return { messages: await this.collab.listMessages(tripId, before) };
}
@@ -340,6 +371,7 @@ export class CollabController {
FilesInterceptor('images', MAX_CHAT_IMAGES, { fileFilter: collabChatImageFilter, limits: { files: MAX_CHAT_IMAGES, fileSize: 10 * 1024 * 1024 } }),
SpoolCleanupInterceptor,
)
@ResponseContract(collabMessageResponseSchema)
async createMessage(@CurrentUser() user: User, @Param('tripId') tripId: string, @Body() body: CollabMessageCreateDto, @UploadedFiles() files: Express.Multer.File[] | undefined, @Headers('x-socket-id') socketId?: string) {
const uploaded = files || [];
const cleanupSpool = () => uploaded.forEach(file => { if (file.path) { try { fs.unlinkSync(file.path); } catch { /* best-effort */ } } });
@@ -398,6 +430,7 @@ export class CollabController {
@RequirePermission('collab_edit')
@Post('messages/:id/react')
@HttpCode(200)
@ResponseContract(collabReactionsResponseSchema)
async react(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Body() body: CollabReactionDto, @Headers('x-socket-id') socketId?: string) {
const result = await this.collab.reactMessage(id, tripId, user.id, body.emoji);
if (!result.found) {
@@ -410,6 +443,7 @@ export class CollabController {
@UseGuards(TripAccessGuard)
@RequirePermission('collab_edit')
@Delete('messages/:id')
@ResponseContract(successResponseSchema)
async deleteMessage(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
const result = await this.collab.deleteMessage(tripId, id, user.id);
if (result.error === 'not_found') throw new HttpException({ error: 'Message not found' }, 404);
@@ -428,6 +462,7 @@ export class CollabController {
// inside the service, not a write permission.
@UseGuards(TripAccessGuard)
@Get('link-preview')
@ResponseContract(collabLinkPreviewResponseSchema)
async linkPreview(@CurrentUser() user: User, @Param('tripId') tripId: string, @Query('url') url?: string) {
// Unlike the legacy route, this verifies trip access — any authed user
// could otherwise drive the SSRF-guarded fetcher through arbitrary trip URLs.
@@ -24,6 +24,7 @@ export class FilesDownloadController {
private readonly storage: StorageService,
) {}
// response-contract-exempt: streams the file through @Res(), there is no JSON body to check.
@Get(':id/download')
async download(
@Req() req: Request,
+20
View File
@@ -33,6 +33,15 @@ import { MAX_FILE_SIZE, BLOCKED_EXTENSIONS, isUploadTypeAllowed, isVideoExtensio
import { FileUploadDto, FileUpdateDto, FileLinkDto } from './files.dto';
import { AllowedFileTypesService } from './allowed-file-types.service';
import { logError } from '../audit/audit-log.logger';
import { ResponseContract } from '../common/response-contract';
import {
fileLinkCreateResponseSchema,
fileLinksResponseSchema,
fileTrashEmptyResponseSchema,
successResponseSchema,
tripFileResponseSchema,
tripFilesResponseSchema,
} from '@trek/shared';
/**
* Trip-file upload filter, built from the container.
@@ -126,12 +135,14 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Get()
@ResponseContract(tripFilesResponseSchema)
async list(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Query('trash') trash?: string) {
return { files: await this.files.listFiles(tripId, trash === 'true') };
}
@Post()
@UseInterceptors(FileInterceptor('file'))
@ResponseContract(tripFileResponseSchema)
async upload(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@@ -197,6 +208,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Put(':id')
@ResponseContract(tripFileResponseSchema)
async update(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Param('id') id: string, @Body() body: FileUpdateDto, @Headers('x-socket-id') socketId?: string) {
if (!(await this.files.can('file_edit', trip, user))) {
throw new HttpException({ error: 'No permission to edit files' }, 403);
@@ -218,6 +230,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Patch(':id/star')
@ResponseContract(tripFileResponseSchema)
async star(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
if (!(await this.files.can('file_edit', trip, user))) {
throw new HttpException({ error: 'No permission' }, 403);
@@ -233,6 +246,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Delete('trash/empty')
@ResponseContract(fileTrashEmptyResponseSchema)
async emptyTrash(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string) {
if (!(await this.files.can('file_delete', trip, user))) {
throw new HttpException({ error: 'No permission' }, 403);
@@ -243,6 +257,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Delete(':id/permanent')
@ResponseContract(successResponseSchema)
async permanent(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
if (!(await this.files.can('file_delete', trip, user))) {
throw new HttpException({ error: 'No permission' }, 403);
@@ -258,6 +273,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Delete(':id')
@ResponseContract(successResponseSchema)
async remove(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
if (!(await this.files.can('file_delete', trip, user))) {
throw new HttpException({ error: 'No permission to delete files' }, 403);
@@ -274,6 +290,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Post(':id/restore')
@HttpCode(200) // Express answers restore with res.json (200), not the POST-default 201.
@ResponseContract(tripFileResponseSchema)
async restore(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Param('id') id: string, @Headers('x-socket-id') socketId?: string) {
if (!(await this.files.can('file_delete', trip, user))) {
throw new HttpException({ error: 'No permission' }, 403);
@@ -290,6 +307,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Post(':id/link')
@HttpCode(200) // Express answers link with res.json (200).
@ResponseContract(fileLinkCreateResponseSchema)
async link(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Param('id') id: string, @Body() body: FileLinkDto) {
if (!(await this.files.can('file_edit', trip, user))) {
throw new HttpException({ error: 'No permission' }, 403);
@@ -305,6 +323,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Delete(':id/link/:linkId')
@ResponseContract(successResponseSchema)
async unlink(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Param('id') id: string, @Param('linkId') linkId: string) {
if (!(await this.files.can('file_edit', trip, user))) {
throw new HttpException({ error: 'No permission' }, 403);
@@ -322,6 +341,7 @@ export class FilesController {
@UseGuards(TripAccessGuard)
@Get(':id/links')
@ResponseContract(fileLinksResponseSchema)
async links(@CurrentUser() user: User, @Trip() trip: TripAccess, @Param('tripId') tripId: string, @Param('id') id: string) {
const file = await this.files.getFileById(id, tripId);
if (!file) {
@@ -1,6 +1,12 @@
import { Body, Controller, Get, HttpCode, HttpException, Post, Put, Req, UseGuards } from '@nestjs/common';
import type { Request } from 'express';
import { MASKED_SETTING_VALUE } from '@trek/shared';
import {
MASKED_SETTING_VALUE,
adminDefaultUserSettingsResponseSchema,
settingUpsertResponseSchema,
settingsBulkResponseSchema,
settingsListResponseSchema,
} from '@trek/shared';
import type { User } from '../../types';
import { SettingsService, isAdminOnlyEndpointSetting } from './settings.service';
import { AdminDefaultUserSettingsDto, SettingUpsertDto, SettingsBulkDto } from './settings.dto';
@@ -11,6 +17,7 @@ import { getClientIp } from '../audit/client-ip';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { isManagedLockedKey, splitManagedKeys } from '../common/managed';
import { ResponseContract } from '../common/response-contract';
/**
* /api/settings — per-user key/value preferences: get-all, single upsert
@@ -54,11 +61,13 @@ export class SettingsController {
}
@Get()
@ResponseContract(settingsListResponseSchema)
async list(@CurrentUser() user: User) {
return { settings: await this.settings.getUserSettings(user.id) };
}
@Put()
@ResponseContract(settingUpsertResponseSchema)
async upsert(@CurrentUser() user: User, @Body() body: SettingUpsertDto) {
this.assertMayWriteInstanceEndpoint({ [body.key]: body.value });
// assertMayWriteInstanceEndpoint only covers llm_base_url and provider 'local'.
@@ -77,6 +86,7 @@ export class SettingsController {
@Post('bulk')
@HttpCode(200) // Express answers bulk with res.json (200), not the POST-default 201.
@ResponseContract(settingsBulkResponseSchema)
async bulk(@CurrentUser() user: User, @Body() body: SettingsBulkDto) {
this.assertMayWriteInstanceEndpoint(body.settings);
const { allowed, blocked } = splitManagedKeys(body.settings, this.env.isManaged());
@@ -107,11 +117,13 @@ export class AdminDefaultUserSettingsController {
) {}
@Get()
@ResponseContract(adminDefaultUserSettingsResponseSchema)
async get() {
return this.settings.getAdminUserDefaults();
}
@Put()
@ResponseContract(adminDefaultUserSettingsResponseSchema)
async update(@CurrentUser() user: User, @Body() body: AdminDefaultUserSettingsDto, @Req() req: Request) {
try {
// Deliberately no managed_keys in the response here: the route answers
+15
View File
@@ -17,6 +17,14 @@ import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard';
import { toRowId } from '../common/row-id';
import { ResponseContract } from '../common/response-contract';
import {
successResponseSchema,
todoCategoryAssigneesResponseSchema,
todoCategoryAssigneesUpdateResponseSchema,
todoItemResponseSchema,
todoListResponseSchema,
} from '@trek/shared';
/**
* /api/trips/:tripId/todo — trip-scoped task list.
@@ -40,12 +48,14 @@ export class TodoController {
@Get()
@ResponseContract(todoListResponseSchema)
async list(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return { items: await this.todo.listItems(tripId) };
}
@RequirePermission('packing_edit')
@Post()
@ResponseContract(todoItemResponseSchema)
async create(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@@ -60,6 +70,7 @@ export class TodoController {
@RequirePermission('packing_edit')
@Put('reorder')
@ResponseContract(successResponseSchema)
async reorder(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@@ -71,6 +82,7 @@ export class TodoController {
@RequirePermission('packing_edit')
@Put(':id')
@ResponseContract(todoItemResponseSchema)
async update(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@@ -105,6 +117,7 @@ export class TodoController {
@RequirePermission('packing_edit')
@Delete(':id')
@ResponseContract(successResponseSchema)
async remove(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@@ -124,12 +137,14 @@ export class TodoController {
}
@Get('category-assignees')
@ResponseContract(todoCategoryAssigneesResponseSchema)
async categoryAssignees(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return { assignees: await this.todo.getCategoryAssignees(tripId) };
}
@RequirePermission('packing_edit')
@Put('category-assignees/:categoryName')
@ResponseContract(todoCategoryAssigneesUpdateResponseSchema)
async updateCategoryAssignees(
@CurrentUser() user: User,
@Param('tripId') tripId: string,