How tejasadhiya/trustfirst works
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: agreements/route.ts Agreement API · [uid]/route.ts User API · Agreement.ts Domain models · mongodb.ts MongoDB
Components 24
App surfaces
Public, auth, and dashboard entry points in the Next.js App Router.
- Home · Public entry point for the product.
app/page.tsx - Sign in · Authentication entry screen.
app/auth/signin/page.tsx - Sign up · Account creation entry screen.
app/auth/signup/page.tsx - Dashboard · Authenticated workspace and shared navigation.
app/dashboard/layout.tsx - Overview · Main authenticated landing page.
app/dashboard/page.tsx - Agreements · Agreement detail, negotiation, and proof upload flows.
app/dashboard/agreement/[id]/page.tsx - Groups · Group browsing, creation, and group request flows.
app/dashboard/groups/page.tsx - Notifications · User notification inbox.
app/dashboard/notifications/page.tsx
API layer
Resource routes and server actions that implement domain workflows.
- Agreement API · Agreement lifecycle, reminders, negotiation, and proof handling.
app/api/agreements/route.ts - Group API · Group creation and lookup endpoints.
app/api/groups/route.ts - Request API · Money request creation and contribution endpoints.
app/api/money-requests/route.ts - User API · Profile lookup, verification, and device token management.
app/api/user/[uid]/route.ts - Auth API · Sign-in and sign-up routes backed by Firebase.
app/api/auth/signin/route.ts - Upload API · File ingestion for installment proof and related artifacts.
app/api/upload/route.ts - Server actions · AI-assisted generation of analysis and installment plans.
app/actions/generate-analysis.ts
Domain state
Persistent models and database access.
- Domain models · Persistent entities for agreements, groups, requests, users, notifications, and live location.
models/Agreement.ts - MongoDB · Primary database connection and persistence boundary.
lib/mongodb.ts
Integrations
External services and messaging infrastructure.
- Firebase · Identity, messaging, and service worker-backed push notifications.
firebase.ts - Email service · Outbound reminder and lifecycle email support.
lib/email.ts - NEAR AI · Negotiation and analysis assistance for lending workflows.
lib/near-ai.ts
Shared UI
Reusable components, hooks, and feature widgets.
- UI library · Reusable primitives and design-system components.
components/ui/button.tsx - Feature widgets · Dashboard-specific helpers such as installment plan generation and friend selection.
components/installment-plan-generator.tsx - Hooks and utils · Cross-cutting hooks and utility helpers used across the app.
hooks/useFcmToken.ts
Diagnostics
Test routes and scripts used for verification and maintenance.
- Diagnostics · Operational verification routes and maintenance scripts.
app/api/test-db/route.ts
Connections 31
- 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.