Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
344 lines (289 loc) · 10.2 KB
/
Copy pathapi.js
File metadata and controls
344 lines (289 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import bodyParser from "body-parser";
import express from "express";
import pg from "pg";
import cors from "cors"
import env from "dotenv"
const app = express();
const port = 4000;
env.config();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
const { Client } = pg
app.use(cors())
const db=new Client({
user: process.env.DATABASE_USER,
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_NAME, // Fixed here
password: process.env.DATABASE_PWD,
port: Number(process.env.DATABASE_PORT) || 5432,
ssl: {
rejectUnauthorized: false, // ✅ Required for cloud PostgreSQL (e.g., Render)
},
});
db.connect()
.then(() => console.log("✅ Connected to PostgreSQL successfully"))
.catch(err => console.error("❌ PostgreSQL connection error:", err));
import ExcelJS from 'exceljs';
app.get('/export-excel', async (req, res) => {
try {
const {id,startDate,endDate}=req.query;
const id1=parseInt(id);
var query='SELECT id, category, amount, date FROM expenses WHERE 1=1';
const params=[];
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Data Export');
if(id1){
query+=` AND user_id=$1`
params.push(id1)
}
if(startDate && endDate){
query+=` AND date>=$2 AND date<$3`;
params.push(startDate)
params.push(endDate)
}
// Add headers
worksheet.columns = [
{ header: 'ID', key: 'id', width: 10 },
{ header: 'Category', key: 'category', width: 20 },
{ header: 'Amount', key: 'amount', width: 15 },
{ header: 'Date', key: 'date', width: 15 },
];
// Fetch data from your database
const result = await db.query(query,params);
result.rows.forEach(row => {
worksheet.addRow({
id: row.id,
category: row.category,
amount: row.amount,
date: row.date.toISOString().split('T')[0],
});
});
// Set response headers for download
res.setHeader(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
);
res.setHeader('Content-Disposition', 'attachment; filename=expenses.xlsx');
await workbook.xlsx.write(res);
res.end();
} catch (err) {
console.error('Error exporting data:', err.message);
res.status(500).send('Failed to export data');
}
});
app.get('/export-excel-income', async (req, res) => {
try {
const {id,startDate,endDate}=req.query;
const id1=parseInt(id);
const params=[]
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Data Export');
var query='SELECT id, category, amount, date FROM incomes WHERE 1=1'
if(id1){
query+=` AND user_id=$1`
params.push(id1)
}
if(startDate && endDate){
query+=` AND date>=$2 AND date<$3`;
params.push(startDate)
params.push(endDate)
}
// Add headers
worksheet.columns = [
{ header: 'ID', key: 'id', width: 10 },
{ header: 'Category', key: 'category', width: 20 },
{ header: 'Amount', key: 'amount', width: 15 },
{ header: 'Date', key: 'date', width: 15 },
];
// Fetch data from your database
const result = await db.query(query,params);
result.rows.forEach(row => {
worksheet.addRow({
id: row.id,
category: row.category,
amount: row.amount,
date: row.date.toISOString().split('T')[0],
});
});
// Set response headers for download
res.setHeader(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
);
res.setHeader('Content-Disposition', 'attachment; filename=incomes.xlsx');
await workbook.xlsx.write(res);
res.end();
} catch (err) {
console.error('Error exporting data:', err.message);
res.status(500).send('Failed to export data');
}
});
app.delete("/delete/income/:id",async(req,res)=>{
try{
const id=parseInt(req.params.id);
const result= await db.query("DELETE FROM incomes WHERE id=$1",[id])
if (result.rowCount > 0) {
res.status(200).json({ success: true, message: "Item deleted successfully!" });
} else {
res.status(404).json({ success: false, message: "Item not found!" });
};
}catch(err){
console.error(err.message);
res.status(500).json({ success: false, message: "Server error!" });
}
})
app.delete("/delete/expense/:id",async(req,res)=>{
try{
const id=parseInt(req.params.id);
const result= await db.query("DELETE FROM expenses WHERE id=$1",[id])
if (result.rowCount > 0) {
res.status(200).json({ success: true, message: "Item deleted successfully!" });
} else {
res.status(404).json({ success: false, message: "Item not found!" });
};
}catch(err){
console.error(err.message);
res.status(500).json({ success: false, message: "Server error!" });
}
})
app.get("/stats1", async (req, res) => {
try {
const {id,startDate,endDate}=req.query;
const id1=parseInt(id);
var query1="SELECT SUM(amount) AS total_food FROM expenses WHERE category = 'Food'";
var query2="SELECT SUM(amount) AS total_transport FROM expenses WHERE category = 'Transport'";
var query3="SELECT SUM(amount) AS total_rent FROM expenses WHERE category = 'Rent'";
var query4="SELECT SUM(amount) AS total_enter FROM expenses WHERE category = 'Entertainment'"
var query5="SELECT SUM(amount) AS total_others FROM expenses WHERE category ='Others'";
const params=[]
if(id1){
query1+=` AND user_id=$1`
query2+=` AND user_id=$1`
query3+=` AND user_id=$1`
query4+=` AND user_id=$1`
query5+=` AND user_id=$1`
params.push(id1)
}
if(startDate && endDate){
query1+=` AND date>=$2 AND date<$3`;
query2+=` AND date>=$2 AND date<$3`;
query3+=` AND date>=$2 AND date<$3`;
query4+=` AND date>=$2 AND date<$3`;
query5+=` AND date>=$2 AND date<$3`;
params.push(startDate)
params.push(endDate)
}
const food = await db.query(
query1,params
);
const transport = await db.query(
query2,params
);;
const rent = await db.query(
query3,params
);
const entertainment = await db.query(
query4,params
);
const other = await db.query(
query5,params
);
res.json({
Entertain: entertainment.rows[0]?.total_enter || 0,
Rents: rent.rows[0]?.total_rent || 0,
Transports: transport.rows[0]?.total_transport || 0,
Foods: food.rows[0]?.total_food || 0,
Others: other.rows[0]?.total_others || 0,
});
} catch (err) {
console.error("Error in /stats1 route:", err.message);
res.status(500).send("Error fetching chart data");
}
});
app.get("/stats2", async (req, res) => {
try {
const {id,startDate,endDate}=req.query;
const id1=parseInt(id);
const params=[]
var query1='SELECT SUM(amount) AS total_expense FROM expenses WHERE 1=1 '
var query2='SELECT SUM(amount) AS total_income FROM incomes WHERE 1=1'
if(id1){
query1+=` AND user_id=$1`
query2+=` AND user_id=$1`
params.push(id1)
}
if(startDate && endDate){
query1+=` AND date>=$2 AND date<$3`;
query2+=` AND date>=$2 AND date<$3`;
params.push(startDate)
params.push(endDate)
}
const expense = await db.query(
query1,params
);
const income = await db.query(
query2,params
);
res.json({
Expense: expense.rows[0]?.total_expense || 0,
Income: income.rows[0]?.total_income || 0,
});
} catch (err) {
console.error("Error in /stats1 route:", err.message);
res.status(500).send("Error fetching chart data");
}
});
app.get('/filter', async (req, res) => {
try {
const { id,option,startDate, endDate, category, minAmount, maxAmount } = req.query;
const id1=parseInt(id);
var table;
if(option=="expense"){
table="expenses";
}else{
table="incomes"
}
let query = 'SELECT * FROM '+table +' WHERE 1=1';
const params = [];
let paramCount = 0;
if (id1) {
paramCount++;
query += ` AND user_id = $${paramCount}`;
params.push(id1);
}
if (startDate) {
paramCount++;
query += ` AND date >= $${paramCount}`;
params.push(startDate);
}
if (endDate) {
paramCount++;
query += ` AND date <= $${paramCount}`;
params.push(endDate);
}
if (category && category!="All") {
paramCount++;
query += ` AND category = $${paramCount}`;
params.push(category);
}
if (minAmount) {
paramCount++;
query += ` AND amount >= $${paramCount}`;
params.push(minAmount);
}
if (maxAmount) {
paramCount++;
query += ` AND amount <= $${paramCount}`;
params.push(maxAmount);
}
const result = await db.query(query, params);
console.log(result.rows)
res.json(result.rows);
} catch (err) {
console.error(err.message);
res.status(500).json({ message: 'Error filtering data' });
}
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
})