-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
173 lines (147 loc) · 4.03 KB
/
server.js
File metadata and controls
173 lines (147 loc) · 4.03 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
require('dotenv').config();
const express = require('express');
const session = require('express-session');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// ----- Middleware -----
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(
session({
secret: process.env.SESSION_SECRET || 'dev-secret',
resave: false,
saveUninitialized: false
})
);
// Serve static UI from /public
app.use(express.static(path.join(__dirname, 'public')));
// ----- In-memory data (NOT for real banking, just demo) -----
const users = [
{
id: 1,
username: 'alice',
password: 'password123', // plain text only for demo
name: 'Alice Doe'
},
{
id: 2,
username: 'bob',
password: 'password123',
name: 'Bob Smith'
}
];
let accounts = [
// each: { id, userId, type, balance }
{ id: 1001, userId: 1, type: 'CHECKING', balance: 1000 },
{ id: 1002, userId: 1, type: 'SAVINGS', balance: 5000 },
{ id: 2001, userId: 2, type: 'CHECKING', balance: 750 }
];
let transfers = []; // each: { id, fromAccountId, toAccountId, amount, timestamp }
// ----- Helper auth middleware -----
function requireAuth(req, res, next) {
if (!req.session.userId) {
return res.status(401).json({ error: 'Unauthorized' });
}
next();
}
function currentUser(req) {
return users.find(u => u.id === req.session.userId) || null;
}
// ----- Routes -----
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'ok', uptime: process.uptime() });
});
// Auth: login
app.post('/api/login', (req, res) => {
const { username, password } = req.body;
const user = users.find(
u => u.username === username && u.password === password
);
if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}
req.session.userId = user.id;
res.json({
id: user.id,
username: user.username,
name: user.name
});
});
// Auth: logout
app.post('/api/logout', (req, res) => {
req.session.destroy(() => {
res.json({ ok: true });
});
});
// Get current user
app.get('/api/me', requireAuth, (req, res) => {
const user = currentUser(req);
res.json({
id: user.id,
username: user.username,
name: user.name
});
});
// List accounts for current user
app.get('/api/accounts', requireAuth, (req, res) => {
const user = currentUser(req);
const userAccounts = accounts.filter(a => a.userId === user.id);
res.json(userAccounts);
});
// Transfer between accounts
app.post('/api/transfer', requireAuth, (req, res) => {
const user = currentUser(req);
const { fromAccountId, toAccountId, amount } = req.body;
const amt = Number(amount);
if (!fromAccountId || !toAccountId || !amt || amt <= 0) {
return res.status(400).json({ error: 'Invalid transfer data' });
}
const from = accounts.find(
a => a.id === Number(fromAccountId) && a.userId === user.id
);
const to = accounts.find(a => a.id === Number(toAccountId));
if (!from) {
return res
.status(400)
.json({ error: 'From account not found or not owned by user' });
}
if (!to) {
return res.status(400).json({ error: 'To account not found' });
}
if (from.balance < amt) {
return res.status(400).json({ error: 'Insufficient funds' });
}
from.balance -= amt;
to.balance += amt;
const transfer = {
id: transfers.length + 1,
fromAccountId: from.id,
toAccountId: to.id,
amount: amt,
timestamp: new Date().toISOString()
};
transfers.push(transfer);
res.json({
status: 'SUCCESS',
transfer
});
});
// Transfer history for current user
app.get('/api/transfers', requireAuth, (req, res) => {
const user = currentUser(req);
const userAccountIds = accounts
.filter(a => a.userId === user.id)
.map(a => a.id);
const myTransfers = transfers.filter(
t =>
userAccountIds.includes(t.fromAccountId) ||
userAccountIds.includes(t.toAccountId)
);
res.json(myTransfers);
});
// ----- Start server -----
app.listen(PORT, () => {
console.log(`Banking app listening on http://localhost:${PORT}`);
});