-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
99 lines (83 loc) · 2.63 KB
/
server.js
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
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const WebSocket = require('ws');
const app = express();
const wss = new WebSocket.Server({ port: 2002 });
const port = 2001;
const db = new sqlite3.Database('transDetail.db')
db.run(`CREATE TABLE IF NOT EXISTS TransDetails (
id INTEGER PRIMARY KEY AUTOINCREMENT,
transaction_id TEXT NOT NULL,
sender_did TEXT NOT NULL,
receiver_did TEXT NOT NULL,
token_time REAL NOT NULL,
token_id TEXT NOT NULL,
amount REAL NOT NULL,
transaction_type INTEGER NOT NULL,
quorum_list TEXT NOT NULL
)`);
app.use(express.json());
app.post('/api/v2/services/app/Rubix/CreateOrUpdateRubixTransaction', (req, res) => {
console.log("accessed")
const {
transaction_id,
sender_did,
receiver_did,
token_time,
token_id,
amount,
transaction_type,
quorum_list
} = req.body;
if (!transaction_id || !sender_did || !receiver_did || !token_time || !token_id || !amount || !transaction_type || !quorum_list) {
return res.status(400).send('Field values missing. All fields are required');
}
const query = `INSERT INTO TransDetails (transaction_id, sender_did, receiver_did, token_time, token_id, amount, transaction_type, quorum_list) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
db.run(query, [
transaction_id,
sender_did,
receiver_did,
token_time,
JSON.stringify(token_id),
amount,
transaction_type,
JSON.stringify(quorum_list)
], function(err) {
if (err) {
return res.status(500).send('Error saving data');
}
const newTransaction = {
id: this.lastID,
transaction_id,
sender_did,
receiver_did,
token_time,
token_id,
amount,
transaction_type,
quorum_list
};
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(newTransaction));
}
});
res.send({ success: true, id: this.lastID });
});
});
// API to get data from TransDetails table
app.get('/get-trans-data', (req, res) => {
db.all(`SELECT * FROM TransDetails`, (err, rows) => {
if (err) {
return res.status(500).send('Error fetching data');
}
res.json(rows);
});
});
// Serve the main page
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});