2024-02-05 15:29:49 +00:00
|
|
|
const Redis = require("ioredis");
|
|
|
|
const redis = new Redis(process.env.REDIS_URI);
|
|
|
|
|
|
|
|
type WhatsappData = {
|
2024-02-07 19:36:45 +00:00
|
|
|
whatsappId: string | number;
|
|
|
|
contactId: string | number;
|
2024-02-05 15:29:49 +00:00
|
|
|
identifier: string;
|
|
|
|
value?: string;
|
|
|
|
};
|
|
|
|
|
2024-02-07 19:36:45 +00:00
|
|
|
export async function set(key: string, value: string, expire: boolean = false) {
|
2024-02-05 15:29:49 +00:00
|
|
|
await redis.set(key, JSON.stringify(value));
|
2024-02-07 19:36:45 +00:00
|
|
|
|
|
|
|
if (expire) await redis.expire(key, 300);
|
2024-02-05 15:29:49 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 19:36:45 +00:00
|
|
|
export async function get(key: string) {
|
2024-02-05 15:29:49 +00:00
|
|
|
const value: any = await redis.get(key);
|
|
|
|
return JSON.parse(value);
|
|
|
|
}
|
2024-02-07 19:36:45 +00:00
|
|
|
export async function del(key: string) {
|
|
|
|
await redis.del(key);
|
|
|
|
}
|
2024-02-05 15:29:49 +00:00
|
|
|
|
|
|
|
export async function createObject({
|
|
|
|
whatsappId,
|
|
|
|
contactId,
|
|
|
|
identifier,
|
|
|
|
value
|
|
|
|
}: WhatsappData) {
|
|
|
|
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
|
|
|
const result = await redis.hmset(
|
|
|
|
key,
|
|
|
|
"whatsappId",
|
|
|
|
whatsappId,
|
|
|
|
"contactId",
|
|
|
|
contactId,
|
|
|
|
"identifier",
|
|
|
|
identifier,
|
|
|
|
"value",
|
|
|
|
value
|
|
|
|
);
|
|
|
|
|
|
|
|
await redis.expire(key, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateObject({
|
|
|
|
whatsappId,
|
|
|
|
contactId,
|
|
|
|
identifier,
|
|
|
|
value
|
|
|
|
}: WhatsappData) {
|
|
|
|
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
|
|
|
|
|
|
|
await redis.hset(key, "value", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function findObject(
|
2024-02-07 19:36:45 +00:00
|
|
|
whatsappId: string | number,
|
|
|
|
contactId: string | number,
|
2024-02-05 15:29:49 +00:00
|
|
|
identifier: string
|
|
|
|
) {
|
|
|
|
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
|
|
|
const result = await redis.hmget(
|
|
|
|
key,
|
|
|
|
"whatsappId",
|
|
|
|
"contactId",
|
|
|
|
"identifier",
|
|
|
|
"value"
|
|
|
|
);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteObject(
|
|
|
|
whatsappId: string,
|
|
|
|
contactId: string,
|
|
|
|
identifier: string
|
|
|
|
) {
|
|
|
|
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
|
|
|
const deletedCount = await redis.del(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteKeysWithPattern(
|
|
|
|
whatsappId: string,
|
|
|
|
contactId: string
|
|
|
|
) {
|
|
|
|
const pattern = `whatsappId:${whatsappId}:contactId:${contactId}:*`;
|
|
|
|
|
|
|
|
let cursor = "0";
|
|
|
|
|
|
|
|
do {
|
|
|
|
const [newCursor, keys] = await redis.scan(
|
|
|
|
cursor,
|
|
|
|
"MATCH",
|
|
|
|
pattern,
|
|
|
|
"COUNT",
|
|
|
|
"100"
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
await redis.del(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = newCursor;
|
|
|
|
} while (cursor !== "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getHashesWithPattern(
|
|
|
|
whatsappId: string,
|
|
|
|
contactId: string
|
|
|
|
) {
|
|
|
|
const pattern = `whatsappId:${whatsappId}:contactId:${contactId}:*`;
|
|
|
|
|
|
|
|
let cursor = "0";
|
|
|
|
const hashes = [];
|
|
|
|
|
|
|
|
do {
|
|
|
|
const [newCursor, keys] = await redis.scan(
|
|
|
|
cursor,
|
|
|
|
"MATCH",
|
|
|
|
pattern,
|
|
|
|
"COUNT",
|
|
|
|
"100"
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
const hash = await redis.hgetall(key);
|
|
|
|
hashes.push(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = newCursor;
|
|
|
|
} while (cursor !== "0");
|
|
|
|
|
|
|
|
return hashes;
|
|
|
|
}
|