diff --git a/backend/src/controllers/TicketController.js b/backend/src/controllers/TicketController.js index 869cad2..f2a34d5 100644 --- a/backend/src/controllers/TicketController.js +++ b/backend/src/controllers/TicketController.js @@ -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"]], }); diff --git a/backend/src/models/Message.js b/backend/src/models/Message.js index a6b573a..6192485 100644 --- a/backend/src/models/Message.js +++ b/backend/src/models/Message.js @@ -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" }); } } diff --git a/backend/src/models/Ticket.js b/backend/src/models/Ticket.js index 1ce9e0b..38a46d6 100644 --- a/backend/src/models/Ticket.js +++ b/backend/src/models/Ticket.js @@ -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" }); } }