How ayushgupta2408/identity-reconciliation works
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: app.js App · contact.service.js Contact service · prisma.js Prisma client · identify.routes.js Identify route
Components 17
Runtime
Process entrypoints and app wiring
HTTP API
Request handling surface and middleware
- Identify route · Maps POST /identify
src/routes/identify.routes.js - Health route · Maps GET /health
src/routes/health.routes.js - Validate · Enforces request rules and normalization
src/middleware/validateRequest.js - Identify controller · Translates HTTP requests into service calls
src/controllers/identify.controller.js
Identity Logic
Reconciliation and response assembly
- Contact service · Reconciliation algorithm and cluster merging
src/services/contact.service.js
Persistence
Prisma and PostgreSQL data layer
- Prisma client · Shared Prisma access helper
src/utils/prisma.js - Schema · Contact model and relations
prisma/schema.prisma - Migrations · Schema evolution history
prisma/migrations - PostgreSQL · Stores contacts and link relationships
Operations
Packaging, logging, and error handling
- Errors · Application error classes
src/utils/errors.js - Error handler · Standardizes API error responses
src/middleware/errorHandler.js - Logger · Server-side logging
src/utils/logger.js - Docker · Container packaging and local orchestration
Dockerfile - Compose · App plus PostgreSQL runtime setup
docker-compose.yml
Testing
End-to-end coverage of reconciliation behavior
- Identity tests · End-to-end reconciliation coverage
tests/identify.test.js
Connections 20
- 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