mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 11:49:19 +00:00
changed ticket creation and list to typescript
This commit is contained in:
38
backend/src/controllers/ImportPhoneContactsController.ts
Normal file
38
backend/src/controllers/ImportPhoneContactsController.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
// const Contact = require("../models/Contact");
|
||||
// const Whatsapp = require("../models/Whatsapp");
|
||||
// const { getIO } = require("../libs/socket");
|
||||
// const { getWbot, initWbot } = require("../libs/wbot");
|
||||
|
||||
// exports.store = async (req, res, next) => {
|
||||
// const defaultWhatsapp = await Whatsapp.findOne({
|
||||
// where: { default: true }
|
||||
// });
|
||||
|
||||
// if (!defaultWhatsapp) {
|
||||
// return res
|
||||
// .status(404)
|
||||
// .json({ error: "No default WhatsApp found. Check Connection page." });
|
||||
// }
|
||||
|
||||
// const io = getIO();
|
||||
// const wbot = getWbot(defaultWhatsapp);
|
||||
|
||||
// let phoneContacts;
|
||||
|
||||
// try {
|
||||
// phoneContacts = await wbot.getContacts();
|
||||
// } catch (err) {
|
||||
// console.log(err);
|
||||
// return res.status(500).json({
|
||||
// error: "Could not check whatsapp contact. Check connection page."
|
||||
// });
|
||||
// }
|
||||
|
||||
// await Promise.all(
|
||||
// phoneContacts.map(async ({ number, name }) => {
|
||||
// await Contact.create({ number, name });
|
||||
// })
|
||||
// );
|
||||
|
||||
// return res.status(200).json({ message: "contacts imported" });
|
||||
// };
|
||||
203
backend/src/controllers/MessageController.ts
Normal file
203
backend/src/controllers/MessageController.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
// const Message = require("../models/Message");
|
||||
// const Contact = require("../models/Contact");
|
||||
// const User = require("../models/User");
|
||||
// const Whatsapp = require("../models/Whatsapp");
|
||||
|
||||
// const Ticket = require("../models/Ticket");
|
||||
// const { getIO } = require("../libs/socket");
|
||||
// const { getWbot } = require("../libs/wbot");
|
||||
// const Sequelize = require("sequelize");
|
||||
|
||||
// const { MessageMedia } = require("whatsapp-web.js");
|
||||
|
||||
// const setMessagesAsRead = async ticket => {
|
||||
// const io = getIO();
|
||||
// const wbot = getWbot(ticket.whatsappId);
|
||||
|
||||
// await Message.update(
|
||||
// { read: true },
|
||||
// {
|
||||
// where: {
|
||||
// ticketId: ticket.id,
|
||||
// read: false,
|
||||
// },
|
||||
// }
|
||||
// );
|
||||
|
||||
// try {
|
||||
// await wbot.sendSeen(`${ticket.contact.number}@c.us`);
|
||||
// } catch (err) {
|
||||
// console.log(
|
||||
// "Could not mark messages as read. Maybe whatsapp session disconnected?"
|
||||
// );
|
||||
// }
|
||||
|
||||
// io.to("notification").emit("ticket", {
|
||||
// action: "updateUnread",
|
||||
// ticketId: ticket.id,
|
||||
// });
|
||||
// };
|
||||
|
||||
// exports.index = async (req, res, next) => {
|
||||
// const { ticketId } = req.params;
|
||||
// const { searchParam = "", pageNumber = 1 } = req.query;
|
||||
|
||||
// const whereCondition = {
|
||||
// body: Sequelize.where(
|
||||
// Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||
// "LIKE",
|
||||
// "%" + searchParam.toLowerCase() + "%"
|
||||
// ),
|
||||
// };
|
||||
|
||||
// const limit = 20;
|
||||
// const offset = limit * (pageNumber - 1);
|
||||
|
||||
// const ticket = await Ticket.findByPk(ticketId, {
|
||||
// include: [
|
||||
// {
|
||||
// model: Contact,
|
||||
// as: "contact",
|
||||
// include: "extraInfo",
|
||||
// attributes: ["id", "name", "number", "profilePicUrl"],
|
||||
// },
|
||||
// {
|
||||
// model: User,
|
||||
// as: "user",
|
||||
// },
|
||||
// ],
|
||||
// });
|
||||
|
||||
// if (!ticket) {
|
||||
// return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
// }
|
||||
|
||||
// await setMessagesAsRead(ticket);
|
||||
|
||||
// const ticketMessages = await ticket.getMessages({
|
||||
// where: whereCondition,
|
||||
// limit,
|
||||
// offset,
|
||||
// order: [["createdAt", "DESC"]],
|
||||
// });
|
||||
|
||||
// const count = await ticket.countMessages();
|
||||
|
||||
// const serializedMessages = ticketMessages.map(message => {
|
||||
// return {
|
||||
// ...message.dataValues,
|
||||
// mediaUrl: `${
|
||||
// message.mediaUrl
|
||||
// ? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
||||
// : ""
|
||||
// }`,
|
||||
// };
|
||||
// });
|
||||
|
||||
// const hasMore = count > offset + ticketMessages.length;
|
||||
|
||||
// return res.json({
|
||||
// messages: serializedMessages.reverse(),
|
||||
// ticket,
|
||||
// count,
|
||||
// hasMore,
|
||||
// });
|
||||
// };
|
||||
|
||||
// exports.store = async (req, res, next) => {
|
||||
// const io = getIO();
|
||||
|
||||
// const { ticketId } = req.params;
|
||||
// const message = req.body;
|
||||
// const media = req.file;
|
||||
// let sentMessage;
|
||||
|
||||
// const ticket = await Ticket.findByPk(ticketId, {
|
||||
// include: [
|
||||
// {
|
||||
// model: Contact,
|
||||
// as: "contact",
|
||||
// attributes: ["number", "name", "profilePicUrl"],
|
||||
// },
|
||||
// ],
|
||||
// });
|
||||
|
||||
// if (!ticket) {
|
||||
// return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
// }
|
||||
|
||||
// if (!ticket.whatsappId) {
|
||||
// const defaultWhatsapp = await Whatsapp.findOne({
|
||||
// where: { default: true },
|
||||
// });
|
||||
|
||||
// if (!defaultWhatsapp) {
|
||||
// return res
|
||||
// .status(404)
|
||||
// .json({ error: "No default WhatsApp found. Check Connection page." });
|
||||
// }
|
||||
|
||||
// await ticket.setWhatsapp(defaultWhatsapp);
|
||||
// }
|
||||
|
||||
// const wbot = getWbot(ticket.whatsappId);
|
||||
|
||||
// try {
|
||||
// if (media) {
|
||||
// const newMedia = MessageMedia.fromFilePath(req.file.path);
|
||||
|
||||
// message.mediaUrl = req.file.filename;
|
||||
// if (newMedia.mimetype) {
|
||||
// message.mediaType = newMedia.mimetype.split("/")[0];
|
||||
// } else {
|
||||
// message.mediaType = "other";
|
||||
// }
|
||||
|
||||
// sentMessage = await wbot.sendMessage(
|
||||
// `${ticket.contact.number}@c.us`,
|
||||
// newMedia
|
||||
// );
|
||||
|
||||
// await ticket.update({ lastMessage: message.mediaUrl });
|
||||
// } else {
|
||||
// sentMessage = await wbot.sendMessage(
|
||||
// `${ticket.contact.number}@c.us`,
|
||||
// message.body
|
||||
// );
|
||||
// await ticket.update({ lastMessage: message.body });
|
||||
// }
|
||||
// } catch (err) {
|
||||
// console.log(
|
||||
// "Could not create whatsapp message. Is session details valid? "
|
||||
// );
|
||||
// }
|
||||
|
||||
// if (sentMessage) {
|
||||
// message.id = sentMessage.id.id;
|
||||
// const newMessage = await ticket.createMessage(message);
|
||||
|
||||
// const serialziedMessage = {
|
||||
// ...newMessage.dataValues,
|
||||
// mediaUrl: `${
|
||||
// message.mediaUrl
|
||||
// ? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
||||
// : ""
|
||||
// }`,
|
||||
// };
|
||||
|
||||
// io.to(ticketId).to("notification").emit("appMessage", {
|
||||
// action: "create",
|
||||
// message: serialziedMessage,
|
||||
// ticket: ticket,
|
||||
// contact: ticket.contact,
|
||||
// });
|
||||
|
||||
// await setMessagesAsRead(ticket);
|
||||
|
||||
// return res.status(200).json({ newMessage, ticket });
|
||||
// }
|
||||
|
||||
// return res
|
||||
// .status(500)
|
||||
// .json({ error: "Cannot sent whatsapp message. Check connection page." });
|
||||
// };
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Request, Response } from "express";
|
||||
import GetDefaultWhatsapp from "../helpers/GetDefaultWhatsapp";
|
||||
import Ticket from "../models/Ticket";
|
||||
import CreateTicketService from "../services/TicketServices/CreateTicketService";
|
||||
import ListTicketsService from "../services/TicketServices/ListTicketsService";
|
||||
// const Sequelize = require("sequelize");
|
||||
// const { startOfDay, endOfDay, parseISO } = require("date-fns");
|
||||
@@ -10,9 +13,7 @@ import ListTicketsService from "../services/TicketServices/ListTicketsService";
|
||||
|
||||
// const { getIO } = require("../libs/socket");
|
||||
|
||||
import Whatsapp from "../models/Whatsapp";
|
||||
import Ticket from "../models/Ticket";
|
||||
import Whatsapp from "../models/Whatsapp";
|
||||
// import FindWhatsAppService from "../services/WhatsappService/FindWhatsAppService";
|
||||
|
||||
type RequestQuery = {
|
||||
searchParam: string;
|
||||
@@ -93,28 +94,17 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
return res.status(200).json({ tickets, count, hasMore });
|
||||
};
|
||||
|
||||
interface TicketData {
|
||||
contactId: number;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
// const io = getIO();
|
||||
|
||||
const defaultWhatsapp = await Whatsapp.findOne({
|
||||
where: { default: true }
|
||||
});
|
||||
const { contactId, status }: TicketData = req.body;
|
||||
|
||||
const ticketData = req.body;
|
||||
|
||||
if (!defaultWhatsapp) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||
}
|
||||
|
||||
const ticket: Ticket = await defaultWhatsapp.$create("ticket", req.body);
|
||||
|
||||
await ticket.$get("contact");
|
||||
|
||||
const wapp = await ticket.$get("whatsapp");
|
||||
|
||||
const tickets = await wapp?.$get("tickets");
|
||||
const ticket = await CreateTicketService({ contactId, status });
|
||||
|
||||
// const serializaedTicket = { ...ticket.dataValues, contact: contact };
|
||||
|
||||
@@ -123,7 +113,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
// ticket: serializaedTicket
|
||||
// });
|
||||
|
||||
return res.status(200).json({ ticket });
|
||||
return res.status(200).json(ticket);
|
||||
};
|
||||
|
||||
// export const update = (req: Request, res: Response): Promise<Response> => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response } from "express";
|
||||
|
||||
import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
|
||||
import CheckSettingsHelper from "../helpers/CheckSettings";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
import CreateUserService from "../services/UserServices/CreateUserService";
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Request, Response } from "express";
|
||||
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
||||
import DeleteWhatsApprService from "../services/WhatsappService/DeleteWhatsApprService";
|
||||
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
|
||||
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
||||
import FindWhatsAppService from "../services/WhatsappService/FindWhatsAppService";
|
||||
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
|
||||
// import Yup from "yup";
|
||||
// import Whatsapp from "../models/Whatsapp";
|
||||
@@ -53,7 +53,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { whatsappId } = req.params;
|
||||
|
||||
const whatsapp = await ShowWhatsAppService(whatsappId);
|
||||
const whatsapp = await FindWhatsAppService(whatsappId);
|
||||
|
||||
return res.status(200).json(whatsapp);
|
||||
};
|
||||
|
||||
32
backend/src/controllers/WhatsAppSessionController.ts
Normal file
32
backend/src/controllers/WhatsAppSessionController.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// const Whatsapp = require("../models/Whatsapp");
|
||||
// const { getIO } = require("../libs/socket");
|
||||
// const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
|
||||
// const wbotMessageListener = require("../services/wbotMessageListener");
|
||||
// const wbotMonitor = require("../services/wbotMonitor");
|
||||
|
||||
// exports.show = async (req, res) => {
|
||||
// const { whatsappId } = req.params;
|
||||
// const dbSession = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
// if (!dbSession) {
|
||||
// return res.status(200).json({ message: "Session not found" });
|
||||
// }
|
||||
|
||||
// return res.status(200).json(dbSession);
|
||||
// };
|
||||
|
||||
// exports.delete = async (req, res) => {
|
||||
// const { whatsappId } = req.params;
|
||||
|
||||
// const dbSession = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
// if (!dbSession) {
|
||||
// return res.status(404).json({ message: "Session not found" });
|
||||
// }
|
||||
|
||||
// const wbot = getWbot(dbSession.id);
|
||||
|
||||
// wbot.logout();
|
||||
|
||||
// return res.status(200).json({ message: "Session disconnected." });
|
||||
// };
|
||||
Reference in New Issue
Block a user