# ayushgupta2408/identity-reconciliation architecture

> Architecture diagram of the GitHub repository ayushgupta2408/identity-reconciliation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ayushgupta2408/identity-reconciliation
- Repository: https://github.com/ayushgupta2408/identity-reconciliation

## Overview

1. **Overall shape**
   - This is a small backend service centered on one business capability: contact identity reconciliation.
   - The runtime boundary is an Express API backed by PostgreSQL, with Prisma as the persistence layer.

2. **Process entrypoints**
   - `src/server.js` is the executable entrypoint that starts the HTTP listener.
   - `src/app.js` builds the Express application itself, keeping app wiring separate from process startup.

3. **HTTP surface**
   - The public API is intentionally narrow:
     - `POST /identify` for reconciliation
     - `GET /health` for liveness/readiness and DB connectivity checks
   - Route definitions live under `src/routes/`, which keeps endpoint mapping separate from business logic.

4. **Request handling flow**
   - Requests go through middleware first, then the controller, then the service layer.
   - `src/middleware/validateRequest.js` enforces input rules such as “at least one of email or phoneNumber” and format/normalization concerns.
   - `src/controllers/identify.controller.js` is thin glue: it translates HTTP requests into a service call and formats the response.

5. **Core business logic boundary**
   - `src/services/contact.service.js` contains the reconciliation algorithm and is the main domain boundary in the repo.
   - It is responsible for:
     - finding matching contacts,
     - creating primary contacts,
     - creating secondary contacts,
     - consolidating linked identities,
     - merging two previously separate identity clusters when a bridging request arrives.

6. **Transactional data flow**
   - The identify flow runs inside a single Prisma transaction.
   - That transaction encloses the read/decide/write sequence so concurrent requests cannot create inconsistent duplicate primaries.
   - The service uses the database as the source of truth for identity clustering, not in-memory state.

7. **Persistence model**
   - Prisma is the ORM layer (`src/utils/prisma.js` plus `prisma/schema.prisma`).
   - The main model is `Contact`, with the repository treating rows as either:
     - a `primary` identity, or
     - a `secondary` contact linked via `linkedId`.
   - The schema and migrations also support soft deletion via `deletedAt`, so queries must filter for active rows only.

8. **Database responsibilities**
   - PostgreSQL stores contact records and link relationships.
   - The README notes indexes on `email`, `phoneNumber`, and `linkedId`, which are important because lookup and cluster traversal are the dominant query patterns.
   - `prisma/migrations/` captures schema evolution, and `prisma/seed.js` provides optional sample data.

9. **Response assembly**
   - The service returns a consolidated identity view:
     - `primaryContactId`
     - deduplicated `emails`
     - deduplicated `phoneNumbers`
     - `secondaryContactIds`
   - The response is derived from the primary plus all linked secondary records, with the primary’s own values listed first.

10. **Error and observability boundary**
    - `src/utils/errors.js` defines application error types used to distinguish validation failures from unexpected server errors.
    - `src/middleware/errorHandler.js` standardizes error responses so clients get safe messages while internal details stay in logs.
    - `src/utils/logger.js` handles server-side logging, which is the main observability mechanism in the codebase.

11. **Operational packaging**
    - `Dockerfile` and `docker-compose.yml` package the app for local/containerized deployment alongside PostgreSQL.
    - The health route is used by Docker health checks and can also support orchestration probes.

12. **Test boundary**
    - `tests/identify.test.js` exercises the API behavior end-to-end enough to validate the reconciliation rules against a real database.
    - The test suite covers the key state transitions: new primary creation, secondary creation, no-op repeats, bridge merges, validation, and health checking.

13. **Supporting artifacts**
    - `postman_collection.json` documents the API for manual testing.
    - The repo is structured around a clean separation of HTTP, domain logic, and persistence, which is the key architectural feature to capture in a diagram.

Main entry points: [`src/app.js`](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/app.js) (App), [`src/services/contact.service.js`](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/services/contact.service.js) (Contact service), [`src/utils/prisma.js`](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/utils/prisma.js) (Prisma client), [`src/routes/identify.routes.js`](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/routes/identify.routes.js) (Identify route)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_server["Server<br/>entrypoint<br/>[server.js]"]
  node_app["App<br/>[app.js]"]
end

subgraph group_http["HTTP API"]
  node_identify_route["Identify route<br/>[identify.routes.js]"]
  node_health_route["Health route<br/>[health.routes.js]"]
  node_validate["Validate<br/>middleware<br/>[validateRequest.js]"]
  node_identify_controller["Identify controller"]
end

subgraph group_domain["Identity Logic"]
  node_contact_service["Contact service<br/>domain service<br/>[contact.service.js]"]
end

subgraph group_data["Persistence"]
  node_prisma_util[("Prisma client<br/>db client<br/>[prisma.js]")]
  node_schema["Schema<br/>[schema.prisma]"]
  node_migrations["Migrations"]
  node_db[("PostgreSQL<br/>database")]
end

subgraph group_ops["Operations"]
  node_errors["Errors<br/>error types<br/>[errors.js]"]
  node_error_handler["Error handler<br/>middleware<br/>[errorHandler.js]"]
  node_logger["Logger<br/>logging<br/>[logger.js]"]
  node_docker["Docker<br/>container config"]
  node_compose["Compose<br/>dev stack<br/>[docker-compose.yml]"]
end

subgraph group_test["Testing"]
  node_tests["Identity tests<br/>integration tests<br/>[identify.test.js]"]
end

node_server -->|"boots"| node_app
node_app -->|"mounts"| node_identify_route
node_app -->|"mounts"| node_health_route
node_identify_route -->|"preflight"| node_validate
node_validate -->|"passes"| node_identify_controller
node_identify_controller -->|"delegates"| node_contact_service
node_contact_service -->|"queries"| node_prisma_util
node_prisma_util -->|"reflects"| node_schema
node_schema -.->|"evolves via"| node_migrations
node_prisma_util -->|"connects"| node_db
node_contact_service -->|"throws"| node_errors
node_error_handler -->|"formats"| node_errors
node_app -->|"uses"| node_error_handler
node_app -->|"logs"| node_logger
node_docker -.->|"pairs with"| node_compose
node_compose -->|"provisions"| node_db
node_tests -->|"exercises"| node_identify_route
node_tests -->|"exercises"| node_health_route
node_contact_service -->|"persists to"| node_db
node_health_route -->|"checks"| node_db

click node_server "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/server.js"
click node_app "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/app.js"
click node_identify_route "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/routes/identify.routes.js"
click node_health_route "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/routes/health.routes.js"
click node_validate "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/middleware/validateRequest.js"
click node_identify_controller "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/controllers/identify.controller.js"
click node_contact_service "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/services/contact.service.js"
click node_prisma_util "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/utils/prisma.js"
click node_schema "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/prisma/schema.prisma"
click node_migrations "https://github.com/ayushgupta2408/identity-reconciliation/tree/main/prisma/migrations"
click node_errors "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/utils/errors.js"
click node_error_handler "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/middleware/errorHandler.js"
click node_logger "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/utils/logger.js"
click node_docker "https://github.com/ayushgupta2408/identity-reconciliation/tree/main/Dockerfile"
click node_compose "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/docker-compose.yml"
click node_tests "https://github.com/ayushgupta2408/identity-reconciliation/blob/main/tests/identify.test.js"

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_server,node_app toneBlue
class node_identify_route,node_health_route,node_validate,node_identify_controller toneAmber
class node_contact_service toneMint
class node_prisma_util,node_schema,node_migrations,node_db toneRose
class node_errors,node_error_handler,node_logger,node_docker,node_compose toneIndigo
class node_tests toneTeal
```

## Components

### Runtime

Process entrypoints and app wiring

- [Server](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/server.js): entrypoint · Starts the HTTP listener · `src/server.js`
- [App](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/app.js): express app · Builds the Express application · `src/app.js`

### HTTP API

Request handling surface and middleware

- [Identify route](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/routes/identify.routes.js): route · Maps POST /identify · `src/routes/identify.routes.js`
- [Health route](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/routes/health.routes.js): route · Maps GET /health · `src/routes/health.routes.js`
- [Validate](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/middleware/validateRequest.js): middleware · Enforces request rules and normalization · `src/middleware/validateRequest.js`
- [Identify controller](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/controllers/identify.controller.js): controller · Translates HTTP requests into service calls · `src/controllers/identify.controller.js`

### Identity Logic

Reconciliation and response assembly

- [Contact service](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/services/contact.service.js): domain service · Reconciliation algorithm and cluster merging · `src/services/contact.service.js`

### Persistence

Prisma and PostgreSQL data layer

- [Prisma client](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/utils/prisma.js): db client · Shared Prisma access helper · `src/utils/prisma.js`
- [Schema](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/prisma/schema.prisma): prisma schema · Contact model and relations · `prisma/schema.prisma`
- [Migrations](https://github.com/ayushgupta2408/identity-reconciliation/tree/main/prisma/migrations): db migrations · Schema evolution history · `prisma/migrations`
- **PostgreSQL**: database · Stores contacts and link relationships

### Operations

Packaging, logging, and error handling

- [Errors](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/utils/errors.js): error types · Application error classes · `src/utils/errors.js`
- [Error handler](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/middleware/errorHandler.js): middleware · Standardizes API error responses · `src/middleware/errorHandler.js`
- [Logger](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/src/utils/logger.js): logging · Server-side logging · `src/utils/logger.js`
- [Docker](https://github.com/ayushgupta2408/identity-reconciliation/tree/main/Dockerfile): container config · Container packaging and local orchestration · `Dockerfile`
- [Compose](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/docker-compose.yml): dev stack · App plus PostgreSQL runtime setup · `docker-compose.yml`

### Testing

End-to-end coverage of reconciliation behavior

- [Identity tests](https://github.com/ayushgupta2408/identity-reconciliation/blob/main/tests/identify.test.js): integration tests · End-to-end reconciliation coverage · `tests/identify.test.js`

## Connections

- Server → App: boots · Process startup creates the Express app
- App → Identify route: mounts · Registers identify endpoints
- App → Health route: mounts · Registers health endpoint
- Identify route → Validate: preflight · Validates identify requests before controller logic
- Validate → Identify controller: passes · Only valid requests reach the controller
- Identify controller → Contact service: delegates · Controller hands off reconciliation work
- Contact service → Prisma client: queries · Service uses Prisma for transactional reads and writes
- Prisma client → Schema: reflects · Client is generated from the Prisma schema
- Schema → Migrations: evolves via · Schema changes are captured in migrations
- Prisma client → PostgreSQL: connects · Prisma client talks to PostgreSQL
- Contact service → Errors: throws · Domain and validation failures use app error types
- Error handler → Errors: formats · Error middleware classifies known application errors
- App → Error handler: uses · Express app installs centralized error handling
- App → Logger: logs · Application wiring relies on server logging
- Docker → Compose: pairs with · Container image is used in the compose stack
- Compose → PostgreSQL: provisions · Local stack includes PostgreSQL
- Identity tests → Identify route: exercises · Integration tests hit the public identify API
- Identity tests → Health route: exercises · Tests verify health checking behavior
- Contact service → PostgreSQL: persists to · Reconciliation state is stored in the database
- Health route → PostgreSQL: checks · Health endpoint can verify database connectivity
