Alteraçã para o auto restore funcionar quando o status for PAIRING

pull/21/head
adriano 2022-12-26 09:05:35 -03:00
parent a59784d18e
commit ccada53316
3 changed files with 34 additions and 33 deletions

View File

@ -53,7 +53,7 @@ const redisConn = async () => {
return await conn(); return await conn();
} catch (e) { } catch (e) {
console.error(e); console.error(`There whas an error on redis connection: ${e}`);
return Promise.resolve([]); return Promise.resolve([]);
} }

View File

@ -8,11 +8,11 @@ import { redisConn } from './TicketCache'
import Whatsapp from "../models/Whatsapp"; import Whatsapp from "../models/Whatsapp";
import { response } from 'express' import { response } from 'express'
const deleteWhatsappCache = async (hash:any) => { const deleteWhatsappCache = async (hash: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
@ -39,7 +39,7 @@ const updateWhatsappCache = async (hash: any, json_object: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
@ -57,18 +57,18 @@ const updateWhatsappCache = async (hash: any, json_object: any) => {
} }
const updateWhatsappCacheById = async (hash:any, update_fields: object | any) => { const updateWhatsappCacheById = async (hash: any, update_fields: object | any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
const whatsapp_cache: any = await redis.hgetall(hash) const whatsapp_cache: any = await redis.hgetall(hash)
try { try {
if (whatsapp_cache && Object.keys(whatsapp_cache).length > 0) { if (whatsapp_cache && Object.keys(whatsapp_cache).length > 0) {
// update_fields.escaped_name = escapeCharCache(update_fields.name) // update_fields.escaped_name = escapeCharCache(update_fields.name)
@ -109,15 +109,15 @@ async function searchWhatsappCache(id: string, status: string) {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
if (redis.status !== 'connect') return null if (redis.status !== 'connect') return null
const number_cache: any = await redis.hgetall(`whatsapp:${id}`) const number_cache: any = await redis.hgetall(`whatsapp:${id}`)
if(Object.entries(number_cache).length == 0){ if (Object.entries(number_cache).length == 0) {
redis.quit() await redis.quit()
return [] return []
} }
@ -126,9 +126,9 @@ async function searchWhatsappCache(id: string, status: string) {
// @x:foo @y:bar // @x:foo @y:bar
const response: any = await redis.call('FT.SEARCH', 'idx_whatsapp', `(@status:*${status}*) (@number:*${number_cache.number}*)`, 'SORTBY', 'status', 'ASC') const response: any = await redis.call('FT.SEARCH', 'idx_whatsapp', `(@status:*${status}*) (@number:*${number_cache.number}*)`, 'SORTBY', 'status', 'ASC')
await redis.quit() await redis.quit()
if (response.length === 1) { if (response.length === 1) {
@ -153,30 +153,30 @@ async function searchWhatsappCache(id: string, status: string) {
return results return results
} }
const insertOrUpeateWhatsCache = async (hash:any, whatsapp: any) => {
const insertOrUpeateWhatsCache = async (hash: any, whatsapp: any) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
if(Array.isArray(whatsapp)){ if (Array.isArray(whatsapp)) {
const pipeline = redis.pipeline() const pipeline = redis.pipeline()
for (let i = 0; i < whatsapp.length; i++) { for (let i = 0; i < whatsapp.length; i++) {
pipeline.hmset(hash, whatsapp[i]); pipeline.hmset(hash, whatsapp[i]);
} }
await pipeline.exec(() => { console.log(`${whatsapp.length} WHATSAPP INSERTED IN CACHE!`) }); await pipeline.exec(() => { console.log(`${whatsapp.length} WHATSAPP INSERTED IN CACHE!`) });
} }
else{ else {
await redis.hmset(hash,JSON.parse(JSON.stringify(whatsapp))); await redis.hmset(hash, JSON.parse(JSON.stringify(whatsapp)));
console.log(`WHATSAPP ID ${whatsapp.id} INSERTED OR UPADTED IN CACHE!`) console.log(`WHATSAPP ID ${whatsapp.id} INSERTED OR UPADTED IN CACHE!`)
@ -195,11 +195,11 @@ const loadWhatsappCache = async () => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
let whatsapps:any = await Whatsapp.findAll({raw: true}) let whatsapps: any = await Whatsapp.findAll({ raw: true })
const pipeline = redis.pipeline() const pipeline = redis.pipeline()
@ -222,7 +222,7 @@ const createWhatsappIndexCache = async (hashIndex: string) => {
const redis: any = await redisConn(); const redis: any = await redisConn();
if(!redis) return if (!redis) return
if (redis.status !== 'connect') return if (redis.status !== 'connect') return
@ -235,7 +235,7 @@ const createWhatsappIndexCache = async (hashIndex: string) => {
await redis.call('FT.DROPINDEX', hashIndex) await redis.call('FT.DROPINDEX', hashIndex)
} }
const response = await redis.call('FT.CREATE', hashIndex, 'ON', 'HASH', 'PREFIX', '1', 'whatsapp:', 'SCHEMA','id', 'NUMERIC', 'status', 'TEXT', 'SORTABLE', 'number', 'TEXT', 'SORTABLE') const response = await redis.call('FT.CREATE', hashIndex, 'ON', 'HASH', 'PREFIX', '1', 'whatsapp:', 'SCHEMA', 'id', 'NUMERIC', 'status', 'TEXT', 'SORTABLE', 'number', 'TEXT', 'SORTABLE')
console.log('Whatsapp index created: ', response) console.log('Whatsapp index created: ', response)
@ -248,8 +248,8 @@ const createWhatsappIndexCache = async (hashIndex: string) => {
export { export {
loadWhatsappCache, loadWhatsappCache,
searchWhatsappCache, searchWhatsappCache,
updateWhatsappCacheById, updateWhatsappCacheById,
insertOrUpeateWhatsCache, insertOrUpeateWhatsCache,
deleteWhatsappCache deleteWhatsappCache
} }

View File

@ -69,6 +69,7 @@ export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: bo
let whatsappStatus = ["CONFLICT", let whatsappStatus = ["CONFLICT",
"DEPRECATED_VERSION", "DEPRECATED_VERSION",
"OPENING", "OPENING",
"PAIRING",
"PROXYBLOCK", "PROXYBLOCK",
"SMB_TOS_BLOCK", "SMB_TOS_BLOCK",
"TIMEOUT", "TIMEOUT",