mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +00:00
finished contact migration to typescript
This commit is contained in:
@@ -6,23 +6,16 @@ import FindContactService from "../services/ContactServices/FindContactService";
|
|||||||
import UpdateContactService from "../services/ContactServices/UpdateContactService";
|
import UpdateContactService from "../services/ContactServices/UpdateContactService";
|
||||||
import DeleteContactService from "../services/ContactServices/DeleteContactService";
|
import DeleteContactService from "../services/ContactServices/DeleteContactService";
|
||||||
|
|
||||||
// const Sequelize = require("sequelize");
|
|
||||||
// const { Op } = require("sequelize");
|
|
||||||
|
|
||||||
// const Contact = require("../models/Contact");
|
|
||||||
// const Whatsapp = require("../models/Whatsapp");
|
|
||||||
// const ContactCustomField = require("../models/ContactCustomField");
|
|
||||||
|
|
||||||
// const { getIO } = require("../libs/socket");
|
// const { getIO } = require("../libs/socket");
|
||||||
// const { getWbot } = require("../libs/wbot");
|
// const { getWbot } = require("../libs/wbot");
|
||||||
|
|
||||||
type RequestQuery = {
|
type IndexQuery = {
|
||||||
searchParam: string;
|
searchParam: string;
|
||||||
pageNumber: string;
|
pageNumber: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const { searchParam, pageNumber } = req.query as RequestQuery;
|
const { searchParam, pageNumber } = req.query as IndexQuery;
|
||||||
|
|
||||||
const { contacts, count, hasMore } = await ListContactsService({
|
const { contacts, count, hasMore } = await ListContactsService({
|
||||||
searchParam,
|
searchParam,
|
||||||
@@ -32,16 +25,26 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
return res.json({ contacts, count, hasMore });
|
return res.json({ contacts, count, hasMore });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface ExtraInfo {
|
||||||
|
name: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
interface ContactData {
|
interface ContactData {
|
||||||
name: string;
|
name: string;
|
||||||
number: string;
|
number: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
|
extraInfo?: ExtraInfo[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const { name, number, email }: ContactData = req.body;
|
const { name, number, email, extraInfo }: ContactData = req.body;
|
||||||
|
|
||||||
const contact = await CreateContactService({ name, number, email });
|
const contact = await CreateContactService({
|
||||||
|
name,
|
||||||
|
number,
|
||||||
|
email,
|
||||||
|
extraInfo
|
||||||
|
});
|
||||||
|
|
||||||
// const defaultWhatsapp = await Whatsapp.findOne({
|
// const defaultWhatsapp = await Whatsapp.findOne({
|
||||||
// where: { default: true }
|
// where: { default: true }
|
||||||
@@ -103,43 +106,13 @@ export const update = async (
|
|||||||
req: Request,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
const contactData = req.body;
|
const contactData: ContactData = req.body;
|
||||||
|
|
||||||
const { contactId } = req.params;
|
const { contactId } = req.params;
|
||||||
|
|
||||||
const contact = await UpdateContactService({ contactData, contactId });
|
const contact = await UpdateContactService({ contactData, contactId });
|
||||||
|
|
||||||
// const io = getIO();
|
// const io = getIO();
|
||||||
// const contact = await Contact.findByPk(contactId, {
|
|
||||||
// include: "extraInfo"
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (!contact) {
|
|
||||||
// return res.status(404).json({ error: "No contact found with this ID" });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (updatedContact.extraInfo) {
|
|
||||||
// await Promise.all(
|
|
||||||
// updatedContact.extraInfo.map(async info => {
|
|
||||||
// await ContactCustomField.upsert({ ...info, contactId: contact.id });
|
|
||||||
// })
|
|
||||||
// );
|
|
||||||
|
|
||||||
// await Promise.all(
|
|
||||||
// contact.extraInfo.map(async oldInfo => {
|
|
||||||
// let stillExists = updatedContact.extraInfo.findIndex(
|
|
||||||
// info => info.id === oldInfo.id
|
|
||||||
// );
|
|
||||||
|
|
||||||
// if (stillExists === -1) {
|
|
||||||
// await ContactCustomField.destroy({ where: { id: oldInfo.id } });
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await contact.update(updatedContact);
|
|
||||||
|
|
||||||
// io.emit("contact", {
|
// io.emit("contact", {
|
||||||
// action: "update",
|
// action: "update",
|
||||||
// contact: contact
|
// contact: contact
|
||||||
@@ -152,19 +125,11 @@ export const remove = async (
|
|||||||
req: Request,
|
req: Request,
|
||||||
res: Response
|
res: Response
|
||||||
): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
// const io = getIO();
|
|
||||||
const { contactId } = req.params;
|
const { contactId } = req.params;
|
||||||
|
|
||||||
await DeleteContactService(contactId);
|
await DeleteContactService(contactId);
|
||||||
|
|
||||||
// const contact = await Contact.findByPk(contactId);
|
// const io = getIO();
|
||||||
|
|
||||||
// if (!contact) {
|
|
||||||
// return res.status(404).json({ error: "No contact found with this ID" });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await contact.destroy();
|
|
||||||
|
|
||||||
// io.emit("contact", {
|
// io.emit("contact", {
|
||||||
// action: "delete",
|
// action: "delete",
|
||||||
// contactId: contactId
|
// contactId: contactId
|
||||||
|
|||||||
@@ -1,170 +1,170 @@
|
|||||||
const Sequelize = require("sequelize");
|
const Sequelize = require("sequelize");
|
||||||
const { Op } = require("sequelize");
|
const { Op } = require("sequelize");
|
||||||
|
|
||||||
const Contact = require("../models/Contact");
|
const Contact = require("../models/Contact");
|
||||||
const Whatsapp = require("../models/Whatsapp");
|
const Whatsapp = require("../models/Whatsapp");
|
||||||
const ContactCustomField = require("../models/ContactCustomField");
|
const ContactCustomField = require("../models/ContactCustomField");
|
||||||
|
|
||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
const { getWbot } = require("../libs/wbot");
|
const { getWbot } = require("../libs/wbot");
|
||||||
|
|
||||||
exports.index = async (req, res) => {
|
exports.index = async (req, res) => {
|
||||||
const { searchParam = "", pageNumber = 1 } = req.query;
|
const { searchParam = "", pageNumber = 1 } = req.query;
|
||||||
|
|
||||||
const whereCondition = {
|
const whereCondition = {
|
||||||
[Op.or]: [
|
[Op.or]: [
|
||||||
{
|
{
|
||||||
name: Sequelize.where(
|
name: Sequelize.where(
|
||||||
Sequelize.fn("LOWER", Sequelize.col("name")),
|
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||||
"LIKE",
|
"LIKE",
|
||||||
"%" + searchParam.toLowerCase() + "%"
|
"%" + searchParam.toLowerCase() + "%"
|
||||||
),
|
)
|
||||||
},
|
},
|
||||||
{ number: { [Op.like]: `%${searchParam}%` } },
|
{ number: { [Op.like]: `%${searchParam}%` } }
|
||||||
],
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
let limit = 20;
|
let limit = 20;
|
||||||
let offset = limit * (pageNumber - 1);
|
let offset = limit * (pageNumber - 1);
|
||||||
|
|
||||||
const { count, rows: contacts } = await Contact.findAndCountAll({
|
const { count, rows: contacts } = await Contact.findAndCountAll({
|
||||||
where: whereCondition,
|
where: whereCondition,
|
||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
order: [["createdAt", "DESC"]],
|
order: [["createdAt", "DESC"]]
|
||||||
});
|
});
|
||||||
|
|
||||||
const hasMore = count > offset + contacts.length;
|
const hasMore = count > offset + contacts.length;
|
||||||
|
|
||||||
return res.json({ contacts, count, hasMore });
|
return res.json({ contacts, count, hasMore });
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.store = async (req, res) => {
|
exports.store = async (req, res) => {
|
||||||
const defaultWhatsapp = await Whatsapp.findOne({
|
const defaultWhatsapp = await Whatsapp.findOne({
|
||||||
where: { default: true },
|
where: { default: true }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!defaultWhatsapp) {
|
if (!defaultWhatsapp) {
|
||||||
return res
|
return res
|
||||||
.status(404)
|
.status(404)
|
||||||
.json({ error: "No default WhatsApp found. Check Connection page." });
|
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||||
}
|
}
|
||||||
|
|
||||||
const wbot = getWbot(defaultWhatsapp);
|
const wbot = getWbot(defaultWhatsapp);
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const newContact = req.body;
|
const newContact = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const isValidNumber = await wbot.isRegisteredUser(
|
const isValidNumber = await wbot.isRegisteredUser(
|
||||||
`${newContact.number}@c.us`
|
`${newContact.number}@c.us`
|
||||||
);
|
);
|
||||||
if (!isValidNumber) {
|
if (!isValidNumber) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
.json({ error: "The suplied number is not a valid Whatsapp number" });
|
.json({ error: "The suplied number is not a valid Whatsapp number" });
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
error: "Could not check whatsapp contact. Check connection page.",
|
error: "Could not check whatsapp contact. Check connection page."
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const profilePicUrl = await wbot.getProfilePicUrl(
|
const profilePicUrl = await wbot.getProfilePicUrl(
|
||||||
`${newContact.number}@c.us`
|
`${newContact.number}@c.us`
|
||||||
);
|
);
|
||||||
|
|
||||||
const contact = await Contact.create(
|
const contact = await Contact.create(
|
||||||
{ ...newContact, profilePicUrl },
|
{ ...newContact, profilePicUrl },
|
||||||
{
|
{
|
||||||
include: "extraInfo",
|
include: "extraInfo"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
io.emit("contact", {
|
io.emit("contact", {
|
||||||
action: "create",
|
action: "create",
|
||||||
contact: contact,
|
contact: contact
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json(contact);
|
return res.status(200).json(contact);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.show = async (req, res) => {
|
exports.show = async (req, res) => {
|
||||||
const { contactId } = req.params;
|
const { contactId } = req.params;
|
||||||
|
|
||||||
const contact = await Contact.findByPk(contactId, {
|
const contact = await Contact.findByPk(contactId, {
|
||||||
include: "extraInfo",
|
include: "extraInfo",
|
||||||
attributes: ["id", "name", "number", "email"],
|
attributes: ["id", "name", "number", "email"]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!contact) {
|
if (!contact) {
|
||||||
return res.status(404).json({ error: "No contact found with this id." });
|
return res.status(404).json({ error: "No contact found with this id." });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).json(contact);
|
return res.status(200).json(contact);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.update = async (req, res) => {
|
exports.update = async (req, res) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
const updatedContact = req.body;
|
const updatedContact = req.body;
|
||||||
|
|
||||||
const { contactId } = req.params;
|
const { contactId } = req.params;
|
||||||
|
|
||||||
const contact = await Contact.findByPk(contactId, {
|
const contact = await Contact.findByPk(contactId, {
|
||||||
include: "extraInfo",
|
include: "extraInfo"
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!contact) {
|
if (!contact) {
|
||||||
return res.status(404).json({ error: "No contact found with this ID" });
|
return res.status(404).json({ error: "No contact found with this ID" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updatedContact.extraInfo) {
|
if (updatedContact.extraInfo) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
updatedContact.extraInfo.map(async info => {
|
updatedContact.extraInfo.map(async info => {
|
||||||
await ContactCustomField.upsert({ ...info, contactId: contact.id });
|
await ContactCustomField.upsert({ ...info, contactId: contact.id });
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
contact.extraInfo.map(async oldInfo => {
|
contact.extraInfo.map(async oldInfo => {
|
||||||
let stillExists = updatedContact.extraInfo.findIndex(
|
let stillExists = updatedContact.extraInfo.findIndex(
|
||||||
info => info.id === oldInfo.id
|
info => info.id === oldInfo.id
|
||||||
);
|
);
|
||||||
|
|
||||||
if (stillExists === -1) {
|
if (stillExists === -1) {
|
||||||
await ContactCustomField.destroy({ where: { id: oldInfo.id } });
|
await ContactCustomField.destroy({ where: { id: oldInfo.id } });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await contact.update(updatedContact);
|
await contact.update(updatedContact);
|
||||||
|
|
||||||
io.emit("contact", {
|
io.emit("contact", {
|
||||||
action: "update",
|
action: "update",
|
||||||
contact: contact,
|
contact: contact
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json(contact);
|
return res.status(200).json(contact);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.delete = async (req, res) => {
|
exports.delete = async (req, res) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const { contactId } = req.params;
|
const { contactId } = req.params;
|
||||||
|
|
||||||
const contact = await Contact.findByPk(contactId);
|
const contact = await Contact.findByPk(contactId);
|
||||||
|
|
||||||
if (!contact) {
|
if (!contact) {
|
||||||
return res.status(404).json({ error: "No contact found with this ID" });
|
return res.status(404).json({ error: "No contact found with this ID" });
|
||||||
}
|
}
|
||||||
|
|
||||||
await contact.destroy();
|
await contact.destroy();
|
||||||
|
|
||||||
io.emit("contact", {
|
io.emit("contact", {
|
||||||
action: "delete",
|
action: "delete",
|
||||||
contactId: contactId,
|
contactId: contactId
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({ message: "Contact deleted" });
|
return res.status(200).json({ message: "Contact deleted" });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,16 +1,23 @@
|
|||||||
import AppError from "../../errors/AppError";
|
import AppError from "../../errors/AppError";
|
||||||
import Contact from "../../models/Contact";
|
import Contact from "../../models/Contact";
|
||||||
|
|
||||||
|
interface ExtraInfo {
|
||||||
|
name: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
name: string;
|
name: string;
|
||||||
number: string;
|
number: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
|
extraInfo?: ExtraInfo[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const CreateContactService = async ({
|
const CreateContactService = async ({
|
||||||
name,
|
name,
|
||||||
number,
|
number,
|
||||||
email
|
email,
|
||||||
|
extraInfo
|
||||||
}: Request): Promise<Contact> => {
|
}: Request): Promise<Contact> => {
|
||||||
const numberExists = await Contact.findOne({
|
const numberExists = await Contact.findOne({
|
||||||
where: { number }
|
where: { number }
|
||||||
@@ -20,11 +27,17 @@ const CreateContactService = async ({
|
|||||||
throw new AppError("A contact with this number already exists.");
|
throw new AppError("A contact with this number already exists.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const contact = await Contact.create({
|
const contact = await Contact.create(
|
||||||
name,
|
{
|
||||||
number,
|
name,
|
||||||
email
|
number,
|
||||||
});
|
email,
|
||||||
|
extraInfo
|
||||||
|
},
|
||||||
|
{
|
||||||
|
include: ["extraInfo"]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return contact;
|
return contact;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
import AppError from "../../errors/AppError";
|
import AppError from "../../errors/AppError";
|
||||||
import Contact from "../../models/Contact";
|
import Contact from "../../models/Contact";
|
||||||
|
import ContactCustomField from "../../models/ContactCustomField";
|
||||||
|
|
||||||
|
interface ExtraInfo {
|
||||||
|
id?: number;
|
||||||
|
name: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
interface ContactData {
|
interface ContactData {
|
||||||
email?: string;
|
email?: string;
|
||||||
number?: string;
|
number?: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
|
extraInfo?: ExtraInfo[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
@@ -16,21 +23,41 @@ const UpdateContactService = async ({
|
|||||||
contactData,
|
contactData,
|
||||||
contactId
|
contactId
|
||||||
}: Request): Promise<Contact> => {
|
}: Request): Promise<Contact> => {
|
||||||
const { email, name, number } = contactData;
|
const { email, name, number, extraInfo } = contactData;
|
||||||
|
|
||||||
const contact = await Contact.findOne({
|
const contact = await Contact.findOne({
|
||||||
where: { id: contactId },
|
where: { id: contactId },
|
||||||
attributes: ["id", "name", "number", "email"]
|
attributes: ["id", "name", "number", "email"],
|
||||||
|
include: ["extraInfo"]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!contact) {
|
if (!contact) {
|
||||||
throw new AppError("No contact found with this ID.", 404);
|
throw new AppError("No contact found with this ID.", 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (extraInfo) {
|
||||||
|
await Promise.all(
|
||||||
|
extraInfo.map(async info => {
|
||||||
|
await ContactCustomField.upsert({ ...info, contactId: contact.id });
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
contact.extraInfo.map(async oldInfo => {
|
||||||
|
const stillExists = extraInfo.findIndex(info => info.id === oldInfo.id);
|
||||||
|
|
||||||
|
if (stillExists === -1) {
|
||||||
|
await ContactCustomField.destroy({ where: { id: oldInfo.id } });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
await contact.update({
|
await contact.update({
|
||||||
name,
|
name,
|
||||||
number,
|
number,
|
||||||
email
|
email,
|
||||||
|
extraInfo
|
||||||
});
|
});
|
||||||
|
|
||||||
return contact;
|
return contact;
|
||||||
|
|||||||
Reference in New Issue
Block a user