feat: Optimize user search via Redis for backend access

feat-scaling-ticket-remote-creation
adriano 2024-02-21 11:23:51 -03:00
parent 9370931737
commit 5abab2ef05
5 changed files with 180 additions and 13 deletions

View File

@ -17,17 +17,32 @@ export async function get(key: string) {
return JSON.parse(value); return JSON.parse(value);
} }
export async function findByContain() { export async function findByContain(
key: string,
keyName: string,
substring: string
) {
// const keys = await redis.keys("*" + substring + "*"); // const keys = await redis.keys("*" + substring + "*");
const keys = await redis.keys("user:*"); // const keys = await redis.keys("user:*");
const keys = await redis.keys(key);
const results: any[] = []; const results: any[] = [];
for (const key of keys) { for (const key of keys) {
const value = await redis.get(key); const value = await redis.get(key);
if (value) { if (value) {
results.push(JSON.parse(value)); const obj = JSON.parse(value);
if (
substring
?.trim()
?.toLowerCase()
.includes(obj[keyName]?.trim()?.toLowerCase())
) {
results.push(obj);
}
} }
} }
return results; return results;
} }

View File

@ -48,7 +48,7 @@ gracefulShutdown(server);
for (const user of users) { for (const user of users) {
const { id, name } = user; const { id, name } = user;
await set(`user:${id}:${name.toLowerCase()}`, { id, name }); await set(`user:${id}`, { id, name });
} }
loadSettings(); loadSettings();

View File

@ -761,12 +761,9 @@ const handleMessage = async (
// Transfer to agent // Transfer to agent
if (!msg.fromMe) { if (!msg.fromMe) {
const listUser = await findByContain(); const filteredUsers = await findByContain("user:*", "name", msg?.body);
let filteredUsers = listUser.filter(user =>
msg?.body?.trim()?.toLowerCase().includes(user.name)
);
if (filteredUsers && filteredUsers.length > 0) { if (filteredUsers && filteredUsers.length > 0) {
const whatsappQueues = await ListWhatsappQueuesByUserQueue( const whatsappQueues = await ListWhatsappQueuesByUserQueue(
+filteredUsers[0].id +filteredUsers[0].id
@ -781,11 +778,11 @@ const handleMessage = async (
+filteredUsers[0].id, +filteredUsers[0].id,
ticket, ticket,
+obj.queueId +obj.queueId
); );
await botSendMessage( await botSendMessage(
ticket, ticket,
`Você foi transferido para falar com o agente ${filteredUsers[0].name}` `Você foi transferido para falar com o agente ${filteredUsers[0].name}, aguarde.`
); );
} }

152
package-lock.json generated
View File

@ -2,5 +2,155 @@
"name": "whaticket", "name": "whaticket",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": {} "packages": {
"": {
"dependencies": {
"natural": "^6.10.4"
}
},
"node_modules/afinn-165": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/afinn-165/-/afinn-165-1.0.4.tgz",
"integrity": "sha512-7+Wlx3BImrK0HiG6y3lU4xX7SpBPSSu8T9iguPMlaueRFxjbYwAQrp9lqZUuFikqKbd/en8lVREILvP2J80uJA==",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/wooorm"
}
},
"node_modules/afinn-165-financialmarketnews": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/afinn-165-financialmarketnews/-/afinn-165-financialmarketnews-3.0.0.tgz",
"integrity": "sha512-0g9A1S3ZomFIGDTzZ0t6xmv4AuokBvBmpes8htiyHpH7N4xDmvSQL6UxL/Zcs2ypRb3VwgCscaD8Q3zEawKYhw==",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/wooorm"
}
},
"node_modules/apparatus": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/apparatus/-/apparatus-0.0.10.tgz",
"integrity": "sha512-KLy/ugo33KZA7nugtQ7O0E1c8kQ52N3IvD/XgIh4w/Nr28ypfkwDfA67F1ev4N1m5D+BOk1+b2dEJDfpj/VvZg==",
"dependencies": {
"sylvester": ">= 0.0.8"
},
"engines": {
"node": ">=0.2.6"
}
},
"node_modules/natural": {
"version": "6.10.4",
"resolved": "https://registry.npmjs.org/natural/-/natural-6.10.4.tgz",
"integrity": "sha512-PpqKTyRwNsYrTQ7/2El9X96pt7OCKynwGahl9oIBQEz9d71jxnHQ0KkuWg7xOC7ZvG0gRSkJ56q7Ygr5zfof2w==",
"dependencies": {
"afinn-165": "^1.0.2",
"afinn-165-financialmarketnews": "^3.0.0",
"apparatus": "^0.0.10",
"safe-stable-stringify": "^2.2.0",
"stopwords-iso": "^1.1.0",
"sylvester": "^0.0.12",
"underscore": "^1.9.1",
"wordnet-db": "^3.1.11"
},
"engines": {
"node": ">=0.4.10"
}
},
"node_modules/safe-stable-stringify": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.4.3.tgz",
"integrity": "sha512-e2bDA2WJT0wxseVd4lsDP4+3ONX6HpMXQa1ZhFQ7SU+GjvORCmShbCMltrtIDfkYhVHrOcPtj+KhmDBdPdZD1g==",
"engines": {
"node": ">=10"
}
},
"node_modules/stopwords-iso": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/stopwords-iso/-/stopwords-iso-1.1.0.tgz",
"integrity": "sha512-I6GPS/E0zyieHehMRPQcqkiBMJKGgLta+1hREixhoLPqEA0AlVFiC43dl8uPpmkkeRdDMzYRWFWk5/l9x7nmNg==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/sylvester": {
"version": "0.0.12",
"resolved": "https://registry.npmjs.org/sylvester/-/sylvester-0.0.12.tgz",
"integrity": "sha512-SzRP5LQ6Ts2G5NyAa/jg16s8e3R7rfdFjizy1zeoecYWw+nGL+YA1xZvW/+iJmidBGSdLkuvdwTYEyJEb+EiUw==",
"engines": {
"node": ">=0.2.6"
}
},
"node_modules/underscore": {
"version": "1.13.6",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.6.tgz",
"integrity": "sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A=="
},
"node_modules/wordnet-db": {
"version": "3.1.14",
"resolved": "https://registry.npmjs.org/wordnet-db/-/wordnet-db-3.1.14.tgz",
"integrity": "sha512-zVyFsvE+mq9MCmwXUWHIcpfbrHHClZWZiVOzKSxNJruIcFn2RbY55zkhiAMMxM8zCVSmtNiViq8FsAZSFpMYag==",
"engines": {
"node": ">=0.6.0"
}
}
},
"dependencies": {
"afinn-165": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/afinn-165/-/afinn-165-1.0.4.tgz",
"integrity": "sha512-7+Wlx3BImrK0HiG6y3lU4xX7SpBPSSu8T9iguPMlaueRFxjbYwAQrp9lqZUuFikqKbd/en8lVREILvP2J80uJA=="
},
"afinn-165-financialmarketnews": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/afinn-165-financialmarketnews/-/afinn-165-financialmarketnews-3.0.0.tgz",
"integrity": "sha512-0g9A1S3ZomFIGDTzZ0t6xmv4AuokBvBmpes8htiyHpH7N4xDmvSQL6UxL/Zcs2ypRb3VwgCscaD8Q3zEawKYhw=="
},
"apparatus": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/apparatus/-/apparatus-0.0.10.tgz",
"integrity": "sha512-KLy/ugo33KZA7nugtQ7O0E1c8kQ52N3IvD/XgIh4w/Nr28ypfkwDfA67F1ev4N1m5D+BOk1+b2dEJDfpj/VvZg==",
"requires": {
"sylvester": ">= 0.0.8"
}
},
"natural": {
"version": "6.10.4",
"resolved": "https://registry.npmjs.org/natural/-/natural-6.10.4.tgz",
"integrity": "sha512-PpqKTyRwNsYrTQ7/2El9X96pt7OCKynwGahl9oIBQEz9d71jxnHQ0KkuWg7xOC7ZvG0gRSkJ56q7Ygr5zfof2w==",
"requires": {
"afinn-165": "^1.0.2",
"afinn-165-financialmarketnews": "^3.0.0",
"apparatus": "^0.0.10",
"safe-stable-stringify": "^2.2.0",
"stopwords-iso": "^1.1.0",
"sylvester": "^0.0.12",
"underscore": "^1.9.1",
"wordnet-db": "^3.1.11"
}
},
"safe-stable-stringify": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.4.3.tgz",
"integrity": "sha512-e2bDA2WJT0wxseVd4lsDP4+3ONX6HpMXQa1ZhFQ7SU+GjvORCmShbCMltrtIDfkYhVHrOcPtj+KhmDBdPdZD1g=="
},
"stopwords-iso": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/stopwords-iso/-/stopwords-iso-1.1.0.tgz",
"integrity": "sha512-I6GPS/E0zyieHehMRPQcqkiBMJKGgLta+1hREixhoLPqEA0AlVFiC43dl8uPpmkkeRdDMzYRWFWk5/l9x7nmNg=="
},
"sylvester": {
"version": "0.0.12",
"resolved": "https://registry.npmjs.org/sylvester/-/sylvester-0.0.12.tgz",
"integrity": "sha512-SzRP5LQ6Ts2G5NyAa/jg16s8e3R7rfdFjizy1zeoecYWw+nGL+YA1xZvW/+iJmidBGSdLkuvdwTYEyJEb+EiUw=="
},
"underscore": {
"version": "1.13.6",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.6.tgz",
"integrity": "sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A=="
},
"wordnet-db": {
"version": "3.1.14",
"resolved": "https://registry.npmjs.org/wordnet-db/-/wordnet-db-3.1.14.tgz",
"integrity": "sha512-zVyFsvE+mq9MCmwXUWHIcpfbrHHClZWZiVOzKSxNJruIcFn2RbY55zkhiAMMxM8zCVSmtNiViq8FsAZSFpMYag=="
}
}
} }

5
package.json 100644
View File

@ -0,0 +1,5 @@
{
"dependencies": {
"natural": "^6.10.4"
}
}