-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
385 lines (338 loc) · 11.2 KB
/
index.js
File metadata and controls
385 lines (338 loc) · 11.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
const express = require('express');
const { Customer, Account, Transaction } = require('./models');
const store = require('./store');
const MockClient = require('./bankz');
const { v4: uuidv4 } = require('uuid');
/**
* The `app` variable is an instance of an Express application.
* Express is a minimal and flexible Node.js web application framework
* that provides a robust set of features for building web and mobile applications.
* It is commonly used to create APIs and handle HTTP requests and responses.
*/
const app = express();
const cors = require('cors');
const bankzClient = new MockClient(); // Global client for simplicity
// Middleware to parse JSON bodies
app.use(express.json());
app.use(cors());
app.post('/api/customers', (req, res) => {
const { name, email } = req.body;
if (!name || !email) {
return res.status(400).json({ errors: ['Invalid request body'] });
}
const errors = [];
if (name.trim() === '') {
errors.push('Name cannot be empty');
}
const nameRegex = /^[a-zA-Z\s]+$/;
if (name.trim() !== '' && !nameRegex.test(name)) {
errors.push('Name must contain only letters and spaces');
}
if (email.trim() === '') {
errors.push('Email cannot be empty');
}
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (email.trim() !== '' && !emailRegex.test(email)) {
errors.push('Invalid email format');
}
if (errors.length > 0) {
return res.status(400).json({ errors });
}
const customerId = uuidv4();
const currentAccountId = uuidv4();
const savingsAccountId = uuidv4();
const bonusTransactionId = uuidv4();
const now = new Date();
const customer = new Customer(customerId, name, email);
store.addCustomer(customer);
const currentAccount = new Account(
currentAccountId,
customerId,
'current',
0.0,
now
);
store.addAccount(currentAccount);
const savingsAccount = new Account(
savingsAccountId,
customerId,
'savings',
500.0,
now
);
store.addAccount(savingsAccount);
const bonusTransaction = new Transaction(
bonusTransactionId,
savingsAccountId,
customerId,
'bonus',
500.0,
null,
null,
now
);
store.addTransaction(bonusTransaction);
const response = {
customerId: customerId,
currentAccountId: currentAccountId,
savingsAccountId: savingsAccountId
};
console.log(`Created customer: ${JSON.stringify(response)}`); // Debug log
res.status(201).json(response);
});
app.get('/api/customers/:customerId/accounts', (req, res) => {
const customerId = req.params.customerId;
const customer = store.getCustomerById(customerId);
if (!customer) {
return res.status(404).json({ error: 'Customer not found' });
}
const accounts = store.getAccountsByCustomerId(customerId);
if (accounts.length === 0) {
return res.status(404).json({ error: 'No accounts found for customer' });
}
const response = {
customerId: customerId,
accounts: accounts.map(account => ({
id: account.id,
customerId: account.customerId,
type: account.type,
balance: account.balance,
createdAt: account.createdAt.toISOString()
}))
};
console.log(`Fetched accounts for customer ${customerId}: ${accounts.map(acc => acc.id)}`); // Debug log
res.status(200).json(response);
});
app.post('/api/customers/:customerId/transfers', (req, res) => {
const customerId = req.params.customerId;
const { fromAccountId, toAccountId, amount } = req.body;
if (!fromAccountId || !toAccountId || !amount) {
return res.status(400).json({ errors: ['Invalid request body'] });
}
const errors = [];
if (fromAccountId.trim() === '') {
errors.push('From account ID cannot be empty');
}
if (toAccountId.trim() === '') {
errors.push('To account ID cannot be empty');
}
if (typeof amount !== 'number' || amount <= 0) {
errors.push('Amount must be positive');
}
if (fromAccountId === toAccountId && fromAccountId) {
errors.push('Cannot transfer to the same account');
}
if (errors.length > 0) {
return res.status(400).json({ errors });
}
const customer = store.getCustomerById(customerId);
if (!customer) {
return res.status(404).json({ error: 'Customer not found' });
}
// Get OAuth token
let token;
try {
token = bankzClient.getToken();
} catch (e) {
return res.status(500).json({ error: 'Failed to authenticate with Bank Z' });
}
// Check fromAccount
const fromAccount = store.getAccountById(fromAccountId);
console.log(`Looking up fromAccountID ${fromAccountId}: found=${!!fromAccount}`); // Debug log
if (!fromAccount) {
return res.status(400).json({ error: `Source account ${fromAccountId} not found` });
}
if (fromAccount.customerId !== customerId) {
return res.status(400).json({ error: 'Source account does not belong to the customer' });
}
// Check toAccount
let isExternalTransfer = false;
let toAccount = store.getAccountById(toAccountId);
console.log(`Looking up toAccountID ${toAccountId}: found=${!!toAccount}`); // Debug log
if (!toAccount) {
try {
bankzClient.getBalance(toAccountId, token);
isExternalTransfer = true;
} catch (e) {
return res.status(400).json({ error: `Destination account ${toAccountId} not found` });
}
} else if (toAccount.customerId !== customerId) {
return res.status(400).json({ error: 'Destination account does not belong to the customer' });
}
if (isExternalTransfer) {
// Bank Z transfer
if (fromAccount.balance < amount) {
return res.status(400).json({ error: 'Insufficient funds' });
}
let transactionId;
try {
transactionId = bankzClient.initiateTransfer(fromAccountId, toAccountId, token, amount);
} catch (e) {
return res.status(400).json({ error: e.message });
}
// Update local account balance
fromAccount.balance -= amount;
store.updateAccount(fromAccount);
// Record transaction
const now = new Date();
store.addTransaction(new Transaction(
transactionId,
fromAccountId,
customerId,
'bankz_transfer',
amount,
fromAccountId,
toAccountId,
now
));
const response = {
transactionId: transactionId,
status: 'success'
};
console.log(`Bank Z transfer successful: ${JSON.stringify(response)}`); // Debug log
return res.status(201).json(response);
}
// Internal transfer
let fee = 0.0;
if (fromAccount.type === 'current') {
fee = amount * 0.0005;
}
const totalDeduction = amount + fee;
if (fromAccount.balance < totalDeduction) {
return res.status(400).json({ error: 'Insufficient funds' });
}
let interest = 0.0;
if (toAccount.type === 'savings') {
interest = amount * 0.005;
}
fromAccount.balance -= totalDeduction;
toAccount.balance += amount + interest;
store.updateAccount(fromAccount);
store.updateAccount(toAccount);
const now = new Date();
const transferTransactionId = uuidv4();
store.addTransaction(new Transaction(
transferTransactionId,
fromAccountId,
customerId,
'transfer',
amount,
fromAccountId,
toAccountId,
now
));
if (fee > 0) {
const feeTransactionId = uuidv4();
store.addTransaction(new Transaction(
feeTransactionId,
fromAccountId,
customerId,
'fee',
fee,
null,
null,
now
));
}
if (interest > 0) {
const interestTransactionId = uuidv4();
store.addTransaction(new Transaction(
interestTransactionId,
toAccountId,
customerId,
'interest',
interest,
null,
null,
now
));
}
const response = {
transactionId: transferTransactionId,
status: 'success'
};
console.log(`Internal transfer successful: ${JSON.stringify(response)}`); // Debug log
res.status(201).json(response);
});
app.get('/api/customers/:customerId/bankz/balances', (req, res) => {
const customerId = req.params.customerId;
const customer = store.getCustomerById(customerId);
if (!customer) {
return res.status(404).json({ error: 'Customer not found' });
}
// Get OAuth token
let token;
try {
token = bankzClient.getToken();
} catch (e) {
return res.status(500).json({ error: 'Failed to authenticate with Bank Z' });
}
const linkedAccountIds = ['bankz-acc-123', 'bankz-acc-456'];
const balances = [];
for (const accountId of linkedAccountIds) {
try {
const balance = bankzClient.getBalance(accountId, token);
balances.push({
accountId: accountId,
balance: balance
});
} catch (e) {
continue; // Skip invalid accounts
}
}
const response = {
customerId: customerId,
balances: balances
};
res.status(200).json(response);
});
app.get('/api/customers/:customerId/transactions', (req, res) => {
const customerId = req.params.customerId;
// Verify customer exists
const customer = store.getCustomerById(customerId);
if (!customer) {
return res.status(404).json({ error: 'Customer not found' });
}
// Get query parameters for filtering
const accountId = req.query.accountId;
const transactionType = req.query.type;
// Fetch transactions
let transactions = store.getTransactionsByCustomerId(customerId);
if (transactions.length === 0) {
return res.status(200).json({
customerId: customerId,
transactions: []
});
}
// Filter transactions
transactions = transactions.filter(tx => {
if (accountId && tx.accountId !== accountId) {
return false;
}
if (transactionType && tx.type !== transactionType) {
return false;
}
return true;
});
// Convert to response format
const responseTransactions = transactions.map(tx => ({
id: tx.id,
accountId: tx.accountId,
customerId: tx.customerId,
type: tx.type,
amount: tx.amount,
fromAccountId: tx.fromAccountId || '',
toAccountId: tx.toAccountId || '',
createdAt: tx.createdAt.toISOString()
}));
const response = {
customerId: customerId,
transactions: responseTransactions
};
console.log(`Fetched ${responseTransactions.length} transactions for customer ${customerId}`); // Debug log
res.status(200).json(response);
});
const PORT = 8080;
app.listen(PORT, () => {
console.log(`Starting javascript-banking server on :${PORT}...`);
});