From 9d4b80986df73c262963173639bf82953c76813b Mon Sep 17 00:00:00 2001 From: adriano Date: Thu, 7 Mar 2024 11:47:42 -0300 Subject: [PATCH] feat: Add function in controller to remove user and update cache in Redis --- backend/src/controllers/UserController.ts | 10 +++++----- backend/src/helpers/RedisClient.ts | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/backend/src/controllers/UserController.ts b/backend/src/controllers/UserController.ts index c4463aa..013eeb6 100644 --- a/backend/src/controllers/UserController.ts +++ b/backend/src/controllers/UserController.ts @@ -12,7 +12,7 @@ import DeleteUserService from "../services/UserServices/DeleteUserService"; import ListUser from "../services/UserServices/ListUserParamiterService"; import User from "../models/User"; -import { get, set } from "../helpers/RedisClient"; +import { del, get, set } from "../helpers/RedisClient"; import { startWhoIsOnlineMonitor, @@ -27,7 +27,7 @@ import { splitDateTime } from "../helpers/SplitDateTime"; import ListUserByWhatsappQueuesService from "../services/UserServices/ListUserByWhatsappQueuesService"; import { json } from "sequelize"; import { getSettingValue } from "../helpers/WhaticketSettings"; -import { setBotInfo } from "../helpers/SetBotInfo" +import { setBotInfo } from "../helpers/SetBotInfo"; type IndexQuery = { searchParam: string; @@ -275,7 +275,7 @@ export const update = async ( let user: any = await UpdateUserService({ userData, userId }); - await setBotInfo(user) + await setBotInfo(user); if (user) { const { id, name } = user; @@ -305,6 +305,8 @@ export const remove = async ( await DeleteUserService(userId); + del(`user:${userId}`); + const io = getIO(); io.emit("user", { action: "delete", @@ -324,5 +326,3 @@ export const remove = async ( return res.status(200).json({ message: "User deleted" }); }; - - diff --git a/backend/src/helpers/RedisClient.ts b/backend/src/helpers/RedisClient.ts index c1cb419..e218e85 100644 --- a/backend/src/helpers/RedisClient.ts +++ b/backend/src/helpers/RedisClient.ts @@ -23,7 +23,7 @@ export async function get(key: string, value?: string) { if (keys.length > 0) { for (const key of keys) { const val = await redis.get(key); - if (value == val) return value; + if (value == val) return value; } } return null; @@ -33,6 +33,10 @@ export async function get(key: string, value?: string) { } } +export async function del(key: string) { + await redis.del(key); +} + export async function clearAllKeys(...keys: string[]) { for (const key of keys) { // Retrieve all keys matching the pattern '*'