# vorellana99/io-card-issuing architecture

> Architecture diagram of the GitHub repository vorellana99/io-card-issuing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vorellana99/io-card-issuing
- Repository: https://github.com/vorellana99/io-card-issuing

## Overview

1. **Repository shape**
   - The repo contains two independent NestJS services: `card-issuer` and `card-processor`.
   - They do not share a runtime; each has its own `package.json`, Dockerfile, tests, and SQLite data directory.
   - The top-level `docker-compose.yml` wires both services together with Kafka and Kafka UI.

2. **Primary system boundary**
   - `card-issuer` is the only HTTP-facing entry point for clients.
   - `card-processor` is not exposed over HTTP; it reacts only to Kafka events.
   - This creates a clean split between synchronous admission/queries and asynchronous card issuance.

3. **card-issuer responsibilities**
   - `src/cards/cards.controller.ts` handles `POST /cards/issue` and `GET /cards/:requestId/status`.
   - `src/cards/cards.service.ts` owns request validation, uniqueness checks, persistence of card requests, and publishing the initial Kafka event.
   - `src/consumer/card-events.controller.ts` consumes Kafka responses and updates request status when issuance completes or fails.

4. **card-issuer persistence model**
   - `src/cards/entities/card-request.entity.ts` represents the local SQLite table of issuance requests.
   - The issuer stores request lifecycle state such as `PENDING`, `ISSUED`, or `FAILED`.
   - The README indicates a unique constraint on `documentNumber`, which enforces the “one client = one card” rule before any Kafka publish happens.

5. **card-processor responsibilities**
   - `src/consumer/card-requested.controller.ts` consumes `io.card.requested.v1`.
   - `src/consumer/card-issuance.service.ts` performs the issuance workflow, including retries and result publication.
   - `src/cards/card-generator.ts` and `src/cards/entities/card.entity.ts` model the generated card data stored by the processor.

6. **Event-driven data flow**
   - A client submits card issuance to `card-issuer`.
   - `card-issuer` persists a request and publishes a CloudEvents-style message to Kafka with type `io.card.requested.v1`.
   - `card-processor` consumes that event, simulates or performs issuance, then publishes either `io.cards.issued.v1` or `io.card.requested.v1.dlq`.
   - `card-issuer` consumes the result event and updates the original request row in SQLite.

7. **Event contract and correlation**
   - Both services use a shared CloudEvents-like envelope defined in `src/common/cloud-event.ts`.
   - The event `source` is the `requestId`, which acts as the correlation key across logs, Kafka messages, and database records.
   - This is the main traceability boundary in the architecture.

8. **Kafka and integration pattern**
   - `card-issuer` runs as a hybrid Nest app: HTTP server plus Kafka consumer in the same process.
   - `card-processor` is a Kafka-only microservice.
   - Both use a dedicated Kafka module and an event publisher service, indicating messaging is a first-class internal subsystem rather than a transport detail.

9. **Reliability and retry boundary**
   - The processor has explicit retry policy logic in `src/retry/retry.policy.ts`.
   - The issuance flow retries multiple times with backoff, and after exhaustion emits to a DLQ topic.
   - `forceError: true` in the request DTO is a deliberate test hook to deterministically exercise the failure/DLQ path.

10. **Validation and API contracts**
   - `card-issuer` defines request/response DTOs under `src/cards/dto`.
   - Validation is domain-specific: document number format, adult age, supported product and currency values.
   - Swagger documentation exists for the issuer API, which matters because the issuer is the external contract boundary.

11. **Observability and operational tooling**
   - Both services use structured logging via Pino (`nestjs-pino`), with the request `source`/`requestId` carried through logs.
   - Kafka UI is part of the deployed stack for inspecting topics, consumer groups, and message flow.
   - This makes the system operable as an event-driven pipeline rather than just two isolated applications.

12. **Runtime and infrastructure**
   - The whole stack is containerized with Docker Compose.
   - Kafka runs in KRaft mode, and each service uses its own SQLite database stored under its `data/` directory.
   - The architecture is therefore local-first and self-contained: HTTP ingress, Kafka backbone, and per-service embedded persistence.

Main entry points: [`card-processor/src/consumer/card-issuance.service.ts`](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/consumer/card-issuance.service.ts) (Issuance service), [`card-issuer/src/cards/cards.service.ts`](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/cards.service.ts) (Card service), [`card-issuer/src/app.module.ts`](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/app.module.ts) (Issuer module), [`card-issuer/src/cards/cards.module.ts`](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/cards.module.ts) (Cards module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_infra["Infra"]
  node_compose["Compose<br/>deployment<br/>[docker-compose.yml]"]
end

subgraph group_issuer["card-issuer"]
  node_issuer_main(("Issuer app<br/>nest bootstrap<br/>[main.ts]"))
  node_issuer_app{{"Issuer module<br/>nest module<br/>[app.module.ts]"}}
  node_issuer_cards_module{{"Cards module<br/>domain module<br/>[cards.module.ts]"}}
  node_issuer_cards_controller["Cards API<br/>http controller"]
  node_issuer_cards_service["Card service<br/>domain service<br/>[cards.service.ts]"]
  node_issuer_card_request[("Request row<br/>sqlite entity")]
  node_issuer_consumer["Result consumer<br/>kafka controller"]
  node_issuer_publisher["Event publisher<br/>kafka publisher"]
  node_issuer_event["Cloud event<br/>event contract<br/>[cloud-event.ts]"]
end

subgraph group_processor["card-processor"]
  node_processor_main(("Worker app<br/>nest bootstrap<br/>[main.ts]"))
  node_processor_app{{"Worker module<br/>nest module<br/>[app.module.ts]"}}
  node_processor_consumer["Request consumer<br/>kafka controller"]
  node_processor_service["Issuance service<br/>workflow service"]
  node_processor_retry["Retry policy<br/>[retry.policy.ts]"]
  node_processor_generator["Card generator<br/>domain service<br/>[card-generator.ts]"]
  node_processor_card[("Card store<br/>sqlite entity<br/>[card.entity.ts]")]
  node_processor_publisher["Event publisher<br/>kafka publisher"]
  node_processor_event["Cloud event<br/>event contract<br/>[cloud-event.ts]"]
end

node_compose -->|"runs"| node_issuer_main
node_compose -->|"runs"| node_processor_main
node_issuer_main -->|"loads"| node_issuer_app
node_issuer_app -->|"includes"| node_issuer_cards_module
node_issuer_app -->|"includes"| node_issuer_consumer
node_issuer_cards_module -->|"exposes"| node_issuer_cards_controller
node_issuer_cards_module -->|"uses"| node_issuer_cards_service
node_issuer_cards_service -->|"stores"| node_issuer_card_request
node_issuer_cards_service -->|"publishes"| node_issuer_publisher
node_issuer_cards_service -->|"formats"| node_issuer_event
node_issuer_consumer -->|"updates"| node_issuer_card_request
node_processor_main -->|"loads"| node_processor_app
node_processor_app -->|"includes"| node_processor_consumer
node_processor_app -->|"includes"| node_processor_service
node_processor_service -->|"applies"| node_processor_retry
node_processor_service -->|"builds"| node_processor_generator
node_processor_service -->|"stores"| node_processor_card
node_processor_service -->|"publishes"| node_processor_publisher
node_processor_service -->|"formats"| node_processor_event
node_processor_consumer -->|"drives"| node_processor_service
node_issuer_consumer -->|"consumes"| node_processor_publisher
node_issuer_publisher -->|"requests"| node_processor_consumer

click node_compose "https://github.com/vorellana99/io-card-issuing/blob/main/docker-compose.yml"
click node_issuer_main "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/main.ts"
click node_issuer_app "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/app.module.ts"
click node_issuer_cards_module "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/cards.module.ts"
click node_issuer_cards_controller "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/cards.controller.ts"
click node_issuer_cards_service "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/cards.service.ts"
click node_issuer_card_request "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/entities/card-request.entity.ts"
click node_issuer_consumer "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/consumer/card-events.controller.ts"
click node_issuer_publisher "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/kafka/event-publisher.service.ts"
click node_issuer_event "https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/common/cloud-event.ts"
click node_processor_main "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/main.ts"
click node_processor_app "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/app.module.ts"
click node_processor_consumer "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/consumer/card-requested.controller.ts"
click node_processor_service "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/consumer/card-issuance.service.ts"
click node_processor_retry "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/retry/retry.policy.ts"
click node_processor_generator "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/cards/card-generator.ts"
click node_processor_card "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/cards/entities/card.entity.ts"
click node_processor_publisher "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/kafka/event-publisher.service.ts"
click node_processor_event "https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/common/cloud-event.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_compose toneBlue
class node_issuer_main,node_issuer_app,node_issuer_cards_module,node_issuer_cards_controller,node_issuer_cards_service,node_issuer_card_request,node_issuer_consumer,node_issuer_publisher,node_issuer_event toneAmber
class node_processor_main,node_processor_app,node_processor_consumer,node_processor_service,node_processor_retry,node_processor_generator,node_processor_card,node_processor_publisher,node_processor_event toneMint
```

## Components

### Infra

Containerized runtime and messaging backbone

- [Compose](https://github.com/vorellana99/io-card-issuing/blob/main/docker-compose.yml): deployment · Wires both services with Kafka and Kafka UI · `docker-compose.yml`

### card-issuer

HTTP-facing issuance service

- [Issuer app](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/main.ts): nest bootstrap · Starts the hybrid HTTP + Kafka runtime · `card-issuer/src/main.ts`
- [Issuer module](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/app.module.ts): nest module · Composes cards, Kafka, consumer, and persistence · `card-issuer/src/app.module.ts`
- [Cards module](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/cards.module.ts): domain module · HTTP API and request state management · `card-issuer/src/cards/cards.module.ts`
- [Cards API](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/cards.controller.ts): http controller · Issue card and status endpoints · `card-issuer/src/cards/cards.controller.ts`
- [Card service](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/cards.service.ts): domain service · Validates, persists, and publishes issuance requests · `card-issuer/src/cards/cards.service.ts`
- [Request row](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/cards/entities/card-request.entity.ts): sqlite entity · Local issuance lifecycle record with unique document number · `card-issuer/src/cards/entities/card-request.entity.ts`
- [Result consumer](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/consumer/card-events.controller.ts): kafka controller · Applies issuance success or failure events · `card-issuer/src/consumer/card-events.controller.ts`
- [Event publisher](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/kafka/event-publisher.service.ts): kafka publisher · Emits the initial requested event · `card-issuer/src/kafka/event-publisher.service.ts`
- [Cloud event](https://github.com/vorellana99/io-card-issuing/blob/main/card-issuer/src/common/cloud-event.ts): event contract · Shared envelope with requestId correlation · `card-issuer/src/common/cloud-event.ts`

### card-processor

Kafka-driven issuance worker

- [Worker app](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/main.ts): nest bootstrap · Starts the Kafka-only processor runtime · `card-processor/src/main.ts`
- [Worker module](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/app.module.ts): nest module · Composes consumer, Kafka, retry, and card generation · `card-processor/src/app.module.ts`
- [Request consumer](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/consumer/card-requested.controller.ts): kafka controller · Consumes card requested events · `card-processor/src/consumer/card-requested.controller.ts`
- [Issuance service](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/consumer/card-issuance.service.ts): workflow service · Runs retries and publishes outcomes · `card-processor/src/consumer/card-issuance.service.ts`
- [Retry policy](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/retry/retry.policy.ts): retry policy · Backoff and exhaustion rules for issuance · `card-processor/src/retry/retry.policy.ts`
- [Card generator](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/cards/card-generator.ts): domain service · Builds generated card data · `card-processor/src/cards/card-generator.ts`
- [Card store](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/cards/entities/card.entity.ts): sqlite entity · Persisted generated card record · `card-processor/src/cards/entities/card.entity.ts`
- [Event publisher](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/kafka/event-publisher.service.ts): kafka publisher · Emits issued or DLQ events · `card-processor/src/kafka/event-publisher.service.ts`
- [Cloud event](https://github.com/vorellana99/io-card-issuing/blob/main/card-processor/src/common/cloud-event.ts): event contract · Shared envelope with requestId correlation · `card-processor/src/common/cloud-event.ts`

## Connections

- Compose → Issuer app: runs · Docker Compose starts the issuer service
- Compose → Worker app: runs · Docker Compose starts the processor service
- Issuer app → Issuer module: loads · Bootstrap wires the issuer modules
- Issuer module → Cards module: includes · Registers the cards boundary
- Issuer module → Result consumer: includes · Registers Kafka result handling
- Cards module → Cards API: exposes · HTTP entry points for clients
- Cards module → Card service: uses · Coordinates validation and persistence
- Card service → Request row: stores · Persists issuance request state
- Card service → Event publisher: publishes · Sends the initial request event
- Card service → Cloud event: formats · Builds the shared event envelope
- Result consumer → Request row: updates · Marks requests issued or failed
- Worker app → Worker module: loads · Bootstrap wires the worker modules
- Worker module → Request consumer: includes · Registers Kafka request handling
- Worker module → Issuance service: includes · Registers issuance workflow
- Issuance service → Retry policy: applies · Uses retry and backoff policy
- Issuance service → Card generator: builds · Creates generated card data
- Issuance service → Card store: stores · Persists issued card data
- Issuance service → Event publisher: publishes · Emits issued or DLQ events
- Issuance service → Cloud event: formats · Builds the shared event envelope
- Request consumer → Issuance service: drives · Starts the issuance workflow from Kafka
- Result consumer → Event publisher: consumes · Receives completion or failure events
- Event publisher → Request consumer: requests · Publishes card requested events to Kafka
