-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.js
170 lines (147 loc) · 6.37 KB
/
Database.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
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
const Ticket = require("./Ticket");
module.exports = class Database {
constructor(pool) {
this._pool = pool
}
async getAllTickets(status) {
const _pool = this._pool;
return await new Promise((resolve, reject) => {
_pool.query("SELECT * FROM tickets WHERE status=? ORDER BY id DESC LIMIT 25;", [status], async function (error, results) {
resolve(results);
});
});
}
async getCountTickets(status) {
const _pool = this._pool;
return await new Promise((resolve, reject) => {
_pool.query('SELECT COUNT(*) as data FROM tickets WHERE status= ?', [status], function (error, results, fields) {
resolve(results[0].data);
});
});
}
async getCountTicketsCorrectRole(status, role) {
const _pool = this._pool;
return await new Promise((resolve, reject) => {
_pool.query('SELECT COUNT(*) as data FROM tickets WHERE status = ? AND role = ?', [status, role], function (error, results, fields) {
resolve(results[0].data);
});
});
}
async getAllTicketsCorrectRole(status, role) {
const _pool = this._pool;
return await new Promise((resolve, reject) => {
_pool.query("SELECT * FROM tickets WHERE status=? AND role=? ORDER BY id DESC LIMIT 25;", [status, role], async function (error, results) {
resolve(results);
});
});
}
async getName(discord_id) {
const _pool = this._pool;
return await new Promise((resolve_name, reject) => {
_pool.query("SELECT * FROM users WHERE discord_id=? LIMIT 1;", [discord_id], async function (error, results) {
if (error) throw error;
if (results.length > 0) {
resolve_name(results[0].name);
} else {
resolve_name(undefined);
}
});
});
}
async getDataMessage(message_id) {
const _pool = this._pool;
return await new Promise((resolve_message, reject) => {
_pool.query("SELECT message,discord_id FROM messages WHERE id=? LIMIT 1;", [message_id], async function (error, results) {
resolve_message(results[0]);
});
});
}
async getOfflineTicket(ticket_id) {
const _pool = this._pool;
return await new Promise((resolve, reject) => {
_pool.query("SELECT * FROM tickets WHERE id=?;", [ticket_id], async function (error, results) {
if (error) throw error;
const ticket = new Ticket(undefined);
ticket.owner = undefined;
ticket.ownerDiscordID = results[0].discord_id;
ticket.title = results[0].title;
ticket.messages_numbers = results[0].description.split(",");
ticket.status = results[0].status;
ticket.date_created = results[0].date_created;
ticket.date_closed = results[0].date_closed;
ticket.username = results[0].username;
ticket.role = results[0].role;
resolve(ticket);
});
});
}
async getRole(discord_id) {
return await new Promise((resolve, reject) => {
this._pool.query('SELECT role FROM users WHERE discord_id=?', [discord_id], function (error, results) {
if (error) throw error;
if (results.length > 0) {
resolve(results[0].role)
} else {
resolve("user");
}
});
});
}
async createTicket(/*Ticket*/ ticket) {
const user = ticket.owner;
const date = new Date()
this._pool.query('INSERT INTO tickets SET ?', {discord_id: user.getDiscordID(), username: user.username, title: ticket.title, description: ticket.messages_numbers, status: 'PENDING', date_created: date.toLocaleDateString() + " " + date.toLocaleTimeString(), date_closed: '-', answered: '-', role: 'admin'}, function (error, results) {
if (error) throw error;
});
}
async getLastCreatedTicketId(user) {
const _pool = this._pool;
return await new Promise((resolve, reject) => {
_pool.query("SELECT id FROM tickets WHERE discord_id=? AND status='PENDING' ORDER BY id DESC LIMIT 1;", [user.getDiscordID()], async function (error, results) {
if (error) throw error;
resolve(results[0].id);
});
});
}
async updateTicket(ticket_id, ticket) {
for (let i = 0; i < ticket.messages_numbers.length; i++) {
ticket.description = (i+1 !== ticket.messages_numbers.length) ?
ticket.description += ticket.messages_numbers[i] + "," :
ticket.description += ticket.messages_numbers[i];
}
const _pool = this._pool;
const sql = "UPDATE tickets t SET " +
"t.discord_id = ?, " +
"t.title = ?, " +
"t.description = ?, " +
"t.status = ?, " +
"t.date_created = ?, " +
"t.date_closed = ?, " +
"t.answered = ? ," +
"t.role = ? " +
"WHERE t.id = ?";
_pool.query(sql, [ticket.ownerDiscordID, ticket.title, ticket.description, ticket.status, ticket.date_created, ticket.date_closed, ticket.answered, ticket.role, ticket_id], async function (error) {
if (error) throw error;
});
}
async createMessageID(user, message) {
this._pool.query('INSERT INTO messages SET ?', {discord_id: user.getDiscordID(), message: message}, function (error) {
if (error) console.log(error)
});
return await new Promise((resolve, reject) => {
this._pool.query('SELECT id FROM messages WHERE discord_id=? ORDER BY id DESC LIMIT 1;', [user.getDiscordID()], function (error, results) {
if (error) throw error;
const message_id = results[0].id;
resolve(message_id);
});
});
}
async getListRoleId(role) {
const _pool = this._pool;
return await new Promise((resolve, reject) => {
_pool.query("SELECT * FROM users WHERE role=?", [role], async function (error, results) {
resolve(results);
});
});
}
}