changed ticket creation and list to typescript

This commit is contained in:
canove
2020-09-19 21:00:49 -03:00
parent 5dda3aabaf
commit 1d0bbda00d
16 changed files with 473 additions and 81 deletions

View File

@@ -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"]]

View File

@@ -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;

View 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;

View File

@@ -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"]]

View 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;

View File

@@ -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;