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 | ### SQL Views
|
---|
120 |
|
---|
121 | The application uses SQL VIEWs to simplify common queries and improve maintainability:
|
---|
122 |
|
---|
123 | #### `invoice_summary`
|
---|
124 |
|
---|
125 | - **Purpose**: Shows invoice info with client and company names
|
---|
126 | - **What it does**: Joins Invoice, Client, and Tenant tables to display invoice details with client name and company name
|
---|
127 | - **Usage**: Makes it easier to see which client each invoice belongs to without writing complex JOINs
|
---|
128 |
|
---|
129 | ```sql
|
---|
130 | SELECT * FROM invoice_summary WHERE invoice_status = 'PENDING';
|
---|
131 | ```
|
---|
132 |
|
---|
133 | #### `client_totals`
|
---|
134 |
|
---|
135 | - **Purpose**: Shows total amounts for each client
|
---|
136 | - **What it does**: Groups invoices by client and calculates total paid, pending, overdue amounts
|
---|
137 | - **Usage**: Provides a quick overview of each client's financial status
|
---|
138 |
|
---|
139 | ```sql
|
---|
140 | SELECT * FROM client_totals ORDER BY total_invoiced DESC;
|
---|
141 | ```
|
---|
142 |
|
---|
143 | #### API Integration
|
---|
144 |
|
---|
145 | The views are used in API endpoints to provide simplified data access:
|
---|
146 |
|
---|
147 | - `src/app/api/clients/summary/route.ts`: Uses `client_totals` view for client financial summaries
|
---|
148 | - `src/app/api/invoices/totals/route.ts`: Uses `invoice_summary` view for invoice analytics
|
---|
149 |
|
---|
150 | #### Benefits
|
---|
151 |
|
---|
152 | - **Simpler queries**: No need to write complex JOINs every time
|
---|
153 | - **Consistent data**: Same calculation logic everywhere
|
---|
154 | - **Easier to maintain**: Change the view, all queries get updated
|
---|
155 | - **Better performance**: Database can optimize the view
|
---|
156 |
|
---|
157 | The views are implemented in `sql/03_business_views.sql`.
|
---|
158 |
|
---|
159 | ## Troubleshooting
|
---|
160 |
|
---|
161 | If you encounter any issues:
|
---|
162 |
|
---|
163 | - Ensure the SSH connection to the database is active
|
---|
164 | - Verify your Google Cloud credentials path is correct
|
---|
165 | - Check if all environment variables are properly set
|
---|
166 | - Make sure the database is properly seeded if you're setting up for the first time
|
---|
167 |
|
---|
168 | ## Support
|
---|
169 |
|
---|
170 | For additional help or questions, please contact the development team.
|
---|