[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 |
|
---|
| 103 | ### Indexes
|
---|
| 104 |
|
---|
| 105 | Several optimized indexes are implemented for better query performance:
|
---|
| 106 |
|
---|
| 107 | - Client email lookups:
|
---|
| 108 | - `idx_client_email`: Basic email search optimization
|
---|
| 109 | - `idx_client_email_tenant`: Composite index for tenant-specific email searches
|
---|
| 110 | - `idx_client_email_lower`: Case-insensitive email searches
|
---|
| 111 | - Invoice management:
|
---|
| 112 | - `idx_invoice_status_history_invoice_id`: Optimizes status history lookups
|
---|
| 113 | - `idx_invoice_status_history_changed_at`: Improves date-based queries
|
---|
| 114 | - `idx_invoice_notifications_invoice_id`: Speeds up notification retrieval
|
---|
| 115 | - `idx_invoice_notifications_processed`: Partial index for unprocessed notifications
|
---|
| 116 |
|
---|
[32e9876] | 117 | ## Troubleshooting
|
---|
| 118 |
|
---|
| 119 | If you encounter any issues:
|
---|
| 120 |
|
---|
| 121 | - Ensure the SSH connection to the database is active
|
---|
| 122 | - Verify your Google Cloud credentials path is correct
|
---|
| 123 | - Check if all environment variables are properly set
|
---|
| 124 | - Make sure the database is properly seeded if you're setting up for the first time
|
---|
| 125 |
|
---|
| 126 | ## Support
|
---|
| 127 |
|
---|
| 128 | For additional help or questions, please contact the development team.
|
---|