feat: Add new function to clear user cache and include phone number field for official WhatsApp

feat-scaling-ticket-remote-creation
adriano 2024-03-04 15:53:50 -03:00
parent 8ca5b4503a
commit 6c5b89fd28
5 changed files with 49 additions and 9 deletions

View File

@ -54,6 +54,7 @@ interface WhatsappData {
isDefault?: boolean; isDefault?: boolean;
isOfficial?: boolean; isOfficial?: boolean;
phoneNumberId?: string; phoneNumberId?: string;
number?: string;
wabaId?: string; wabaId?: string;
} }
@ -322,7 +323,8 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
urlApi, urlApi,
phoneNumberId, phoneNumberId,
wabaId, wabaId,
isOfficial isOfficial,
number
}: WhatsappData = req.body; }: WhatsappData = req.body;
if (req.user.profile !== "master") { if (req.user.profile !== "master") {
@ -333,7 +335,8 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
urlApi, urlApi,
isOfficial, isOfficial,
phoneNumberId, phoneNumberId,
wabaId wabaId,
number
}); });
if (invalid) { if (invalid) {
@ -346,6 +349,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
} else if (!isOfficial) { } else if (!isOfficial) {
phoneNumberId = ""; phoneNumberId = "";
wabaId = ""; wabaId = "";
number = "";
} }
let invalidPhoneName = validatePhoneName(name); let invalidPhoneName = validatePhoneName(name);
@ -365,7 +369,8 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
queueIds, queueIds,
phoneNumberId, phoneNumberId,
wabaId, wabaId,
isOfficial isOfficial,
number
}); });
console.log("whatsapp.id: ", whatsapp.id); console.log("whatsapp.id: ", whatsapp.id);
@ -549,18 +554,22 @@ interface WhatsappDataValidate {
isOfficial?: boolean; isOfficial?: boolean;
phoneNumberId?: string; phoneNumberId?: string;
wabaId?: string; wabaId?: string;
number?: string;
} }
const checkWhatsAppData = ({ const checkWhatsAppData = ({
urlApi, urlApi,
isOfficial, isOfficial,
phoneNumberId, phoneNumberId,
wabaId wabaId,
number
}: WhatsappDataValidate) => { }: WhatsappDataValidate) => {
if (isOfficial && (!phoneNumberId || phoneNumberId.trim() == "")) { if (isOfficial && (!phoneNumberId || phoneNumberId.trim() == "")) {
return { message: "Phone number Id is required!" }; return { message: "Phone number Id is required!" };
} else if (isOfficial && (!wabaId || wabaId.trim() == "")) { } else if (isOfficial && (!wabaId || wabaId.trim() == "")) {
return { message: "WABA ID is required!" }; return { message: "WABA ID is required!" };
} else if (isOfficial && (!number || number.trim() == "")) {
return { message: "Phone number is required!" };
} else if (!isOfficial && (!urlApi || urlApi.trim() == "")) { } else if (!isOfficial && (!urlApi || urlApi.trim() == "")) {
return { message: "urlApi is required!" }; return { message: "urlApi is required!" };
} }

View File

@ -17,6 +17,18 @@ export async function get(key: string) {
return JSON.parse(value); return JSON.parse(value);
} }
export async function clearAllKeys() {
// Retrieve all keys matching the pattern '*'
const keys = await redis.keys("user:*");
// If there are keys, delete them
if (keys.length > 0) {
console.log('keys: ', keys)
await redis.del(...keys);
}
}
export async function findByContain( export async function findByContain(
key: string, key: string,
keyName: string, keyName: string,

View File

@ -23,7 +23,7 @@ import fs from "fs";
import dir from "path"; import dir from "path";
import { getSettingValue } from "./helpers/WhaticketSettings"; import { getSettingValue } from "./helpers/WhaticketSettings";
import loadSettings from "./helpers/LoadSettings"; import loadSettings from "./helpers/LoadSettings";
import { set } from "./helpers/RedisClient"; import { clearAllKeys, set } from "./helpers/RedisClient";
const server = app.listen(process.env.PORT, () => { const server = app.listen(process.env.PORT, () => {
logger.info(`Server started on port: ${process.env.PORT}`); logger.info(`Server started on port: ${process.env.PORT}`);
@ -44,6 +44,8 @@ gracefulShutdown(server);
(async () => { (async () => {
console.log("os.tmpdir(): ", os.tmpdir()); console.log("os.tmpdir(): ", os.tmpdir());
await clearAllKeys();
const users = await User.findAll(); const users = await User.findAll();
for (const user of users) { for (const user of users) {

View File

@ -16,6 +16,7 @@ interface Request {
phoneNumberId?: string; phoneNumberId?: string;
wabaId?: string; wabaId?: string;
isOfficial?: boolean; isOfficial?: boolean;
number?: string;
} }
interface Response { interface Response {
@ -34,7 +35,8 @@ const CreateWhatsAppService = async ({
isDefault = false, isDefault = false,
isOfficial = false, isOfficial = false,
phoneNumberId, phoneNumberId,
wabaId wabaId,
number
}: Request): Promise<Response> => { }: Request): Promise<Response> => {
try { try {
const schema = Yup.object().shape({ const schema = Yup.object().shape({
@ -98,6 +100,7 @@ const CreateWhatsAppService = async ({
phoneNumberId, phoneNumberId,
wabaId, wabaId,
isOfficial, isOfficial,
number,
classification classification
}, },
{ include: ["queues"] } { include: ["queues"] }

View File

@ -70,6 +70,7 @@ const WhatsAppModal = ({ open, onClose, whatsAppId, whatsAppOfficial }) => {
farewellMessage: '', farewellMessage: '',
isDefault: false, isDefault: false,
isOfficial: false, isOfficial: false,
number: '',
phoneNumberId: '', phoneNumberId: '',
wabaId: '' wabaId: ''
} }
@ -109,6 +110,7 @@ const WhatsAppModal = ({ open, onClose, whatsAppId, whatsAppOfficial }) => {
if (!isOfficial) { if (!isOfficial) {
values.phoneNumberId = '' values.phoneNumberId = ''
values.wabaId = '' values.wabaId = ''
values.number = ''
} }
@ -276,6 +278,18 @@ const WhatsAppModal = ({ open, onClose, whatsAppId, whatsAppOfficial }) => {
margin="dense" margin="dense"
className={classes.textField} className={classes.textField}
/> />
<Field
as={TextField}
label="Phone number"
autoFocus
name="number"
error={touched.name && Boolean(errors.name)}
helperText={touched.name && errors.name}
variant="outlined"
margin="dense"
className={classes.textField}
/>
</div> </div>