Repository navigation
Expand file tree
/
Copy pathdb.js
More file actions
159 lines (149 loc) · 4.86 KB
/
Copy pathdb.js
File metadata and controls
159 lines (149 loc) · 4.86 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
require("dotenv").config();
const mysql = require("mysql2");
const { getDate, getDateBefore } = require("./utils");
// -------------- CONSTANTS & ENV VARS ---------------
const OneX = "ONEX";
const maxStockDefault = process.env.MAX_STOCK_DEFAULT;
const P2Pallowed = process.env.P2P_ALLOWED;
const stock_limit = process.env.STOCK_LIMIT;
const first_welcome_clients = process.env.FIRST_WELCOME_CLIENTS;
const welcome_bonus = process.env.WELCOME_BONUS;
const tfees = Number(process.env.FEES);
const admin_pswd = process.env.ADMIN_PSWD;
const admin_pin = process.env.ADMIN_PIN;
const APP_VERSION = process.env.APP_VERSION;
const PORT_DB = process.env.DB_PORT || 3306;
const PORT_SERVER = process.env.SERVER_PORT;
const MAIL_PORT = process.env.MAIL_PORT;
const SERVER_MAIL = process.env.SERV_MAIL;
const SERVER_MAIL_PSWD = process.env.SERV_MAIL_PSWD;
const DB_CONFIG = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PSWD,
database: process.env.DB_NAME,
port: PORT_DB,
waitForConnections: true,
connectionLimit: 200,
queueLimit: 200,
multipleStatements: true,
};
// ---------------- DATABASE POOL --------------------
const db = mysql.createPool(DB_CONFIG);
db.queryAsync = (sql, params = []) =>
new Promise((res, rej) =>
db.query(sql, params, (e, r) => (e ? rej(e) : res(r))),
);
async function initDbIfEmpty() {
const [date, time] = getDate();
try {
await db.queryAsync("SELECT id FROM users_stock WHERE username=?", [OneX]);
} catch {
const tables = [
`CREATE TABLE IF NOT EXISTS auths(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL, password VARCHAR(255) NOT NULL,
name VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL,
birthdate VARCHAR(20) NOT NULL, cni VARCHAR(255) NOT NULL,
address VARCHAR(50) NOT NULL, seed TEXT NOT NULL,
status INT, record_date INT, record_time VARCHAR(10)
);`,
`CREATE TABLE IF NOT EXISTS users_stock(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL, balance VARCHAR(255) DEFAULT '0',
record_date INT, record_time VARCHAR(10)
);`,
`CREATE TABLE IF NOT EXISTS activities(
id INT AUTO_INCREMENT PRIMARY KEY,
sender VARCHAR(30) NOT NULL, receiver VARCHAR(30) NOT NULL,
type INT, amount VARCHAR(30) NOT NULL,
unit_price VARCHAR(255) DEFAULT '0',
fees VARCHAR(30) DEFAULT '0', reference VARCHAR(20),
record_date INT, record_time VARCHAR(10)
);`,
`CREATE TABLE IF NOT EXISTS notifs(
id INT AUTO_INCREMENT PRIMARY KEY,
content TEXT, record_date INT, record_time VARCHAR(10)
);`,
`CREATE TABLE IF NOT EXISTS common(
id INT AUTO_INCREMENT PRIMARY KEY,
total_units_price VARCHAR(255) DEFAULT '0',
unit_price VARCHAR(255) DEFAULT '0',
backed_units VARCHAR(255) DEFAULT '0',
record_date INT, record_time VARCHAR(10)
);`,
];
let tables_count = tables.length;
for (let s of tables) await db.queryAsync(s);
console.log("Each table of " + tables_count + " ready");
await db.queryAsync(
"INSERT INTO users_stock(username,balance,record_date,record_time) VALUES(?,?,?,?)",
[OneX, "0", date, time],
);
console.log("OneX ready");
await db.queryAsync(
"INSERT INTO common(total_units_price,unit_price,backed_units,record_date,record_time) VALUES(?,?,?,?,?)",
[stock_limit, "1", "0", date, time],
);
console.log("Unit price initialized");
}
}
async function cleanup() {
const [date7] = getDateBefore(7);
try {
const rows = await db.queryAsync(
"SELECT username FROM auths WHERE status=0 AND record_date=?",
[date7],
);
for (let { username: u } of rows) {
await db.queryAsync(
"DELETE FROM users_stock WHERE username=?;DELETE FROM auths WHERE username=?;",
[u, u],
);
}
} catch (e) {
console.error(e);
}
}
async function lastUnitPrice(user) {
let price = 1;
try {
if (isActive(user)) {
const [[{ unit_price } = {}]] = await db.queryAsync(
"SELECT MAX(unit_price) AS unit_price FROM common",
);
if (unit_price != null) price = unit_price;
} else {
const [[{ unit_price } = {}]] = await db.queryAsync(
"SELECT unit_price FROM activities WHERE username = ? ORDER BY id DESC LIMIT 1",
[user],
);
if (unit_price != null) price = unit_price;
}
} catch (error) {
console.error("Error retrieving appropriate unit price :", error);
}
return price;
}
async function isActive(user) {}
module.exports = {
db,
OneX,
maxStockDefault,
P2Pallowed,
stock_limit,
first_welcome_clients,
welcome_bonus,
tfees,
admin_pswd,
admin_pin,
APP_VERSION,
PORT_SERVER,
MAIL_PORT,
SERVER_MAIL,
SERVER_MAIL_PSWD,
initDbIfEmpty,
cleanup,
lastUnitPrice,
isActive,
};