1 | # Project Setup Guide
|
---|
2 |
|
---|
3 | This guide will help you set up and run the project locally.
|
---|
4 |
|
---|
5 | ## Prerequisites
|
---|
6 |
|
---|
7 | - Node.js (v16.x or v18.x)
|
---|
8 | - Yarn (recommended) or npm
|
---|
9 | - Access to project's database credentials
|
---|
10 | - Google Cloud credentials
|
---|
11 |
|
---|
12 | ## Installation Steps
|
---|
13 |
|
---|
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 |
|
---|
34 | Create a `.env` file in the root directory and set the path to your service-account.json file:
|
---|
35 |
|
---|
36 | ```env
|
---|
37 | GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account.json"
|
---|
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 |
|
---|
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 | The trigger is implemented in `sql/01_invoice_status_trigger.sql`.
|
---|
104 |
|
---|
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 |
|
---|
119 | ## Troubleshooting
|
---|
120 |
|
---|
121 | If you encounter any issues:
|
---|
122 |
|
---|
123 | - Ensure the SSH connection to the database is active
|
---|
124 | - Verify your Google Cloud credentials path is correct
|
---|
125 | - Check if all environment variables are properly set
|
---|
126 | - Make sure the database is properly seeded if you're setting up for the first time
|
---|
127 |
|
---|
128 | ## Support
|
---|
129 |
|
---|
130 | For additional help or questions, please contact the development team.
|
---|