Alteração para que o cache seja atualizado apos o bulk insert de contactos

pull/20/head
adriano 2022-11-11 20:49:43 -03:00
commit 82f0f107f4
2 changed files with 37 additions and 6 deletions

View File

@ -11,9 +11,10 @@ import DeleteContactService from "../services/ContactServices/DeleteContactServi
import CheckContactNumber from "../services/WbotServices/CheckNumber"
import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
import AppError from "../errors/AppError";
import AppError from "../errors/AppError";
import { searchContactCache, insertContactsCache } from '../helpers/ContactsCache'
import { searchContactCache } from '../helpers/ContactsCache'
import { off } from "process";
import GetContactService from "../services/ContactServices/GetContactService";
@ -57,7 +58,7 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
const data = await searchContactCache(searchParam, offset, 20)
if (data) {
if (data) {
console.log('QUERY CONTACTS FROM CACHE QUERY LENGTH: ', data.length)
@ -233,7 +234,7 @@ export const contacsBulkInsertOnQueue = async (req: Request, res: Response): Pro
// console.log('THE BODY: ', req.body)
const { adminId, identifier, queueStatus, file } = req.body
const { adminId, identifier, queueStatus, file, contacts_inserted } = req.body
const io = getIO();
io.emit("contactsBulkInsertOnQueueStatus", {
@ -245,7 +246,14 @@ export const contacsBulkInsertOnQueue = async (req: Request, res: Response): Pro
file: file
}
});
return res.status(200).json({ message: 'ok' })
if (process.env.CACHE && contacts_inserted) {
await insertContactsCache(contacts_inserted)
}
return res.status(200).json({ message: 'ok' })
};

View File

@ -162,6 +162,28 @@ const escapeCharCache = (str: string) => {
}
const insertContactsCache = async (contacts: any) => {
const redis: any = await redisConn();
if(!redis) return
if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
for (let i = 0; i < contacts.length; i++) {
contacts[i].escaped_name = escapeCharCache(contacts[i].name)
pipeline.hmset(`contact:${contacts[i].id}`, contacts[i]);
}
await pipeline.exec(() => { console.log(`${contacts.length} CONTACTS INSERTED IN CACHE!`) });
redis.quit()
}
const loadContactsCache = async () => {
await createContactIndexCache('idx_contact')
@ -225,5 +247,6 @@ export {
deleteContactsByIdCache,
updateContactCacheById,
createOrUpdateContactCache,
escapeCharCache
escapeCharCache,
insertContactsCache
}