[32e9876] | 1 | # Project Setup Guide
|
---|
[5d6f37a] | 2 |
|
---|
[32e9876] | 3 | This guide will help you set up and run the project locally.
|
---|
[5d6f37a] | 4 |
|
---|
[32e9876] | 5 | ## Prerequisites
|
---|
[5d6f37a] | 6 |
|
---|
[32e9876] | 7 | - Node.js (v16.x or v18.x)
|
---|
| 8 | - Yarn (recommended) or npm
|
---|
| 9 | - Access to project's database credentials
|
---|
| 10 | - Google Cloud credentials
|
---|
[5d6f37a] | 11 |
|
---|
[32e9876] | 12 | ## Installation Steps
|
---|
[5d6f37a] | 13 |
|
---|
[32e9876] | 14 | 1. **Install Dependencies**
|
---|
| 15 |
|
---|
| 16 | ```bash
|
---|
| 17 | yarn install
|
---|
| 18 | # or using npm
|
---|
| 19 | npm install --legacy-peer-deps
|
---|
| 20 | ```
|
---|
| 21 |
|
---|
| 22 | 2. **Database Connection**
|
---|
| 23 |
|
---|
| 24 | Open a terminal and run the following SSH command to establish a connection to the database:
|
---|
| 25 |
|
---|
| 26 | ```bash
|
---|
| 27 | ssh -L 5432:localhost:5432 t_agency_os@194.149.135.130 -N
|
---|
| 28 | ```
|
---|
| 29 |
|
---|
| 30 | Keep this terminal window open while working with the application.
|
---|
| 31 |
|
---|
| 32 | 3. **Environment Setup**
|
---|
| 33 |
|
---|
[dc3406b] | 34 | Create a `.env` file in the root directory and set the path to your service-account.json file:
|
---|
[32e9876] | 35 |
|
---|
| 36 | ```env
|
---|
[dc3406b] | 37 | GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account.json"
|
---|
[32e9876] | 38 | ```
|
---|
| 39 |
|
---|
| 40 | 4. **Generate Prisma Client**
|
---|
| 41 |
|
---|
| 42 | ```bash
|
---|
| 43 | yarn prisma:generate
|
---|
| 44 | ```
|
---|
| 45 |
|
---|
| 46 | 5. **Seed Database (First-time setup)**
|
---|
| 47 |
|
---|
| 48 | If you're setting up the project for the first time and the database is empty, run:
|
---|
| 49 |
|
---|
| 50 | ```bash
|
---|
| 51 | yarn db:seed
|
---|
| 52 | ```
|
---|
| 53 |
|
---|
| 54 | This will create an initial tenant and user in the database.
|
---|
| 55 |
|
---|
| 56 | 6. **Start Development Server**
|
---|
| 57 | ```bash
|
---|
| 58 | yarn dev
|
---|
| 59 | ```
|
---|
| 60 | The application should now be running on `http://localhost:3000`
|
---|
| 61 |
|
---|
| 62 | ## Authentication
|
---|
| 63 |
|
---|
| 64 | The application uses Firebase Authentication. You can log in with the following default credentials:
|
---|
| 65 |
|
---|
| 66 | - Email: naum@mvpmasters.com
|
---|
| 67 | - Password: BnP2025~
|
---|
| 68 |
|
---|
| 69 | ## Available Scripts
|
---|
| 70 |
|
---|
| 71 | - `yarn dev` - Start development server
|
---|
| 72 | - `yarn prisma:generate` - Generate Prisma client
|
---|
| 73 | - `yarn build` - Build for production
|
---|
| 74 | - `yarn start` - Start production server
|
---|
| 75 | - `yarn db:seed` - Seed the database with initial data
|
---|
| 76 |
|
---|
[38387ce] | 77 | ## Database Features
|
---|
| 78 |
|
---|
| 79 | ### Transactions
|
---|
| 80 |
|
---|
| 81 | The application uses database transactions in several key operations:
|
---|
| 82 |
|
---|
| 83 | - Invoice creation: When creating a new invoice, a transaction ensures that both the invoice and its items are created atomically
|
---|
| 84 | - Status updates: Invoice status changes are handled within transactions to maintain data consistency
|
---|
| 85 | - Client updates: When updating client information, related operations are wrapped in transactions
|
---|
| 86 |
|
---|
| 87 | Transactions are implemented in the following files:
|
---|
| 88 |
|
---|
| 89 | - `src/app/api/invoices/[id]/route.ts`: For updating and deleting invoices
|
---|
| 90 | - `src/app/api/invoices/route.ts`: For creating new invoices
|
---|
| 91 |
|
---|
| 92 | ### Triggers
|
---|
| 93 |
|
---|
| 94 | The system implements a sophisticated trigger system for invoice management:
|
---|
| 95 |
|
---|
| 96 | - `invoice_status_update_trigger`: Automatically fires when an invoice status changes
|
---|
| 97 | - Logs status changes in `InvoiceStatusHistory`
|
---|
| 98 | - Creates notifications in `InvoiceNotifications`
|
---|
| 99 | - Updates client status based on payment history
|
---|
| 100 | - Automatically extends due dates when needed
|
---|
| 101 | - Calculates and updates financial metrics
|
---|
| 102 |
|
---|
[658eaee] | 103 | The trigger is implemented in `sql/01_invoice_status_trigger.sql`.
|
---|
| 104 |
|
---|
[38387ce] | 105 | ### Indexes
|
---|
| 106 |
|
---|
| 107 | Several optimized indexes are implemented for better query performance:
|
---|
| 108 |
|
---|
| 109 | - Client email lookups:
|
---|
| 110 | - `idx_client_email`: Basic email search optimization
|
---|
| 111 | - `idx_client_email_tenant`: Composite index for tenant-specific email searches
|
---|
| 112 | - `idx_client_email_lower`: Case-insensitive email searches
|
---|
| 113 | - Invoice management:
|
---|
| 114 | - `idx_invoice_status_history_invoice_id`: Optimizes status history lookups
|
---|
| 115 | - `idx_invoice_status_history_changed_at`: Improves date-based queries
|
---|
| 116 | - `idx_invoice_notifications_invoice_id`: Speeds up notification retrieval
|
---|
| 117 | - `idx_invoice_notifications_processed`: Partial index for unprocessed notifications
|
---|
| 118 |
|
---|
[d7c72f6] | 119 | ### SQL Views
|
---|
| 120 |
|
---|
[d808bc2] | 121 | The application implements sophisticated SQL VIEWs for business intelligence and analytics:
|
---|
[d7c72f6] | 122 |
|
---|
[d808bc2] | 123 | #### `v_invoice_analytics`
|
---|
[d7c72f6] | 124 |
|
---|
[d808bc2] | 125 | - **Purpose**: Comprehensive invoice analytics with calculated business metrics
|
---|
| 126 | - **Features**:
|
---|
| 127 | - Combines invoice, client, and tenant data
|
---|
| 128 | - Calculates days overdue, payment status, value categories
|
---|
| 129 | - Provides age categorization and risk indicators
|
---|
| 130 | - Real-time business intelligence metrics
|
---|
[d7c72f6] | 131 |
|
---|
[d808bc2] | 132 | #### `v_client_financial_summary`
|
---|
[d7c72f6] | 133 |
|
---|
[d808bc2] | 134 | - **Purpose**: Complete financial overview for each client
|
---|
| 135 | - **Features**:
|
---|
| 136 | - Total invoiced, paid, outstanding, and overdue amounts
|
---|
| 137 | - Payment behavior analysis and risk assessment
|
---|
| 138 | - Client performance metrics and historical data
|
---|
| 139 | - Automated risk level calculation
|
---|
[d7c72f6] | 140 |
|
---|
[d808bc2] | 141 | #### Advanced Features
|
---|
[d7c72f6] | 142 |
|
---|
[d808bc2] | 143 | - **Complex business logic**: Multi-level conditional calculations
|
---|
| 144 | - **Performance optimization**: Strategic indexing for view performance
|
---|
| 145 | - **Data aggregation**: Multi-level aggregation for different time periods
|
---|
| 146 | - **Risk assessment**: Automated client risk evaluation algorithms
|
---|
[d7c72f6] | 147 |
|
---|
| 148 | #### API Integration
|
---|
| 149 |
|
---|
[d808bc2] | 150 | The views are integrated into API endpoints for comprehensive data access:
|
---|
[d7c72f6] | 151 |
|
---|
[d808bc2] | 152 | - `src/app/api/clients/summary/route.ts`: Uses `v_client_financial_summary` for detailed client analytics
|
---|
| 153 | - `src/app/api/invoices/totals/route.ts`: Uses `v_invoice_analytics` for invoice analytics
|
---|
[d7c72f6] | 154 |
|
---|
| 155 | #### Benefits
|
---|
| 156 |
|
---|
[d808bc2] | 157 | - **Business Intelligence**: Pre-computed complex business metrics
|
---|
| 158 | - **Performance**: Optimized queries with strategic indexing
|
---|
| 159 | - **Maintainability**: Centralized business logic in database layer
|
---|
| 160 | - **Scalability**: Supports complex reporting requirements
|
---|
[d7c72f6] | 161 |
|
---|
[d808bc2] | 162 | The views are implemented in `sql/03_business_views.sql` with comprehensive indexing for optimal performance.
|
---|
[d7c72f6] | 163 |
|
---|
[32e9876] | 164 | ## Troubleshooting
|
---|
| 165 |
|
---|
| 166 | If you encounter any issues:
|
---|
| 167 |
|
---|
| 168 | - Ensure the SSH connection to the database is active
|
---|
| 169 | - Verify your Google Cloud credentials path is correct
|
---|
| 170 | - Check if all environment variables are properly set
|
---|
| 171 | - Make sure the database is properly seeded if you're setting up for the first time
|
---|
| 172 |
|
---|
| 173 | ## Support
|
---|
| 174 |
|
---|
| 175 | For additional help or questions, please contact the development team.
|
---|