-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
158 lines (134 loc) · 5.15 KB
/
Copy pathserver.js
File metadata and controls
158 lines (134 loc) · 5.15 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
const express = require('express');
const mysql = require('mysql2/promise');
const bcrypt = require('bcrypt');
const cors = require('cors');
const path = require('path');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname)));
const pool = mysql.createPool({
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || '',
database: process.env.DB_NAME || 'soundpro',
waitForConnections: true,
connectionLimit: 10,
});
app.post('/api/login', async (req, res) => {
const { email, password } = req.body;
if (!email || !password) {
return res.json({ success: false, message: 'Email and password are required' });
}
try {
const [rows] = await pool.execute(
'SELECT id, name, email, password FROM users WHERE email = ?',
[email]
);
if (rows.length === 0) {
return res.json({ success: false, message: 'Invalid email or password' });
}
const user = rows[0];
const match = await bcrypt.compare(password, user.password);
if (!match) {
return res.json({ success: false, message: 'Invalid email or password' });
}
res.json({
success: true,
message: 'Login successful',
name: user.name,
email: user.email,
});
} catch (err) {
console.error('Login error:', err);
res.status(500).json({ success: false, message: 'Server error' });
}
});
app.listen(PORT, () => {
console.log(`SoundPro server running at http://localhost:${PORT}`);
});
// Add new customer
app.post('/api/clients', async (req, res) => {
const { customer_name, customer_phone, customer_address, company_name, user_email } = req.body;
if (!customer_name || !customer_phone) {
return res.status(400).json({ success: false, message: 'Name and contact number are required' });
}
try {
const [result] = await pool.execute(
`INSERT INTO clients (id, customer_name, customer_phone, customer_address, company_name, user_email)
VALUES (UUID(), ?, ?, ?, ?, ?)`,
[customer_name, customer_phone, customer_address || null, company_name || null, user_email || 'admin@soundpro.com']
);
const [rows] = await pool.execute('SELECT * FROM clients WHERE customer_phone = ? ORDER BY created_at DESC LIMIT 1', [customer_phone]);
res.status(201).json({ success: true, message: 'Customer saved successfully', client: rows[0] });
} catch (err) {
console.error('Add client error:', err);
res.status(500).json({ success: false, message: 'Server error' });
}
});
// Get all customers (for the Recent Customers list)
app.get('/api/clients', async (req, res) => {
try {
const [rows] = await pool.execute('SELECT * FROM clients ORDER BY created_at DESC');
res.json({ success: true, clients: rows });
} catch (err) {
console.error('Fetch clients error:', err);
res.status(500).json({ success: false, message: 'Server error' });
}
});
// Search customers by name / phone
app.get('/api/clients/search', async (req, res) => {
const q = req.query.q || '';
try {
const [rows] = await pool.execute(
`SELECT * FROM clients WHERE customer_name LIKE ? OR customer_phone LIKE ? ORDER BY created_at DESC`,
[`%${q}%`, `%${q}%`]
);
res.json({ success: true, clients: rows });
} catch (err) {
console.error('Search clients error:', err);
res.status(500).json({ success: false, message: 'Server error' });
}
});
app.post('/api/bills', async (req, res) => {
const { invoice_no, customer_name, subtotal, cgst, sgst, igst, discount, grand_total } = req.body;
if (!invoice_no || !customer_name || subtotal === undefined) {
return res.status(400).json({ success: false, message: 'Missing required bill fields' });
}
try {
await pool.execute(
`INSERT INTO bills (invoice_no, customer_name, subtotal, cgst, sgst, igst, discount, grand_total)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
[invoice_no, customer_name, subtotal, cgst || 0, sgst || 0, igst || 0, discount || 0, grand_total]
);
res.status(201).json({ success: true, message: 'Bill saved successfully' });
} catch (err) {
console.error('Save bill error:', err);
res.status(500).json({ success: false, message: 'Server error', detail: err.message });
}
});
app.get('/api/bills', async (req, res) => {
try {
const [rows] = await pool.execute('SELECT * FROM bills ORDER BY bill_date DESC');
res.json({ success: true, bills: rows });
} catch (err) {
console.error('Fetch bills error:', err);
res.status(500).json({ success: false, message: 'Server error' });
}
});
// Delete a customer
app.delete('/api/clients/:id', async (req, res) => {
const { id } = req.params;
try {
const [result] = await pool.execute('DELETE FROM clients WHERE id = ?', [id]);
if (result.affectedRows === 0) {
return res.status(404).json({ success: false, message: 'Customer not found' });
}
res.json({ success: true, message: 'Customer deleted successfully' });
} catch (err) {
console.error('Delete client error:', err);
res.status(500).json({ success: false, message: 'Server error' });
}
});