# tejasadhiya/trustfirst architecture

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

- Interactive diagram: https://gitdiagram.com/tejasadhiya/trustfirst
- Repository: https://github.com/tejasadhiya/trustfirst

## Overview

1. **Overall shape**
   - This is a Next.js 16 App Router monorepo-style application for a P2P lending platform.
   - The app is split into three main layers: client UI under `app/` and `components/`, server/API logic under `app/api/`, and integration/model code under `lib/` and `models/`.

2. **Primary user-facing surfaces**
   - Public entry points are `app/page.tsx`, auth pages under `app/auth/signin` and `app/auth/signup`, and the authenticated experience under `app/dashboard/`.
   - The dashboard is further divided into domain areas: agreements, groups, money requests, notifications, profile, creation flows, and testing screens.
   - `app/dashboard/layout.tsx` likely acts as the shell for authenticated navigation and shared dashboard UI.

3. **Domain-centered backend boundaries**
   - The backend is organized around resource-based API routes:
     - `agreements`
     - `groups`
     - `money-requests`
     - `users` / `user`
     - `notifications`
     - `upload`
     - auth sign-in/sign-up
   - Each resource has REST-style endpoints plus specialized actions, such as approving witnesses, extending due dates, generating AI negotiation responses, uploading installment proof, and sending reminders.

4. **Core business objects**
   - The data model layer in `models/` defines the persistent domain entities:
     - `Agreement`
     - `Group`
     - `MoneyRequest`
     - `Notification`
     - `User`
     - `LiveLocation`
   - These models indicate the system tracks lending agreements, group-based requests, repayment evidence, notifications, and optional live location history.

5. **Persistence and server-side data access**
   - `lib/mongodb.ts` is the main database connector, implying MongoDB as the primary data store with Mongoose-style models.
   - Server logic in API routes reads/writes these models directly, so the API layer is the main boundary between the UI and persistence.

6. **Identity and messaging infrastructure**
   - Firebase appears to handle authentication and device messaging:
     - `firebase.ts`
     - `app/api/auth/*`
     - `app/api/user/save-token`
     - `hooks/useFcmToken.ts`
     - `public/firebase-messaging-sw.js`
   - This suggests a split between user identity/session handling and push notification token management for mobile/web notifications.

7. **AI-assisted workflows**
   - AI functionality is split into two distinct paths:
     - `lib/near-ai.ts` and related test routes for NEAR AI–backed negotiation/analysis.
     - `app/actions/generate-analysis.ts` and `app/actions/generate-installment-plan.ts`, which likely power server actions for repayment planning.
   - The repository also includes scripts for testing models and installment-plan generation, showing AI is treated as a first-class backend capability rather than a UI-only feature.

8. **Communication and automation**
   - `lib/email.ts` handles outbound email generation/delivery, and several agreement routes trigger reminders, witness approvals, and other lifecycle emails.
   - Voice/call and reminder automation are exposed through agreement-specific endpoints such as `send-reminder` and `ask-ai-call`, indicating the platform mixes async notifications with interactive escalation flows.

9. **Location and proof capture**
   - `app/api/live-location` and the `LiveLocation` model indicate optional real-time or recent location tracking, likely tied to trust/safety features.
   - Payment proof upload is handled through `app/api/upload` and installment-proof endpoints, with files stored under `public/uploads/installments/...`, so file handling is local filesystem–backed rather than object-storage–backed in this repo state.

10. **UI component structure**
   - Shared UI is built from a large `components/ui/` library, likely shadcn/ui-based primitives.
   - Feature-specific components live alongside it, such as `components/installment-plan-generator.tsx` and `components/dashboard/RecentFriendsModal.tsx`.
   - Hooks under `hooks/` and utility helpers under `app/utils/` and `lib/utils.ts` keep cross-cutting client logic separate from domain screens.

11. **Operational/testing surface**
   - The repo includes many explicit test and diagnostics routes: `test-db`, `test-email`, `test-email-config`, `test-near-ai`, `test-app-url`, and `extract-transaction-id`.
   - There is also a `scripts/` directory with utility scripts for model inspection, FCM testing, trust-score maintenance, and transaction-id extraction, which suggests a fairly operational backend with manual verification tooling.

12. **Important architectural takeaway**
   - The system is not a simple CRUD app; it is a workflow engine for informal lending.
   - The key architecture pattern is: authenticated user actions in the dashboard → domain-specific API routes → MongoDB models + external integrations (Firebase, email, AI, location, and possibly blockchain-related services) → UI updates and notification flows.

Main entry points: [`app/api/agreements/route.ts`](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/agreements/route.ts) (Agreement API), [`app/api/user/[uid]/route.ts`](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/user/[uid]/route.ts) (User API), [`models/Agreement.ts`](https://github.com/tejasadhiya/trustfirst/blob/main/models/Agreement.ts) (Domain models), [`lib/mongodb.ts`](https://github.com/tejasadhiya/trustfirst/blob/main/lib/mongodb.ts) (MongoDB)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App surfaces"]
  node_home["Home<br/>landing page<br/>[page.tsx]"]
  node_signin["Sign in<br/>auth page<br/>[page.tsx]"]
  node_signup["Sign up<br/>auth page<br/>[page.tsx]"]
  node_dashboard["Dashboard<br/>app shell<br/>[layout.tsx]"]
  node_dash_home["Overview<br/>dashboard page<br/>[page.tsx]"]
  node_dash_agreements["Agreements<br/>dashboard area<br/>[page.tsx]"]
  node_dash_groups["Groups<br/>dashboard area<br/>[page.tsx]"]
  node_dash_notifications["Notifications<br/>dashboard page<br/>[page.tsx]"]
end

subgraph group_api["API layer"]
  node_api_agreements["Agreement API<br/>REST resource<br/>[route.ts]"]
  node_api_groups["Group API<br/>REST resource<br/>[route.ts]"]
  node_api_requests["Request API<br/>REST resource<br/>[route.ts]"]
  node_api_user["User API<br/>profile/auth<br/>[route.ts]"]
  node_api_auth["Auth API<br/>identity<br/>[route.ts]"]
  node_api_upload["Upload API<br/>file upload<br/>[route.ts]"]
  node_api_actions["Server actions<br/>workflow actions"]
end

subgraph group_data["Domain state"]
  node_models[("Domain models<br/>mongoose models<br/>[Agreement.ts]")]
  node_mongodb[("MongoDB<br/>db connector<br/>[mongodb.ts]")]
end

subgraph group_integrations["Integrations"]
  node_firebase{{"Firebase<br/>auth/messaging<br/>[firebase.ts]"}}
  node_email{{"Email service<br/>messaging<br/>[email.ts]"}}
  node_near_ai{{"NEAR AI<br/>ai integration<br/>[near-ai.ts]"}}
end

subgraph group_ui["Shared UI"]
  node_ui_lib["UI library<br/>shared components<br/>[button.tsx]"]
  node_feature_ui["Feature widgets<br/>feature components"]
  node_hooks_utils["Hooks and utils<br/>client helpers<br/>[useFcmToken.ts]"]
end

subgraph group_ops["Diagnostics"]
  node_diagnostics["Diagnostics<br/>test routes and scripts<br/>[route.ts]"]
end

node_home -->|"auth path"| node_signin
node_home -->|"register"| node_signup
node_signin -->|"authenticate"| node_api_auth
node_signup -->|"register"| node_api_auth
node_dashboard -->|"shell"| node_dash_home
node_dashboard -->|"nav"| node_dash_agreements
node_dashboard -->|"nav"| node_dash_groups
node_dashboard -->|"nav"| node_dash_notifications
node_dash_agreements -->|"loads data"| node_api_agreements
node_dash_groups -->|"loads data"| node_api_groups
node_dash_groups -->|"creates requests"| node_api_requests
node_dash_notifications -->|"loads user state"| node_api_user
node_api_agreements -->|"persists"| node_models
node_api_groups -->|"persists"| node_models
node_api_requests -->|"persists"| node_models
node_api_user -->|"persists"| node_models
node_api_agreements -->|"db access"| node_mongodb
node_api_groups -->|"db access"| node_mongodb
node_api_requests -->|"db access"| node_mongodb
node_api_user -->|"db access"| node_mongodb
node_api_auth -->|"identity"| node_firebase
node_api_user -->|"push token"| node_firebase
node_api_agreements -->|"negotiation"| node_near_ai
node_api_actions -->|"generate"| node_near_ai
node_api_agreements -->|"reminders"| node_email
node_api_upload -->|"attach proof"| node_models
node_api_user -->|"token flow"| node_hooks_utils
node_dash_agreements -->|"uses widgets"| node_feature_ui
node_dash_home -->|"renders with"| node_ui_lib
node_diagnostics -.->|"checks db"| node_mongodb
node_diagnostics -.->|"checks ai"| node_near_ai

click node_home "https://github.com/tejasadhiya/trustfirst/blob/main/app/page.tsx"
click node_signin "https://github.com/tejasadhiya/trustfirst/blob/main/app/auth/signin/page.tsx"
click node_signup "https://github.com/tejasadhiya/trustfirst/blob/main/app/auth/signup/page.tsx"
click node_dashboard "https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/layout.tsx"
click node_dash_home "https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/page.tsx"
click node_dash_agreements "https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/agreement/[id]/page.tsx"
click node_dash_groups "https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/groups/page.tsx"
click node_dash_notifications "https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/notifications/page.tsx"
click node_api_agreements "https://github.com/tejasadhiya/trustfirst/blob/main/app/api/agreements/route.ts"
click node_api_groups "https://github.com/tejasadhiya/trustfirst/blob/main/app/api/groups/route.ts"
click node_api_requests "https://github.com/tejasadhiya/trustfirst/blob/main/app/api/money-requests/route.ts"
click node_api_user "https://github.com/tejasadhiya/trustfirst/blob/main/app/api/user/[uid]/route.ts"
click node_api_auth "https://github.com/tejasadhiya/trustfirst/blob/main/app/api/auth/signin/route.ts"
click node_api_upload "https://github.com/tejasadhiya/trustfirst/blob/main/app/api/upload/route.ts"
click node_api_actions "https://github.com/tejasadhiya/trustfirst/blob/main/app/actions/generate-analysis.ts"
click node_models "https://github.com/tejasadhiya/trustfirst/blob/main/models/Agreement.ts"
click node_mongodb "https://github.com/tejasadhiya/trustfirst/blob/main/lib/mongodb.ts"
click node_firebase "https://github.com/tejasadhiya/trustfirst/blob/main/firebase.ts"
click node_email "https://github.com/tejasadhiya/trustfirst/blob/main/lib/email.ts"
click node_near_ai "https://github.com/tejasadhiya/trustfirst/blob/main/lib/near-ai.ts"
click node_ui_lib "https://github.com/tejasadhiya/trustfirst/blob/main/components/ui/button.tsx"
click node_feature_ui "https://github.com/tejasadhiya/trustfirst/blob/main/components/installment-plan-generator.tsx"
click node_hooks_utils "https://github.com/tejasadhiya/trustfirst/blob/main/hooks/useFcmToken.ts"
click node_diagnostics "https://github.com/tejasadhiya/trustfirst/blob/main/app/api/test-db/route.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_home,node_signin,node_signup,node_dashboard,node_dash_home,node_dash_agreements,node_dash_groups,node_dash_notifications toneBlue
class node_api_agreements,node_api_groups,node_api_requests,node_api_user,node_api_auth,node_api_upload,node_api_actions toneAmber
class node_models,node_mongodb toneMint
class node_firebase,node_email,node_near_ai toneRose
class node_ui_lib,node_feature_ui,node_hooks_utils toneIndigo
class node_diagnostics toneTeal
```

## Components

### App surfaces

Public, auth, and dashboard entry points in the Next.js App Router.

- [Home](https://github.com/tejasadhiya/trustfirst/blob/main/app/page.tsx): landing page · Public entry point for the product. · `app/page.tsx`
- [Sign in](https://github.com/tejasadhiya/trustfirst/blob/main/app/auth/signin/page.tsx): auth page · Authentication entry screen. · `app/auth/signin/page.tsx`
- [Sign up](https://github.com/tejasadhiya/trustfirst/blob/main/app/auth/signup/page.tsx): auth page · Account creation entry screen. · `app/auth/signup/page.tsx`
- [Dashboard](https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/layout.tsx): app shell · Authenticated workspace and shared navigation. · `app/dashboard/layout.tsx`
- [Overview](https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/page.tsx): dashboard page · Main authenticated landing page. · `app/dashboard/page.tsx`
- [Agreements](https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/agreement/[id]/page.tsx): dashboard area · Agreement detail, negotiation, and proof upload flows. · `app/dashboard/agreement/[id]/page.tsx`
- [Groups](https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/groups/page.tsx): dashboard area · Group browsing, creation, and group request flows. · `app/dashboard/groups/page.tsx`
- [Notifications](https://github.com/tejasadhiya/trustfirst/blob/main/app/dashboard/notifications/page.tsx): dashboard page · User notification inbox. · `app/dashboard/notifications/page.tsx`

### API layer

Resource routes and server actions that implement domain workflows.

- [Agreement API](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/agreements/route.ts): REST resource · Agreement lifecycle, reminders, negotiation, and proof handling. · `app/api/agreements/route.ts`
- [Group API](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/groups/route.ts): REST resource · Group creation and lookup endpoints. · `app/api/groups/route.ts`
- [Request API](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/money-requests/route.ts): REST resource · Money request creation and contribution endpoints. · `app/api/money-requests/route.ts`
- [User API](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/user/[uid]/route.ts): profile/auth · Profile lookup, verification, and device token management. · `app/api/user/[uid]/route.ts`
- [Auth API](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/auth/signin/route.ts): identity · Sign-in and sign-up routes backed by Firebase. · `app/api/auth/signin/route.ts`
- [Upload API](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/upload/route.ts): file upload · File ingestion for installment proof and related artifacts. · `app/api/upload/route.ts`
- [Server actions](https://github.com/tejasadhiya/trustfirst/blob/main/app/actions/generate-analysis.ts): workflow actions · AI-assisted generation of analysis and installment plans. · `app/actions/generate-analysis.ts`

### Domain state

Persistent models and database access.

- [Domain models](https://github.com/tejasadhiya/trustfirst/blob/main/models/Agreement.ts): mongoose models · Persistent entities for agreements, groups, requests, users, notifications, and live location. · `models/Agreement.ts`
- [MongoDB](https://github.com/tejasadhiya/trustfirst/blob/main/lib/mongodb.ts): db connector · Primary database connection and persistence boundary. · `lib/mongodb.ts`

### Integrations

External services and messaging infrastructure.

- [Firebase](https://github.com/tejasadhiya/trustfirst/blob/main/firebase.ts): auth/messaging · Identity, messaging, and service worker-backed push notifications. · `firebase.ts`
- [Email service](https://github.com/tejasadhiya/trustfirst/blob/main/lib/email.ts): messaging · Outbound reminder and lifecycle email support. · `lib/email.ts`
- [NEAR AI](https://github.com/tejasadhiya/trustfirst/blob/main/lib/near-ai.ts): ai integration · Negotiation and analysis assistance for lending workflows. · `lib/near-ai.ts`

### Shared UI

Reusable components, hooks, and feature widgets.

- [UI library](https://github.com/tejasadhiya/trustfirst/blob/main/components/ui/button.tsx): shared components · Reusable primitives and design-system components. · `components/ui/button.tsx`
- [Feature widgets](https://github.com/tejasadhiya/trustfirst/blob/main/components/installment-plan-generator.tsx): feature components · Dashboard-specific helpers such as installment plan generation and friend selection. · `components/installment-plan-generator.tsx`
- [Hooks and utils](https://github.com/tejasadhiya/trustfirst/blob/main/hooks/useFcmToken.ts): client helpers · Cross-cutting hooks and utility helpers used across the app. · `hooks/useFcmToken.ts`

### Diagnostics

Test routes and scripts used for verification and maintenance.

- [Diagnostics](https://github.com/tejasadhiya/trustfirst/blob/main/app/api/test-db/route.ts): test routes and scripts · Operational verification routes and maintenance scripts. · `app/api/test-db/route.ts`

## Connections

- Home → Sign in: auth path · Public users move into authentication.
- Home → Sign up: register · Public users can create an account.
- Sign in → Auth API: authenticate · Credential and session handling is delegated to auth routes.
- Sign up → Auth API: register · Account creation flows through auth endpoints.
- Dashboard → Overview: shell · Authenticated pages render inside the dashboard layout.
- Dashboard → Agreements: nav · The dashboard hosts the agreement workspace.
- Dashboard → Groups: nav · The dashboard hosts group workflows.
- Dashboard → Notifications: nav · The dashboard exposes notifications.
- Agreements → Agreement API: loads data · Agreement pages read and mutate agreement state through the API.
- Groups → Group API: loads data · Group pages use the group API.
- Groups → Request API: creates requests · Group request flows submit money requests and contributions.
- Notifications → User API: loads user state · Notifications and profile-related state come from user endpoints.
- Agreement API → Domain models: persists · Agreement workflow routes read and write domain models.
- Group API → Domain models: persists · Group routes operate on persisted domain entities.
- Request API → Domain models: persists · Money request routes update request-related models.
- User API → Domain models: persists · User verification and token storage use persisted user data.
- Agreement API → MongoDB: db access · Server routes connect through the MongoDB layer.
- Group API → MongoDB: db access · Group endpoints use the shared database connector.
- Request API → MongoDB: db access · Request endpoints use the shared database connector.
- User API → MongoDB: db access · User endpoints use the shared database connector.
- Auth API → Firebase: identity · Authentication integrates with Firebase.
- User API → Firebase: push token · Device token saving and verification rely on Firebase services.
- Agreement API → NEAR AI: negotiation · Agreement negotiation and AI call flows use NEAR AI.
- Server actions → NEAR AI: generate · Server actions produce analysis and installment plans with AI.
- Agreement API → Email service: reminders · Agreement lifecycle actions trigger email notifications.
- Upload API → Domain models: attach proof · Uploads are associated with agreement/installment records.
- User API → Hooks and utils: token flow · Client-side token acquisition supports user messaging setup.
- Agreements → Feature widgets: uses widgets · Agreement screens compose specialized feature components.
- Overview → UI library: renders with · Pages compose the shared UI system.
- Diagnostics → MongoDB: checks db · Test routes and scripts validate persistence and integrations.
- Diagnostics → NEAR AI: checks ai · Diagnostics cover AI-backed workflow behavior.
