mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
feat: removed optionNumber from queues assocs
This commit is contained in:
@@ -9,13 +9,9 @@ import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsServi
|
||||
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
||||
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
|
||||
|
||||
interface QueueData {
|
||||
id: number;
|
||||
optionNumber: number;
|
||||
}
|
||||
interface WhatsappData {
|
||||
name: string;
|
||||
queuesData: QueueData[];
|
||||
queueIds: number[];
|
||||
greetingMessage?: string;
|
||||
status?: string;
|
||||
isDefault?: boolean;
|
||||
@@ -33,7 +29,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
status,
|
||||
isDefault,
|
||||
greetingMessage,
|
||||
queuesData
|
||||
queueIds
|
||||
}: WhatsappData = req.body;
|
||||
|
||||
const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({
|
||||
@@ -41,7 +37,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
status,
|
||||
isDefault,
|
||||
greetingMessage,
|
||||
queuesData
|
||||
queueIds
|
||||
});
|
||||
|
||||
// StartWhatsAppSession(whatsapp);
|
||||
|
||||
@@ -3,9 +3,6 @@ import { QueryInterface, DataTypes } from "sequelize";
|
||||
module.exports = {
|
||||
up: (queryInterface: QueryInterface) => {
|
||||
return queryInterface.createTable("WhatsappQueues", {
|
||||
optionNumber: {
|
||||
type: DataTypes.INTEGER
|
||||
},
|
||||
whatsappId: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true
|
||||
|
||||
@@ -12,9 +12,6 @@ import Whatsapp from "./Whatsapp";
|
||||
|
||||
@Table
|
||||
class WhatsappQueue extends Model<WhatsappQueue> {
|
||||
@Column
|
||||
optionNumber: number;
|
||||
|
||||
@ForeignKey(() => Whatsapp)
|
||||
@Column
|
||||
whatsappId: number;
|
||||
|
||||
@@ -1,32 +1,12 @@
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
import WhatsappQueue from "../../models/WhatsappQueue";
|
||||
|
||||
interface QueueData {
|
||||
id: number;
|
||||
optionNumber: number;
|
||||
}
|
||||
|
||||
const AssociateWhatsappQueue = async (
|
||||
whatsapp: Whatsapp,
|
||||
queuesData: QueueData[]
|
||||
queueIds: number[]
|
||||
): Promise<void> => {
|
||||
const queueIds = queuesData.map(({ id }) => id);
|
||||
|
||||
await whatsapp.$set("queues", queueIds);
|
||||
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
/* eslint-disable no-await-in-loop */
|
||||
for (const queueData of queuesData) {
|
||||
await WhatsappQueue.update(
|
||||
{ optionNumber: queueData.optionNumber },
|
||||
{
|
||||
where: {
|
||||
whatsappId: whatsapp.id,
|
||||
queueId: queueData.id
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
await whatsapp.reload();
|
||||
};
|
||||
|
||||
export default AssociateWhatsappQueue;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Queue from "../../models/Queue";
|
||||
|
||||
const ListQueuesService = async (): Promise<Queue[]> => {
|
||||
const queues = await Queue.findAll();
|
||||
const queues = await Queue.findAll({ order: [["name", "ASC"]] });
|
||||
|
||||
return queues;
|
||||
};
|
||||
|
||||
@@ -133,12 +133,10 @@ const verifyQueue = async (
|
||||
ticket: Ticket,
|
||||
contact: Contact
|
||||
) => {
|
||||
const { whatsappQueues, greetingMessage } = await ShowWhatsAppService(
|
||||
wbot.id!
|
||||
);
|
||||
const { queues, greetingMessage } = await ShowWhatsAppService(wbot.id!);
|
||||
|
||||
if (whatsappQueues.length === 1) {
|
||||
await ticket.$set("queue", whatsappQueues[0].queue);
|
||||
if (queues.length === 1) {
|
||||
await ticket.$set("queue", queues[0]);
|
||||
// TODO sendTicketQueueUpdate to frontend
|
||||
|
||||
return;
|
||||
@@ -146,14 +144,12 @@ const verifyQueue = async (
|
||||
|
||||
const selectedOption = msg.body[0];
|
||||
|
||||
const validOption = whatsappQueues.find(
|
||||
q => q.optionNumber === +selectedOption
|
||||
);
|
||||
const validOption = queues[+selectedOption - 1];
|
||||
|
||||
if (validOption) {
|
||||
await ticket.$set("queue", validOption.queue);
|
||||
await ticket.$set("queue", validOption);
|
||||
|
||||
const body = `\u200e ${validOption.queue.greetingMessage}`;
|
||||
const body = `\u200e ${validOption.greetingMessage}`;
|
||||
|
||||
const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);
|
||||
|
||||
@@ -163,8 +159,8 @@ const verifyQueue = async (
|
||||
} else {
|
||||
let options = "";
|
||||
|
||||
whatsappQueues.forEach(whatsQueue => {
|
||||
options += `*${whatsQueue.optionNumber}* - ${whatsQueue.queue.name}\n`;
|
||||
queues.forEach((queue, index) => {
|
||||
options += `*${index + 1}* - ${queue.name}\n`;
|
||||
});
|
||||
|
||||
const body = `\u200e ${greetingMessage}\n\n${options}`;
|
||||
|
||||
@@ -4,13 +4,9 @@ import AppError from "../../errors/AppError";
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
import AssociateWhatsappQueue from "../QueueService/AssociateWhatsappQueue";
|
||||
|
||||
interface QueueData {
|
||||
id: number;
|
||||
optionNumber: number;
|
||||
}
|
||||
interface Request {
|
||||
name: string;
|
||||
queuesData: QueueData[];
|
||||
queueIds?: number[];
|
||||
greetingMessage?: string;
|
||||
status?: string;
|
||||
isDefault?: boolean;
|
||||
@@ -24,7 +20,7 @@ interface Response {
|
||||
const CreateWhatsAppService = async ({
|
||||
name,
|
||||
status = "OPENING",
|
||||
queuesData = [],
|
||||
queueIds = [],
|
||||
greetingMessage,
|
||||
isDefault = false
|
||||
}: Request): Promise<Response> => {
|
||||
@@ -71,6 +67,10 @@ const CreateWhatsAppService = async ({
|
||||
}
|
||||
}
|
||||
|
||||
if (queueIds.length > 1 && !greetingMessage) {
|
||||
throw new AppError("ERR_WAPP_GREETING_REQUIRED");
|
||||
}
|
||||
|
||||
const whatsapp = await Whatsapp.create(
|
||||
{
|
||||
name,
|
||||
@@ -81,9 +81,7 @@ const CreateWhatsAppService = async ({
|
||||
{ include: ["queues"] }
|
||||
);
|
||||
|
||||
await AssociateWhatsappQueue(whatsapp, queuesData);
|
||||
|
||||
await whatsapp.reload();
|
||||
await AssociateWhatsappQueue(whatsapp, queueIds);
|
||||
|
||||
return { whatsapp, oldDefaultWhatsapp };
|
||||
};
|
||||
|
||||
@@ -1,24 +1,15 @@
|
||||
import Queue from "../../models/Queue";
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
import WhatsappQueue from "../../models/WhatsappQueue";
|
||||
|
||||
const ListWhatsAppsService = async (): Promise<Whatsapp[]> => {
|
||||
const whatsapps = await Whatsapp.findAll({
|
||||
include: [
|
||||
{
|
||||
model: WhatsappQueue,
|
||||
as: "whatsappQueues",
|
||||
attributes: ["optionNumber"],
|
||||
include: [
|
||||
{
|
||||
model: Queue,
|
||||
as: "queue",
|
||||
attributes: ["id", "name", "color", "greetingMessage"]
|
||||
}
|
||||
]
|
||||
model: Queue,
|
||||
as: "queues",
|
||||
attributes: ["id", "name", "color", "greetingMessage"]
|
||||
}
|
||||
],
|
||||
order: [["whatsappQueues", "optionNumber", "ASC"]]
|
||||
]
|
||||
});
|
||||
|
||||
return whatsapps;
|
||||
|
||||
@@ -1,25 +1,17 @@
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
import AppError from "../../errors/AppError";
|
||||
import Queue from "../../models/Queue";
|
||||
import WhatsappQueue from "../../models/WhatsappQueue";
|
||||
|
||||
const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => {
|
||||
const whatsapp = await Whatsapp.findByPk(id, {
|
||||
include: [
|
||||
{
|
||||
model: WhatsappQueue,
|
||||
as: "whatsappQueues",
|
||||
attributes: ["optionNumber"],
|
||||
include: [
|
||||
{
|
||||
model: Queue,
|
||||
as: "queue",
|
||||
attributes: ["id", "name", "color", "greetingMessage"]
|
||||
}
|
||||
]
|
||||
model: Queue,
|
||||
as: "queues",
|
||||
attributes: ["id", "name", "color", "greetingMessage"]
|
||||
}
|
||||
],
|
||||
order: [["whatsappQueues", "optionNumber", "ASC"]]
|
||||
order: [["queues", "name", "ASC"]]
|
||||
});
|
||||
|
||||
if (!whatsapp) {
|
||||
|
||||
@@ -6,17 +6,13 @@ import Whatsapp from "../../models/Whatsapp";
|
||||
import ShowWhatsAppService from "./ShowWhatsAppService";
|
||||
import AssociateWhatsappQueue from "../QueueService/AssociateWhatsappQueue";
|
||||
|
||||
interface QueueData {
|
||||
id: number;
|
||||
optionNumber: number;
|
||||
}
|
||||
interface WhatsappData {
|
||||
name?: string;
|
||||
status?: string;
|
||||
session?: string;
|
||||
isDefault?: boolean;
|
||||
greetingMessage?: string;
|
||||
queuesData?: QueueData[];
|
||||
queueIds?: number[];
|
||||
}
|
||||
|
||||
interface Request {
|
||||
@@ -34,7 +30,7 @@ const UpdateWhatsAppService = async ({
|
||||
whatsappId
|
||||
}: Request): Promise<Response> => {
|
||||
const schema = Yup.object().shape({
|
||||
name: Yup.string().min(2),
|
||||
name: Yup.string().min(2).required(),
|
||||
isDefault: Yup.boolean()
|
||||
});
|
||||
|
||||
@@ -44,7 +40,7 @@ const UpdateWhatsAppService = async ({
|
||||
isDefault,
|
||||
session,
|
||||
greetingMessage,
|
||||
queuesData = []
|
||||
queueIds = []
|
||||
} = whatsappData;
|
||||
|
||||
try {
|
||||
@@ -53,6 +49,10 @@ const UpdateWhatsAppService = async ({
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
if (queueIds.length > 1 && !greetingMessage) {
|
||||
throw new AppError("ERR_WAPP_GREETING_REQUIRED");
|
||||
}
|
||||
|
||||
let oldDefaultWhatsapp: Whatsapp | null = null;
|
||||
|
||||
if (isDefault) {
|
||||
@@ -74,9 +74,7 @@ const UpdateWhatsAppService = async ({
|
||||
isDefault
|
||||
});
|
||||
|
||||
await AssociateWhatsappQueue(whatsapp, queuesData);
|
||||
|
||||
await whatsapp.reload();
|
||||
await AssociateWhatsappQueue(whatsapp, queueIds);
|
||||
|
||||
return { whatsapp, oldDefaultWhatsapp };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user