mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 20:29:17 +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 { 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";
|
import ListTicketsService from "../services/TicketServices/ListTicketsService";
|
||||||
// const Sequelize = require("sequelize");
|
// const Sequelize = require("sequelize");
|
||||||
// const { startOfDay, endOfDay, parseISO } = require("date-fns");
|
// const { startOfDay, endOfDay, parseISO } = require("date-fns");
|
||||||
@@ -10,9 +13,7 @@ import ListTicketsService from "../services/TicketServices/ListTicketsService";
|
|||||||
|
|
||||||
// const { getIO } = require("../libs/socket");
|
// const { getIO } = require("../libs/socket");
|
||||||
|
|
||||||
import Whatsapp from "../models/Whatsapp";
|
// import FindWhatsAppService from "../services/WhatsappService/FindWhatsAppService";
|
||||||
import Ticket from "../models/Ticket";
|
|
||||||
import Whatsapp from "../models/Whatsapp";
|
|
||||||
|
|
||||||
type RequestQuery = {
|
type RequestQuery = {
|
||||||
searchParam: string;
|
searchParam: string;
|
||||||
@@ -93,28 +94,17 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
return res.status(200).json({ tickets, count, hasMore });
|
return res.status(200).json({ tickets, count, hasMore });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface TicketData {
|
||||||
|
contactId: number;
|
||||||
|
status: string;
|
||||||
|
}
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
// const io = getIO();
|
// const io = getIO();
|
||||||
|
|
||||||
const defaultWhatsapp = await Whatsapp.findOne({
|
const { contactId, status }: TicketData = req.body;
|
||||||
where: { default: true }
|
|
||||||
});
|
|
||||||
|
|
||||||
const ticketData = req.body;
|
const ticket = await CreateTicketService({ contactId, status });
|
||||||
|
|
||||||
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 serializaedTicket = { ...ticket.dataValues, contact: contact };
|
// const serializaedTicket = { ...ticket.dataValues, contact: contact };
|
||||||
|
|
||||||
@@ -123,7 +113,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
// ticket: serializaedTicket
|
// ticket: serializaedTicket
|
||||||
// });
|
// });
|
||||||
|
|
||||||
return res.status(200).json({ ticket });
|
return res.status(200).json(ticket);
|
||||||
};
|
};
|
||||||
|
|
||||||
// export const update = (req: Request, res: Response): Promise<Response> => {
|
// export const update = (req: Request, res: Response): Promise<Response> => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
|
||||||
import CheckSettingsHelper from "../helpers/CheckSettingsHelper";
|
import CheckSettingsHelper from "../helpers/CheckSettings";
|
||||||
import AppError from "../errors/AppError";
|
import AppError from "../errors/AppError";
|
||||||
|
|
||||||
import CreateUserService from "../services/UserServices/CreateUserService";
|
import CreateUserService from "../services/UserServices/CreateUserService";
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Request, Response } from "express";
|
|||||||
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
||||||
import DeleteWhatsApprService from "../services/WhatsappService/DeleteWhatsApprService";
|
import DeleteWhatsApprService from "../services/WhatsappService/DeleteWhatsApprService";
|
||||||
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
|
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 UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
|
||||||
// import Yup from "yup";
|
// import Yup from "yup";
|
||||||
// import Whatsapp from "../models/Whatsapp";
|
// 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> => {
|
export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
|
|
||||||
const whatsapp = await ShowWhatsAppService(whatsappId);
|
const whatsapp = await FindWhatsAppService(whatsappId);
|
||||||
|
|
||||||
return res.status(200).json(whatsapp);
|
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." });
|
||||||
|
// };
|
||||||
17
backend/src/helpers/GetDefaultWhatsapp.ts
Normal file
17
backend/src/helpers/GetDefaultWhatsapp.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import AppError from "../errors/AppError";
|
||||||
|
import Whatsapp from "../models/Whatsapp";
|
||||||
|
import FindWhatsAppService from "../services/WhatsappService/FindWhatsAppService";
|
||||||
|
|
||||||
|
const GetDefaultWhatsapp = async (): Promise<Whatsapp> => {
|
||||||
|
const defaultWhatsapp = await FindWhatsAppService({
|
||||||
|
where: { isDefault: true }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!defaultWhatsapp) {
|
||||||
|
throw new AppError("No default WhatsApp found. Check Connection page.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultWhatsapp;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default GetDefaultWhatsapp;
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
const express = require("express");
|
|
||||||
const isAuth = require("../../middleware/is-auth");
|
|
||||||
|
|
||||||
const WhatsAppSessionController = require("../../controllers/WhatsAppSessionController");
|
|
||||||
|
|
||||||
const routes = express.Router();
|
|
||||||
|
|
||||||
routes.get(
|
|
||||||
"/whatsappsession/:whatsappId",
|
|
||||||
isAuth,
|
|
||||||
WhatsAppSessionController.show
|
|
||||||
);
|
|
||||||
|
|
||||||
routes.delete(
|
|
||||||
"/whatsappsession/:whatsappId",
|
|
||||||
isAuth,
|
|
||||||
WhatsAppSessionController.delete
|
|
||||||
);
|
|
||||||
|
|
||||||
module.exports = routes;
|
|
||||||
20
backend/src/routes/whatsappSessionRoutes.ts
Normal file
20
backend/src/routes/whatsappSessionRoutes.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
// const express = require("express");
|
||||||
|
// const isAuth = require("../../middleware/is-auth");
|
||||||
|
|
||||||
|
// const WhatsAppSessionController = require("../../controllers/WhatsAppSessionController");
|
||||||
|
|
||||||
|
// const routes = express.Router();
|
||||||
|
|
||||||
|
// routes.get(
|
||||||
|
// "/whatsappsession/:whatsappId",
|
||||||
|
// isAuth,
|
||||||
|
// WhatsAppSessionController.show
|
||||||
|
// );
|
||||||
|
|
||||||
|
// routes.delete(
|
||||||
|
// "/whatsappsession/:whatsappId",
|
||||||
|
// isAuth,
|
||||||
|
// WhatsAppSessionController.delete
|
||||||
|
// );
|
||||||
|
|
||||||
|
// module.exports = routes;
|
||||||
@@ -16,6 +16,16 @@ const ListContactsService = async ({
|
|||||||
searchParam = "",
|
searchParam = "",
|
||||||
pageNumber = "1"
|
pageNumber = "1"
|
||||||
}: Request): Promise<Response> => {
|
}: Request): Promise<Response> => {
|
||||||
|
let includeCondition = [
|
||||||
|
{
|
||||||
|
model: Contact,
|
||||||
|
as: "contact",
|
||||||
|
attributes: ["name", "number", "profilePicUrl"]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
// let whereCondition = {};
|
||||||
|
|
||||||
const whereCondition = {
|
const whereCondition = {
|
||||||
[Op.or]: [
|
[Op.or]: [
|
||||||
{
|
{
|
||||||
@@ -33,6 +43,7 @@ const ListContactsService = async ({
|
|||||||
|
|
||||||
const { count, rows: contacts } = await Contact.findAndCountAll({
|
const { count, rows: contacts } = await Contact.findAndCountAll({
|
||||||
where: whereCondition,
|
where: whereCondition,
|
||||||
|
include: includeCondition,
|
||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
order: [["createdAt", "DESC"]]
|
order: [["createdAt", "DESC"]]
|
||||||
|
|||||||
@@ -1,24 +1,34 @@
|
|||||||
import * as Yup from "yup";
|
|
||||||
|
|
||||||
import AppError from "../../errors/AppError";
|
import AppError from "../../errors/AppError";
|
||||||
import User from "../../models/User";
|
import GetDefaultWhatsapp from "../../helpers/GetDefaultWhatsapp";
|
||||||
|
import Ticket from "../../models/Ticket";
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
email: string;
|
contactId: number;
|
||||||
password: string;
|
status?: string;
|
||||||
name: string;
|
|
||||||
profile?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Response {
|
const CreateTicketService = async ({
|
||||||
email: string;
|
contactId,
|
||||||
name: string;
|
status
|
||||||
id: number;
|
}: Request): Promise<Ticket> => {
|
||||||
profile: string;
|
const defaultWhatsapp = await GetDefaultWhatsapp();
|
||||||
}
|
|
||||||
|
|
||||||
const CreateTicketService = async (): Promise<boolean> => {
|
if (!defaultWhatsapp) {
|
||||||
return true;
|
throw new AppError("No default WhatsApp found. Check Connection page.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { id } = await defaultWhatsapp.$create("ticket", {
|
||||||
|
contactId,
|
||||||
|
status
|
||||||
|
});
|
||||||
|
|
||||||
|
const ticket = await Ticket.findByPk(id, { include: ["contact"] });
|
||||||
|
|
||||||
|
if (!ticket) {
|
||||||
|
throw new AppError("Error, ticket not created.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ticket;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CreateTicketService;
|
export default CreateTicketService;
|
||||||
|
|||||||
28
backend/src/services/TicketServices/FindTicketService.ts
Normal file
28
backend/src/services/TicketServices/FindTicketService.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
|
||||||
|
interface WhereParams {
|
||||||
|
id?: number;
|
||||||
|
name?: string;
|
||||||
|
isDefault?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
where?: WhereParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
const FindTicketService = async ({ where }: Request): Promise<Whatsapp> => {
|
||||||
|
const whereCondition = { ...where };
|
||||||
|
|
||||||
|
const whatsapp = await Whatsapp.findOne({
|
||||||
|
where: whereCondition
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!whatsapp) {
|
||||||
|
throw new AppError("No whatsapp found with this conditions.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return whatsapp;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default FindTicketService;
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
import { Op } from "sequelize";
|
import { Op, fn, where, col } from "sequelize";
|
||||||
import { startOfDay, endOfDay, parseISO } from "date-fns";
|
import { startOfDay, endOfDay, parseISO } from "date-fns";
|
||||||
|
|
||||||
import Ticket from "../../models/Ticket";
|
import Ticket from "../../models/Ticket";
|
||||||
|
import Contact from "../../models/Contact";
|
||||||
|
import Message from "../../models/Message";
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
searchParam?: string;
|
searchParam?: string;
|
||||||
@@ -27,6 +29,15 @@ const ListTicketsService = async ({
|
|||||||
userId
|
userId
|
||||||
}: Request): Promise<Response> => {
|
}: Request): Promise<Response> => {
|
||||||
let whereCondition = {};
|
let whereCondition = {};
|
||||||
|
let includeCondition = [];
|
||||||
|
|
||||||
|
includeCondition = [
|
||||||
|
{
|
||||||
|
model: Contact,
|
||||||
|
as: "contact",
|
||||||
|
attributes: ["name", "number", "profilePicUrl"]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
if (showAll === "true") {
|
if (showAll === "true") {
|
||||||
whereCondition = {};
|
whereCondition = {};
|
||||||
@@ -41,6 +52,46 @@ const ListTicketsService = async ({
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (searchParam) {
|
||||||
|
includeCondition = [
|
||||||
|
...includeCondition,
|
||||||
|
{
|
||||||
|
model: Message,
|
||||||
|
as: "messages",
|
||||||
|
attributes: ["id", "body"],
|
||||||
|
where: {
|
||||||
|
body: where(
|
||||||
|
fn("LOWER", col("body")),
|
||||||
|
"LIKE",
|
||||||
|
`%${searchParam.toLowerCase()}%`
|
||||||
|
)
|
||||||
|
},
|
||||||
|
required: false,
|
||||||
|
duplicating: false
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
whereCondition = {
|
||||||
|
[Op.or]: [
|
||||||
|
{
|
||||||
|
"$contact.name$": where(
|
||||||
|
fn("LOWER", col("name")),
|
||||||
|
"LIKE",
|
||||||
|
`%${searchParam.toLowerCase()}%`
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{ "$contact.number$": { [Op.like]: `%${searchParam}%` } },
|
||||||
|
{
|
||||||
|
"$message.body$": where(
|
||||||
|
fn("LOWER", col("body")),
|
||||||
|
"LIKE",
|
||||||
|
`%${searchParam.toLowerCase()}%`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (date) {
|
if (date) {
|
||||||
whereCondition = {
|
whereCondition = {
|
||||||
...whereCondition,
|
...whereCondition,
|
||||||
@@ -55,8 +106,8 @@ const ListTicketsService = async ({
|
|||||||
|
|
||||||
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
||||||
where: whereCondition,
|
where: whereCondition,
|
||||||
// distinct: true,
|
include: includeCondition,
|
||||||
// include: includeCondition,
|
distinct: true,
|
||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
order: [["updatedAt", "DESC"]]
|
order: [["updatedAt", "DESC"]]
|
||||||
|
|||||||
30
backend/src/services/WhatsappService/FindWhatsAppService.ts
Normal file
30
backend/src/services/WhatsappService/FindWhatsAppService.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
|
||||||
|
interface WhereParams {
|
||||||
|
id?: number;
|
||||||
|
name?: string;
|
||||||
|
isDefault?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
where?: WhereParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
const FindWhatsAppService = async ({
|
||||||
|
where
|
||||||
|
}: Request): Promise<Whatsapp | undefined> => {
|
||||||
|
const whereCondition = { ...where };
|
||||||
|
|
||||||
|
const whatsapp = await Whatsapp.findOne({
|
||||||
|
where: whereCondition
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!whatsapp) {
|
||||||
|
throw new AppError("No whatsapp found with this conditions.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return whatsapp;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default FindWhatsAppService;
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import Whatsapp from "../../models/Whatsapp";
|
|
||||||
import AppError from "../../errors/AppError";
|
|
||||||
|
|
||||||
const ShowWhatsAppService = async (
|
|
||||||
id: string
|
|
||||||
): Promise<Whatsapp | undefined> => {
|
|
||||||
const whatsapp = await Whatsapp.findOne({
|
|
||||||
where: { id }
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!whatsapp) {
|
|
||||||
throw new AppError("No whatsapp found with this ID.", 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
return whatsapp;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default ShowWhatsAppService;
|
|
||||||
Reference in New Issue
Block a user