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

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