|
| 1 | +/** |
| 2 | + * Project buzz routes: |
| 3 | + * GET /api/projects/:slug/buzz |
| 4 | + * GET /api/project-buzz (global feed) |
| 5 | + */ |
| 6 | +import type { FastifyInstance } from 'fastify'; |
| 7 | +import { paginated } from '../lib/response.js'; |
| 8 | +import { ApiNotFoundError, ApiValidationError } from '../lib/errors.js'; |
| 9 | +import { getCallerSession } from '../services/permissions.js'; |
| 10 | + |
| 11 | +export async function projectBuzzRoutes(fastify: FastifyInstance): Promise<void> { |
| 12 | + // GET /api/projects/:slug/buzz |
| 13 | + fastify.get( |
| 14 | + '/api/projects/:slug/buzz', |
| 15 | + { |
| 16 | + schema: { |
| 17 | + tags: ['project-buzz'], |
| 18 | + summary: "List a project's buzz", |
| 19 | + params: { |
| 20 | + type: 'object', |
| 21 | + properties: { slug: { type: 'string' } }, |
| 22 | + required: ['slug'], |
| 23 | + }, |
| 24 | + querystring: { |
| 25 | + type: 'object', |
| 26 | + properties: { |
| 27 | + sort: { type: 'string' }, |
| 28 | + page: { type: 'integer', minimum: 1 }, |
| 29 | + perPage: { type: 'integer', minimum: 1, maximum: 100 }, |
| 30 | + }, |
| 31 | + additionalProperties: false, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + async (request) => { |
| 36 | + const { slug } = request.params as { slug: string }; |
| 37 | + const q = request.query as Record<string, unknown>; |
| 38 | + const caller = getCallerSession(request); |
| 39 | + |
| 40 | + const opts = { |
| 41 | + sort: q['sort'] as string | undefined, |
| 42 | + page: q['page'] as number | undefined, |
| 43 | + perPage: q['perPage'] as number | undefined, |
| 44 | + }; |
| 45 | + |
| 46 | + const result = fastify.services.projectBuzz.listForProject(slug, opts, caller); |
| 47 | + |
| 48 | + if ('error' in result) { |
| 49 | + if (result.error === 'not_found') throw new ApiNotFoundError(`Project '${slug}' not found`); |
| 50 | + if (result.error === 'invalid_sort') { |
| 51 | + throw new ApiValidationError('Unknown sort key', { sort: 'unknown sort key' }); |
| 52 | + } |
| 53 | + throw new ApiValidationError('Invalid parameter'); |
| 54 | + } |
| 55 | + |
| 56 | + const page = Math.max(1, opts.page ?? 1); |
| 57 | + const perPage = Math.min(100, Math.max(1, opts.perPage ?? 20)); |
| 58 | + |
| 59 | + return paginated(result.items, { |
| 60 | + page, |
| 61 | + perPage, |
| 62 | + totalItems: result.totalItems, |
| 63 | + totalPages: Math.ceil(result.totalItems / perPage), |
| 64 | + }); |
| 65 | + }, |
| 66 | + ); |
| 67 | + |
| 68 | + // GET /api/project-buzz (global feed) |
| 69 | + fastify.get( |
| 70 | + '/api/project-buzz', |
| 71 | + { |
| 72 | + schema: { |
| 73 | + tags: ['project-buzz'], |
| 74 | + summary: 'Global buzz feed', |
| 75 | + querystring: { |
| 76 | + type: 'object', |
| 77 | + properties: { |
| 78 | + page: { type: 'integer', minimum: 1 }, |
| 79 | + perPage: { type: 'integer', minimum: 1, maximum: 100 }, |
| 80 | + since: { type: 'string' }, |
| 81 | + tag: { type: 'array', items: { type: 'string' } }, |
| 82 | + }, |
| 83 | + additionalProperties: false, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + async (request) => { |
| 88 | + const q = request.query as Record<string, unknown>; |
| 89 | + const caller = getCallerSession(request); |
| 90 | + |
| 91 | + const opts = { |
| 92 | + page: q['page'] as number | undefined, |
| 93 | + perPage: q['perPage'] as number | undefined, |
| 94 | + since: q['since'] as string | undefined, |
| 95 | + tag: q['tag'] as string[] | undefined, |
| 96 | + }; |
| 97 | + |
| 98 | + const result = fastify.services.projectBuzz.globalFeed(opts, caller); |
| 99 | + |
| 100 | + const page = Math.max(1, opts.page ?? 1); |
| 101 | + const perPage = Math.min(100, Math.max(1, opts.perPage ?? 30)); |
| 102 | + |
| 103 | + return paginated(result.items, { |
| 104 | + page, |
| 105 | + perPage, |
| 106 | + totalItems: result.totalItems, |
| 107 | + totalPages: Math.ceil(result.totalItems / perPage), |
| 108 | + }); |
| 109 | + }, |
| 110 | + ); |
| 111 | +} |
0 commit comments