2022-01-06 01:26:15 +00:00
|
|
|
import * as Yup from "yup";
|
|
|
|
import { Request, Response } from "express";
|
|
|
|
import { getIO } from "../libs/socket";
|
|
|
|
|
|
|
|
import ListContactsService from "../services/ContactServices/ListContactsService";
|
|
|
|
import CreateContactService from "../services/ContactServices/CreateContactService";
|
|
|
|
import ShowContactService from "../services/ContactServices/ShowContactService";
|
|
|
|
import UpdateContactService from "../services/ContactServices/UpdateContactService";
|
|
|
|
import DeleteContactService from "../services/ContactServices/DeleteContactService";
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
import CheckContactNumber from "../services/WbotServices/CheckNumber";
|
2022-01-06 01:26:15 +00:00
|
|
|
import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
|
|
|
|
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
|
|
|
import AppError from "../errors/AppError";
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
import {
|
|
|
|
searchContactCache,
|
|
|
|
insertContactsCache,
|
|
|
|
escapeCharCache
|
|
|
|
} from "../helpers/ContactsCache";
|
2022-11-11 22:44:27 +00:00
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
import { off } from "process";
|
2024-02-26 14:18:01 +00:00
|
|
|
import GetContactService from "../services/ContactServices/GetContactService"
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
type IndexQuery = {
|
|
|
|
searchParam: string;
|
|
|
|
pageNumber: string;
|
|
|
|
};
|
|
|
|
|
2024-02-26 14:18:01 +00:00
|
|
|
type IndexGetContactQuery = {
|
|
|
|
name: string;
|
|
|
|
number: string;
|
|
|
|
};
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface ExtraInfo {
|
|
|
|
name: string;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
interface ContactData {
|
|
|
|
name: string;
|
|
|
|
number: string;
|
|
|
|
email?: string;
|
|
|
|
extraInfo?: ExtraInfo[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
2022-10-25 14:16:36 +00:00
|
|
|
let { searchParam, pageNumber } = req.query as IndexQuery;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log("PAGE NUMBER CONTACT: ", pageNumber);
|
2022-12-21 00:28:41 +00:00
|
|
|
|
2022-12-22 12:15:30 +00:00
|
|
|
if (pageNumber === undefined || pageNumber.trim().length == 0) {
|
2023-08-14 09:48:39 +00:00
|
|
|
pageNumber = "1";
|
2022-12-21 00:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
// TEST DEL
|
2022-11-08 23:16:39 +00:00
|
|
|
if (searchParam && searchParam.trim().length > 0 && process.env.CACHE) {
|
2022-10-25 14:16:36 +00:00
|
|
|
try {
|
|
|
|
const offset = 20 * (+pageNumber - 1);
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
searchParam = searchParam.replace(/\s+/g, " ").trim().toLowerCase();
|
2022-12-21 00:28:41 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
const data = await searchContactCache(searchParam, offset, 20);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-11-11 22:44:27 +00:00
|
|
|
if (data) {
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log("QUERY CONTACTS FROM CACHE SEARCH PARAM: ", searchParam);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log("QUERY CONTACTS FROM CACHE QUERY LENGTH: ", data.length);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
return res.json({
|
|
|
|
contacts: data,
|
|
|
|
count: data.length,
|
|
|
|
hasMore: data.length > 0 ? true : false
|
|
|
|
});
|
2022-10-25 14:16:36 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log(
|
|
|
|
"There was an error on search ContactController.ts search cache: ",
|
|
|
|
error
|
|
|
|
);
|
2022-10-25 14:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log("QUERY CONTACTS FROM DATABASE SEARCH PARAM: ", searchParam);
|
2023-01-05 18:22:25 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
const { contacts, count, hasMore } = await ListContactsService({
|
|
|
|
searchParam,
|
|
|
|
pageNumber
|
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
return res.json({ contacts, count, hasMore });
|
|
|
|
};
|
|
|
|
|
2024-02-26 14:18:01 +00:00
|
|
|
export const getContact = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const { name, number } = req.body as IndexGetContactQuery;
|
|
|
|
|
|
|
|
const contact = await GetContactService({
|
|
|
|
name,
|
|
|
|
number
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json(contact);
|
|
|
|
};
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const newContact: ContactData = req.body;
|
|
|
|
newContact.number = newContact.number.replace("-", "").replace(" ", "");
|
|
|
|
|
|
|
|
const schema = Yup.object().shape({
|
|
|
|
name: Yup.string().required(),
|
|
|
|
number: Yup.string()
|
|
|
|
.required()
|
|
|
|
.matches(/^\d+$/, "Invalid number format. Only numbers is allowed.")
|
2024-01-29 14:34:56 +00:00
|
|
|
// .matches(/^55\d+$/, "The number must start with 55.")
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await schema.validate(newContact);
|
2022-12-22 12:15:30 +00:00
|
|
|
} catch (err: any) {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError(err.message);
|
|
|
|
}
|
|
|
|
|
2024-01-29 14:34:56 +00:00
|
|
|
newContact.number = addStartPhoneNumber(newContact.number);
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
const validNumber = await CheckIsValidContact(newContact.number);
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
// const validNumber: any = await CheckContactNumber(newContact.number)
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
if (!validNumber) {
|
2023-02-07 15:47:40 +00:00
|
|
|
throw new AppError("ERR_WAPP_CHECK_CONTACT");
|
|
|
|
}
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const profilePicUrl = await GetProfilePicUrl(validNumber);
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log("xxxxxxxxxxx profilePicUrl: ", profilePicUrl);
|
2023-04-04 12:30:28 +00:00
|
|
|
|
|
|
|
// console.log(`newContact.name: ${newContact.name}\n
|
|
|
|
// newContact.number: ${newContact.number}\n
|
|
|
|
// newContact.email: ${newContact.email}\n
|
|
|
|
// newContact.extraInfo: ${newContact.extraInfo}`)
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
let name = newContact.name;
|
|
|
|
let number = validNumber;
|
|
|
|
let email = newContact.email;
|
|
|
|
let extraInfo = newContact.extraInfo;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const contact = await CreateContactService({
|
|
|
|
name,
|
|
|
|
number,
|
|
|
|
email,
|
2023-02-07 15:47:40 +00:00
|
|
|
profilePicUrl: profilePicUrl,
|
2023-08-14 09:48:39 +00:00
|
|
|
extraInfo
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("contact", {
|
|
|
|
action: "create",
|
|
|
|
contact
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json(contact);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const show = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { contactId } = req.params;
|
|
|
|
|
|
|
|
const contact = await ShowContactService(contactId);
|
|
|
|
|
|
|
|
return res.status(200).json(contact);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const update = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const contactData: ContactData = req.body;
|
|
|
|
|
|
|
|
const schema = Yup.object().shape({
|
|
|
|
name: Yup.string(),
|
2024-01-29 14:34:56 +00:00
|
|
|
number: Yup.string().matches(
|
|
|
|
/^\d+$/,
|
|
|
|
"Invalid number format. Only numbers is allowed."
|
|
|
|
)
|
|
|
|
// .matches(/^55\d+$/, "The number must start with 55.")
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await schema.validate(contactData);
|
2022-12-22 12:15:30 +00:00
|
|
|
} catch (err: any) {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError(err.message);
|
|
|
|
}
|
|
|
|
|
2024-01-29 14:34:56 +00:00
|
|
|
contactData.number = addStartPhoneNumber(contactData.number);
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await CheckIsValidContact(contactData.number);
|
|
|
|
|
|
|
|
const { contactId } = req.params;
|
|
|
|
|
|
|
|
const contact = await UpdateContactService({ contactData, contactId });
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("contact", {
|
|
|
|
action: "update",
|
|
|
|
contact
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json(contact);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const remove = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const { contactId } = req.params;
|
|
|
|
|
|
|
|
await DeleteContactService(contactId);
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("contact", {
|
|
|
|
action: "delete",
|
|
|
|
contactId
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json({ message: "Contact deleted" });
|
|
|
|
};
|
2022-11-07 20:49:44 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
export const contacsBulkInsertOnQueue = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2022-11-07 20:49:44 +00:00
|
|
|
// console.log('THE BODY: ', req.body)
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
const {
|
|
|
|
adminId,
|
|
|
|
identifier,
|
|
|
|
queueStatus,
|
|
|
|
file,
|
|
|
|
contacts_inserted,
|
|
|
|
campaign
|
|
|
|
} = req.body;
|
2022-11-07 20:49:44 +00:00
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("contactsBulkInsertOnQueueStatus", {
|
|
|
|
action: "update",
|
|
|
|
insertOnQueue: {
|
|
|
|
adminId: adminId,
|
|
|
|
identifier: identifier,
|
|
|
|
queueStatus: queueStatus,
|
2023-08-14 09:48:39 +00:00
|
|
|
file: file,
|
|
|
|
campaign
|
2022-11-07 20:49:44 +00:00
|
|
|
}
|
|
|
|
});
|
2022-12-21 00:28:41 +00:00
|
|
|
|
2022-11-11 22:44:27 +00:00
|
|
|
if (process.env.CACHE && contacts_inserted) {
|
2023-08-14 09:48:39 +00:00
|
|
|
await insertContactsCache(contacts_inserted);
|
2022-11-11 22:44:27 +00:00
|
|
|
}
|
2022-11-07 20:49:44 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
return res.status(200).json({ message: "ok" });
|
2022-11-07 20:49:44 +00:00
|
|
|
};
|
2024-01-29 14:34:56 +00:00
|
|
|
|
|
|
|
function addStartPhoneNumber(phoneNumber: string) {
|
|
|
|
const regex = /^55/;
|
|
|
|
|
|
|
|
if (!regex.test(phoneNumber)) {
|
|
|
|
phoneNumber = "55" + phoneNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
return phoneNumber;
|
|
|
|
}
|