mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 20:29:17 +00:00
feat: start tickets and messages search
This commit is contained in:
@@ -3,13 +3,22 @@ const { startOfDay, endOfDay, parseISO } = require("date-fns");
|
|||||||
|
|
||||||
const Ticket = require("../models/Ticket");
|
const Ticket = require("../models/Ticket");
|
||||||
const Contact = require("../models/Contact");
|
const Contact = require("../models/Contact");
|
||||||
|
const Message = require("../models/Message");
|
||||||
|
|
||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
|
|
||||||
exports.index = async (req, res) => {
|
exports.index = async (req, res) => {
|
||||||
const { status = "", date = "" } = req.query;
|
const { status = "", date = "", searchParam = "" } = req.query;
|
||||||
|
|
||||||
let whereCondition = {};
|
let whereCondition = {};
|
||||||
|
let includeCondition = [
|
||||||
|
{
|
||||||
|
model: Contact,
|
||||||
|
as: "contact",
|
||||||
|
attributes: ["name", "number", "profilePicUrl"],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
if (status === "open") {
|
if (status === "open") {
|
||||||
whereCondition = {
|
whereCondition = {
|
||||||
...whereCondition,
|
...whereCondition,
|
||||||
@@ -17,7 +26,46 @@ exports.index = async (req, res) => {
|
|||||||
};
|
};
|
||||||
} else if (status === "closed") {
|
} else if (status === "closed") {
|
||||||
whereCondition = { ...whereCondition, 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) {
|
if (date) {
|
||||||
whereCondition = {
|
whereCondition = {
|
||||||
...whereCondition,
|
...whereCondition,
|
||||||
@@ -32,13 +80,7 @@ exports.index = async (req, res) => {
|
|||||||
|
|
||||||
const tickets = await Ticket.findAll({
|
const tickets = await Ticket.findAll({
|
||||||
where: whereCondition,
|
where: whereCondition,
|
||||||
include: [
|
include: includeCondition,
|
||||||
{
|
|
||||||
model: Contact,
|
|
||||||
as: "contact",
|
|
||||||
attributes: ["name", "number", "profilePicUrl"],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
order: [["updatedAt", "DESC"]],
|
order: [["updatedAt", "DESC"]],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class Message extends Sequelize.Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static associate(models) {
|
static associate(models) {
|
||||||
this.belongsTo(models.Ticket, { foreignKey: "ticketId" });
|
this.belongsTo(models.Ticket, { foreignKey: "ticketId", as: "messages" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class Ticket extends Sequelize.Model {
|
|||||||
static associate(models) {
|
static associate(models) {
|
||||||
this.belongsTo(models.Contact, { foreignKey: "contactId", as: "contact" });
|
this.belongsTo(models.Contact, { foreignKey: "contactId", as: "contact" });
|
||||||
this.belongsTo(models.User, { foreignKey: "userId", as: "user" });
|
this.belongsTo(models.User, { foreignKey: "userId", as: "user" });
|
||||||
this.hasMany(models.Message, { foreignKey: "ticketId" });
|
this.hasMany(models.Message, { foreignKey: "ticketId", as: "messages" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user