mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-18 19:49:16 +00:00
increase 95% coverage
This commit is contained in:
17
packages/database/src/mock/index.js
Normal file
17
packages/database/src/mock/index.js
Normal file
@@ -0,0 +1,17 @@
|
||||
class MockDatabase {
|
||||
listHistory = []
|
||||
|
||||
constructor() {
|
||||
/**
|
||||
* Se debe cargar listHistory con historial de mensajes
|
||||
* para que se pueda continuar el flow
|
||||
*/
|
||||
}
|
||||
|
||||
save = (ctx) => {
|
||||
console.log('Guardando DB...', ctx)
|
||||
this.listHistory.push(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MockDatabase
|
||||
46
packages/database/src/mongo/index.js
Normal file
46
packages/database/src/mongo/index.js
Normal file
@@ -0,0 +1,46 @@
|
||||
require('dotenv').config()
|
||||
const { MongoClient } = require('mongodb')
|
||||
|
||||
const DB_URI = process.env.DB_URI || 'mongodb://0.0.0.0:27017'
|
||||
const DB_NAME = process.env.DB_NAME || 'db_bot'
|
||||
|
||||
class MongoAdapter {
|
||||
db
|
||||
listHistory = []
|
||||
|
||||
constructor() {
|
||||
this.init().then()
|
||||
}
|
||||
|
||||
init = async () => {
|
||||
try {
|
||||
const client = new MongoClient(DB_URI, {})
|
||||
await client.connect()
|
||||
console.log('🆗 Conexión Correcta DB')
|
||||
const db = client.db(DB_NAME)
|
||||
this.db = db
|
||||
return true
|
||||
} catch (e) {
|
||||
console.log('Error', e)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
getPrevByNumber = async (from) => {
|
||||
const result = await this.db
|
||||
.collection('history')
|
||||
.find({ from })
|
||||
.sort({ _id: -1 })
|
||||
.limit(1)
|
||||
.toArray()
|
||||
return result[0]
|
||||
}
|
||||
|
||||
save = async (ctx) => {
|
||||
await this.db.collection('history').insert(ctx)
|
||||
console.log('Guardando DB...', ctx)
|
||||
this.listHistory.push(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MongoAdapter
|
||||
Reference in New Issue
Block a user