chore: pre-chore

This commit is contained in:
Leifer Mendez
2023-02-04 18:03:57 +01:00
parent 99b9b17f52
commit 2ecc41f3f0
82 changed files with 411 additions and 904 deletions

View File

@@ -24,12 +24,7 @@ class MongoAdapter {
}
getPrevByNumber = async (from) => {
const result = await this.db
.collection('history')
.find({ from })
.sort({ _id: -1 })
.limit(1)
.toArray()
const result = await this.db.collection('history').find({ from }).sort({ _id: -1 }).limit(1).toArray()
return result[0]
}

View File

@@ -1,76 +1,66 @@
const mysql = require("mysql2");
const mysql = require('mysql2')
class MyslAdapter {
db;
listHistory = [];
credentials = { host: null, user: null, database: null, password: null };
db
listHistory = []
credentials = { host: null, user: null, database: null, password: null }
constructor(_credentials) {
this.credentials = _credentials;
this.init().then();
}
constructor(_credentials) {
this.credentials = _credentials
this.init().then()
}
async init() {
this.db = mysql.createConnection(this.credentials);
async init() {
this.db = mysql.createConnection(this.credentials)
await this.db.connect(async (error) => {
if (!error) {
console.log(`Solicitud de conexión a base de datos exitosa`);
await this.checkTableExists();
}
await this.db.connect(async (error) => {
if (!error) {
console.log(`Solicitud de conexión a base de datos exitosa`)
await this.checkTableExists()
}
if (error) {
console.log(`Solicitud de conexión fallida ${error.stack}`);
}
});
}
if (error) {
console.log(`Solicitud de conexión fallida ${error.stack}`)
}
})
}
getPrevByNumber = (from) =>
new Promise((resolve, reject) => {
const sql = `SELECT * FROM history WHERE phone=${from} ORDER BY id DESC`;
this.db.query(sql, (error, rows) => {
if (error) {
reject(error);
}
getPrevByNumber = (from) =>
new Promise((resolve, reject) => {
const sql = `SELECT * FROM history WHERE phone=${from} ORDER BY id DESC`
this.db.query(sql, (error, rows) => {
if (error) {
reject(error)
}
if (rows.length) {
const [row] = rows;
row.options = JSON.parse(row.options);
resolve(row);
}
if (rows.length) {
const [row] = rows
row.options = JSON.parse(row.options)
resolve(row)
}
if (!rows.length) {
resolve(null);
}
});
});
if (!rows.length) {
resolve(null)
}
})
})
save = (ctx) => {
const values = [
[
ctx.ref,
ctx.keyword,
ctx.answer,
ctx.refSerialize,
ctx.from,
JSON.stringify(ctx.options),
],
];
const sql =
"INSERT INTO history (ref, keyword, answer, refSerialize, phone, options ) values ?";
save = (ctx) => {
const values = [[ctx.ref, ctx.keyword, ctx.answer, ctx.refSerialize, ctx.from, JSON.stringify(ctx.options)]]
const sql = 'INSERT INTO history (ref, keyword, answer, refSerialize, phone, options ) values ?'
this.db.query(sql, [values], (err) => {
if (err) throw err;
console.log("Guardado en DB...", values);
});
this.listHistory.push(ctx);
};
this.db.query(sql, [values], (err) => {
if (err) throw err
console.log('Guardado en DB...', values)
})
this.listHistory.push(ctx)
}
createTable = () =>
new Promise((resolve) => {
const tableName = "history";
createTable = () =>
new Promise((resolve) => {
const tableName = 'history'
const sql = `CREATE TABLE ${tableName}
const sql = `CREATE TABLE ${tableName}
(id INT AUTO_INCREMENT PRIMARY KEY,
ref varchar(255) NOT NULL,
keyword varchar(255) NOT NULL,
@@ -78,29 +68,29 @@ class MyslAdapter {
refSerialize varchar(255) NOT NULL,
phone varchar(255) NOT NULL,
options longtext NOT NULL)
CHARACTER SET utf8mb4 COLLATE utf8mb4_General_ci`;
CHARACTER SET utf8mb4 COLLATE utf8mb4_General_ci`
this.db.query(sql, (err) => {
if (err) throw err;
console.log(`Tabla ${tableName} creada correctamente `);
resolve(true);
});
});
this.db.query(sql, (err) => {
if (err) throw err
console.log(`Tabla ${tableName} creada correctamente `)
resolve(true)
})
})
checkTableExists = () =>
new Promise((resolve) => {
const sql = "SHOW TABLES LIKE 'history'";
checkTableExists = () =>
new Promise((resolve) => {
const sql = "SHOW TABLES LIKE 'history'"
this.db.query(sql, (err, rows) => {
if (err) throw err;
this.db.query(sql, (err, rows) => {
if (err) throw err
if (!rows.length) {
this.createTable();
}
if (!rows.length) {
this.createTable()
}
resolve(!!rows.length);
});
});
resolve(!!rows.length)
})
})
}
module.exports = MyslAdapter;
module.exports = MyslAdapter