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:
@@ -16,6 +16,16 @@ const ListContactsService = async ({
|
||||
searchParam = "",
|
||||
pageNumber = "1"
|
||||
}: Request): Promise<Response> => {
|
||||
let includeCondition = [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
attributes: ["name", "number", "profilePicUrl"]
|
||||
}
|
||||
];
|
||||
|
||||
// let whereCondition = {};
|
||||
|
||||
const whereCondition = {
|
||||
[Op.or]: [
|
||||
{
|
||||
@@ -33,6 +43,7 @@ const ListContactsService = async ({
|
||||
|
||||
const { count, rows: contacts } = await Contact.findAndCountAll({
|
||||
where: whereCondition,
|
||||
include: includeCondition,
|
||||
limit,
|
||||
offset,
|
||||
order: [["createdAt", "DESC"]]
|
||||
|
||||
@@ -1,24 +1,34 @@
|
||||
import * as Yup from "yup";
|
||||
|
||||
import AppError from "../../errors/AppError";
|
||||
import User from "../../models/User";
|
||||
import GetDefaultWhatsapp from "../../helpers/GetDefaultWhatsapp";
|
||||
import Ticket from "../../models/Ticket";
|
||||
|
||||
interface Request {
|
||||
email: string;
|
||||
password: string;
|
||||
name: string;
|
||||
profile?: string;
|
||||
contactId: number;
|
||||
status?: string;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
email: string;
|
||||
name: string;
|
||||
id: number;
|
||||
profile: string;
|
||||
}
|
||||
const CreateTicketService = async ({
|
||||
contactId,
|
||||
status
|
||||
}: Request): Promise<Ticket> => {
|
||||
const defaultWhatsapp = await GetDefaultWhatsapp();
|
||||
|
||||
const CreateTicketService = async (): Promise<boolean> => {
|
||||
return true;
|
||||
if (!defaultWhatsapp) {
|
||||
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;
|
||||
|
||||
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 Ticket from "../../models/Ticket";
|
||||
import Contact from "../../models/Contact";
|
||||
import Message from "../../models/Message";
|
||||
|
||||
interface Request {
|
||||
searchParam?: string;
|
||||
@@ -27,6 +29,15 @@ const ListTicketsService = async ({
|
||||
userId
|
||||
}: Request): Promise<Response> => {
|
||||
let whereCondition = {};
|
||||
let includeCondition = [];
|
||||
|
||||
includeCondition = [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
attributes: ["name", "number", "profilePicUrl"]
|
||||
}
|
||||
];
|
||||
|
||||
if (showAll === "true") {
|
||||
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) {
|
||||
whereCondition = {
|
||||
...whereCondition,
|
||||
@@ -55,8 +106,8 @@ const ListTicketsService = async ({
|
||||
|
||||
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
||||
where: whereCondition,
|
||||
// distinct: true,
|
||||
// include: includeCondition,
|
||||
include: includeCondition,
|
||||
distinct: true,
|
||||
limit,
|
||||
offset,
|
||||
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