feat: start tickets and messages search

This commit is contained in:
canove
2020-08-04 20:55:44 -03:00
parent 4b2db9e003
commit a6d195f53a
3 changed files with 52 additions and 10 deletions

View File

@@ -3,13 +3,22 @@ const { startOfDay, endOfDay, parseISO } = require("date-fns");
const Ticket = require("../models/Ticket");
const Contact = require("../models/Contact");
const Message = require("../models/Message");
const { getIO } = require("../libs/socket");
exports.index = async (req, res) => {
const { status = "", date = "" } = req.query;
const { status = "", date = "", searchParam = "" } = req.query;
let whereCondition = {};
let includeCondition = [
{
model: Contact,
as: "contact",
attributes: ["name", "number", "profilePicUrl"],
},
];
if (status === "open") {
whereCondition = {
...whereCondition,
@@ -17,7 +26,46 @@ exports.index = async (req, res) => {
};
} else if (status === "closed") {
whereCondition = { ...whereCondition, status: "closed" };
} else if (searchParam) {
includeCondition = [
...includeCondition,
{
model: Message,
as: "messages",
attributes: ["id", "body"],
where: {
body: Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("body")),
"LIKE",
"%" + searchParam.toLowerCase() + "%"
),
},
required: false,
},
];
whereCondition = {
...whereCondition,
[Sequelize.Op.or]: [
{
"$contact.name$": Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("name")),
"LIKE",
"%" + searchParam.toLowerCase() + "%"
),
},
{ "$contact.number$": { [Sequelize.Op.like]: `%${searchParam}%` } },
{
"$message.body$": Sequelize.where(
Sequelize.fn("LOWER", Sequelize.col("body")),
"LIKE",
"%" + searchParam.toLowerCase() + "%"
),
},
],
};
}
if (date) {
whereCondition = {
...whereCondition,
@@ -32,13 +80,7 @@ exports.index = async (req, res) => {
const tickets = await Ticket.findAll({
where: whereCondition,
include: [
{
model: Contact,
as: "contact",
attributes: ["name", "number", "profilePicUrl"],
},
],
include: includeCondition,
order: [["updatedAt", "DESC"]],
});

View File

@@ -28,7 +28,7 @@ class Message extends Sequelize.Model {
}
static associate(models) {
this.belongsTo(models.Ticket, { foreignKey: "ticketId" });
this.belongsTo(models.Ticket, { foreignKey: "ticketId", as: "messages" });
}
}

View File

@@ -39,7 +39,7 @@ class Ticket extends Sequelize.Model {
static associate(models) {
this.belongsTo(models.Contact, { foreignKey: "contactId", as: "contact" });
this.belongsTo(models.User, { foreignKey: "userId", as: "user" });
this.hasMany(models.Message, { foreignKey: "ticketId" });
this.hasMany(models.Message, { foreignKey: "ticketId", as: "messages" });
}
}