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