All files / src/presentation/http/routes public.routes.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                    21x           22x   22x                           4x   4x 136x               132x               4x        
import type { App } from '../../../infrastructure/http/build-app';
import { PrismaVaccineRepository } from '../../../infrastructure/database/repositories/prisma-vaccine.repository';
import { CachedVaccineRepository } from '../../../infrastructure/database/repositories/cached-vaccine.repository';
import { prisma } from '../../../infrastructure/database/prisma-client';
import { redis } from '../../../infrastructure/cache/redis-client';
import { publicVaccineScheduleResponseSchema } from '../schemas/public.schema';
 
/**
 * The only unauthenticated, cross-origin-readable surface of this API.
 *
 * Everything under /public/ answers with `Access-Control-Allow-Origin: *` and, critically,
 * WITHOUT `Access-Control-Allow-Credentials` — see the CORS delegate in build-app.ts. That pairing
 * is what makes it safe: a wildcard origin and cookies are mutually exclusive by browser rule, so
 * a page on any origin can read this data but can never make an authenticated call here on a
 * visitor's behalf.
 *
 * The rule for anything added under this prefix: it must be data that is identical for every
 * visitor and tied to no account. The PNI immunization schedule qualifies — it is published public
 * health policy, the same rows for everyone, and carries no personal data at all.
 */
/**
 * Exported so the CORS delegate in build-app.ts matches on the same string this router mounts.
 * If these two ever drift, the failure is silent and one-directional: routes would still answer,
 * but under the strict policy, so the widget breaks in the browser while every server-side probe
 * (curl, health check, integration test without an Origin header) keeps passing.
 */
export const PUBLIC_ROUTE_PREFIX = '/public/';
 
export async function publicRoutes(app: App) {
  // Reads go through the Redis-backed cache, not straight to Postgres. This endpoint is the one
  // that an embedded widget on someone else's site hits, so its traffic is the least predictable
  // of the whole API — and the catalog changes about as often as national policy does.
  const vaccineRepository = new CachedVaccineRepository(new PrismaVaccineRepository(prisma), redis);
 
  app.route({
    method: 'GET',
    url: '/public/vaccine-schedule',
    schema: {
      tags: ['Public'],
      summary: 'The PNI immunization schedule (public, no authentication)',
      description:
        'Returns the Brazilian national immunization schedule as reference data. Readable from any origin so it ' +
        'can back an embeddable widget. Contains no personal data and requires no authentication.',
      response: {
        200: publicVaccineScheduleResponseSchema,
      },
    },
    handler: async (_request, reply) => {
      const vaccines = await vaccineRepository.findAll();
 
      const schedule = vaccines
        .map((vaccine) => ({
          id: vaccine.id,
          name: vaccine.name,
          description: vaccine.description,
          recommendedAgeInMonths: vaccine.recommendedAgeInMonths,
          doseNumber: vaccine.doseNumber,
        }))
        .sort((a, b) =>
          a.recommendedAgeInMonths === b.recommendedAgeInMonths
            ? a.doseNumber - b.doseNumber
            : a.recommendedAgeInMonths - b.recommendedAgeInMonths,
        );
 
      // Reference data that changes with national policy, not with usage: worth caching hard at
      // the edge and in the browser. `public` is deliberate — there is nothing per-visitor here,
      // so a shared cache holding one copy for everyone is correct rather than a leak.
      return reply.header('cache-control', 'public, max-age=3600').send({ schedule });
    },
  });
}