# Bem Coberto

> Travel insurance marketplace for Brazilian travellers: quote several insurers in one search, compare every plan in one format, pay with Pix or card. Built around insurer adapters, a payment-safe order state machine and an outbox-driven worker.

Case study · Vinícius Albuquerque · https://www.vinicius-albuquerque.com.br/en

- Status: In development
- Case study (HTML): https://www.vinicius-albuquerque.com.br/en/projeto/bem-coberto

## Overview

Bem Coberto is a travel insurance marketplace for Brazilian travellers, currently in development. One search quotes several insurers in parallel, every plan is normalized into one comparable format, and the customer pays with Pix or card and receives the policy by email and in their account. The hard part is money and policies moving between systems that share no transactions, so the architecture is built around a few firm rules. Each insurer sits behind a single TravelInsuranceProvider adapter (quote, issue, find issued, cancel, health): domain code never sees an insurer's types, and adding an insurer means writing one adapter. Quotes fan out with per-insurer timeouts, are cached in Redis, and degrade to partial results when one insurer fails. Payment follows a charge-then-issue model: the payment webhook marks the order paid and writes an outbox row in the same database transaction, and a BullMQ worker issues the policy, stores the files and sends the email. Orders move through an explicit state machine owned by the domain package, every transition is recorded, and an issue call that times out is never retried blindly; it is reconciled against the insurer, so no one is charged without a policy or a refund. The stack is a strict TypeScript monorepo on pnpm and Turborepo: Next.js App Router for the storefront and back office, NestJS on Fastify for the API, PostgreSQL with Prisma, Redis, S3 and SES on AWS, with every decision recorded in a build spec and ADRs.

## Highlights

- Insurer adapters behind one TravelInsuranceProvider interface: domain code never imports an insurer's types, and adding an insurer never touches checkout
- Parallel quoting with per-insurer timeouts, Redis caching and partial results when an insurer is down, targeting under 8 s at p95
- Payment-safe issuing: transactional outbox, BullMQ worker, and reconciliation instead of blind retries, so no one is charged without a policy or a refund
- Order state machine in a pure domain package, alongside integer-cent money, Pix and card pricing with installments, and a best-value ranking that checks the Schengen €30k minimum
- One Zod contract shared by API and web: requests validated, every response parsed at runtime, OpenAPI 3.1 generated from the same schemas
- Quality gates in CI: lint, typecheck, Vitest, Playwright end-to-end tests with axe accessibility checks, and a 200 KB JavaScript budget on results

## Architecture

Storefront (Next.js App Router) → NestJS API (Quote fan-out to insurer adapters) → Payment webhook (Outbox row, same transaction) → BullMQ worker (Issue policy, store files, email)

Reconciled against the insurer, never retried blindly


## Technologies

Turborepo, Next.js, NestJS, TypeScript, PostgreSQL, Redis, BullMQ, Zod, AWS, Playwright

## Screenshots

![Bem Coberto home with the trip search](https://www.vinicius-albuquerque.com.br/projects/bem-coberto/bem-coberto-home.avif)

![Results page with three recommended plans and filters](https://www.vinicius-albuquerque.com.br/projects/bem-coberto/bem-coberto-results.avif)

![Side-by-side comparison of two plans](https://www.vinicius-albuquerque.com.br/projects/bem-coberto/bem-coberto-compare.avif)

![Plan detail with every coverage explained](https://www.vinicius-albuquerque.com.br/projects/bem-coberto/bem-coberto-coverage.avif)
