feat: removed optionNumber from queues assocs

This commit is contained in:
canove
2021-01-11 06:38:45 -03:00
parent a75eb49b31
commit be320fa34b
11 changed files with 39 additions and 94 deletions

View File

@@ -9,13 +9,9 @@ import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsServi
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService"; import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService"; import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
interface QueueData {
id: number;
optionNumber: number;
}
interface WhatsappData { interface WhatsappData {
name: string; name: string;
queuesData: QueueData[]; queueIds: number[];
greetingMessage?: string; greetingMessage?: string;
status?: string; status?: string;
isDefault?: boolean; isDefault?: boolean;
@@ -33,7 +29,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
status, status,
isDefault, isDefault,
greetingMessage, greetingMessage,
queuesData queueIds
}: WhatsappData = req.body; }: WhatsappData = req.body;
const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({ const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({
@@ -41,7 +37,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
status, status,
isDefault, isDefault,
greetingMessage, greetingMessage,
queuesData queueIds
}); });
// StartWhatsAppSession(whatsapp); // StartWhatsAppSession(whatsapp);

View File

@@ -3,9 +3,6 @@ import { QueryInterface, DataTypes } from "sequelize";
module.exports = { module.exports = {
up: (queryInterface: QueryInterface) => { up: (queryInterface: QueryInterface) => {
return queryInterface.createTable("WhatsappQueues", { return queryInterface.createTable("WhatsappQueues", {
optionNumber: {
type: DataTypes.INTEGER
},
whatsappId: { whatsappId: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true primaryKey: true

View File

@@ -12,9 +12,6 @@ import Whatsapp from "./Whatsapp";
@Table @Table
class WhatsappQueue extends Model<WhatsappQueue> { class WhatsappQueue extends Model<WhatsappQueue> {
@Column
optionNumber: number;
@ForeignKey(() => Whatsapp) @ForeignKey(() => Whatsapp)
@Column @Column
whatsappId: number; whatsappId: number;

View File

@@ -1,32 +1,12 @@
import Whatsapp from "../../models/Whatsapp"; import Whatsapp from "../../models/Whatsapp";
import WhatsappQueue from "../../models/WhatsappQueue";
interface QueueData {
id: number;
optionNumber: number;
}
const AssociateWhatsappQueue = async ( const AssociateWhatsappQueue = async (
whatsapp: Whatsapp, whatsapp: Whatsapp,
queuesData: QueueData[] queueIds: number[]
): Promise<void> => { ): Promise<void> => {
const queueIds = queuesData.map(({ id }) => id);
await whatsapp.$set("queues", queueIds); await whatsapp.$set("queues", queueIds);
/* eslint-disable no-restricted-syntax */ await whatsapp.reload();
/* eslint-disable no-await-in-loop */
for (const queueData of queuesData) {
await WhatsappQueue.update(
{ optionNumber: queueData.optionNumber },
{
where: {
whatsappId: whatsapp.id,
queueId: queueData.id
}
}
);
}
}; };
export default AssociateWhatsappQueue; export default AssociateWhatsappQueue;

View File

@@ -1,7 +1,7 @@
import Queue from "../../models/Queue"; import Queue from "../../models/Queue";
const ListQueuesService = async (): Promise<Queue[]> => { const ListQueuesService = async (): Promise<Queue[]> => {
const queues = await Queue.findAll(); const queues = await Queue.findAll({ order: [["name", "ASC"]] });
return queues; return queues;
}; };

View File

@@ -133,12 +133,10 @@ const verifyQueue = async (
ticket: Ticket, ticket: Ticket,
contact: Contact contact: Contact
) => { ) => {
const { whatsappQueues, greetingMessage } = await ShowWhatsAppService( const { queues, greetingMessage } = await ShowWhatsAppService(wbot.id!);
wbot.id!
);
if (whatsappQueues.length === 1) { if (queues.length === 1) {
await ticket.$set("queue", whatsappQueues[0].queue); await ticket.$set("queue", queues[0]);
// TODO sendTicketQueueUpdate to frontend // TODO sendTicketQueueUpdate to frontend
return; return;
@@ -146,14 +144,12 @@ const verifyQueue = async (
const selectedOption = msg.body[0]; const selectedOption = msg.body[0];
const validOption = whatsappQueues.find( const validOption = queues[+selectedOption - 1];
q => q.optionNumber === +selectedOption
);
if (validOption) { 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); const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);
@@ -163,8 +159,8 @@ const verifyQueue = async (
} else { } else {
let options = ""; let options = "";
whatsappQueues.forEach(whatsQueue => { queues.forEach((queue, index) => {
options += `*${whatsQueue.optionNumber}* - ${whatsQueue.queue.name}\n`; options += `*${index + 1}* - ${queue.name}\n`;
}); });
const body = `\u200e ${greetingMessage}\n\n${options}`; const body = `\u200e ${greetingMessage}\n\n${options}`;

View File

@@ -4,13 +4,9 @@ import AppError from "../../errors/AppError";
import Whatsapp from "../../models/Whatsapp"; import Whatsapp from "../../models/Whatsapp";
import AssociateWhatsappQueue from "../QueueService/AssociateWhatsappQueue"; import AssociateWhatsappQueue from "../QueueService/AssociateWhatsappQueue";
interface QueueData {
id: number;
optionNumber: number;
}
interface Request { interface Request {
name: string; name: string;
queuesData: QueueData[]; queueIds?: number[];
greetingMessage?: string; greetingMessage?: string;
status?: string; status?: string;
isDefault?: boolean; isDefault?: boolean;
@@ -24,7 +20,7 @@ interface Response {
const CreateWhatsAppService = async ({ const CreateWhatsAppService = async ({
name, name,
status = "OPENING", status = "OPENING",
queuesData = [], queueIds = [],
greetingMessage, greetingMessage,
isDefault = false isDefault = false
}: Request): Promise<Response> => { }: 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( const whatsapp = await Whatsapp.create(
{ {
name, name,
@@ -81,9 +81,7 @@ const CreateWhatsAppService = async ({
{ include: ["queues"] } { include: ["queues"] }
); );
await AssociateWhatsappQueue(whatsapp, queuesData); await AssociateWhatsappQueue(whatsapp, queueIds);
await whatsapp.reload();
return { whatsapp, oldDefaultWhatsapp }; return { whatsapp, oldDefaultWhatsapp };
}; };

View File

@@ -1,24 +1,15 @@
import Queue from "../../models/Queue"; import Queue from "../../models/Queue";
import Whatsapp from "../../models/Whatsapp"; import Whatsapp from "../../models/Whatsapp";
import WhatsappQueue from "../../models/WhatsappQueue";
const ListWhatsAppsService = async (): Promise<Whatsapp[]> => { const ListWhatsAppsService = async (): Promise<Whatsapp[]> => {
const whatsapps = await Whatsapp.findAll({ const whatsapps = await Whatsapp.findAll({
include: [ include: [
{ {
model: WhatsappQueue, model: Queue,
as: "whatsappQueues", as: "queues",
attributes: ["optionNumber"], attributes: ["id", "name", "color", "greetingMessage"]
include: [
{
model: Queue,
as: "queue",
attributes: ["id", "name", "color", "greetingMessage"]
}
]
} }
], ]
order: [["whatsappQueues", "optionNumber", "ASC"]]
}); });
return whatsapps; return whatsapps;

View File

@@ -1,25 +1,17 @@
import Whatsapp from "../../models/Whatsapp"; import Whatsapp from "../../models/Whatsapp";
import AppError from "../../errors/AppError"; import AppError from "../../errors/AppError";
import Queue from "../../models/Queue"; import Queue from "../../models/Queue";
import WhatsappQueue from "../../models/WhatsappQueue";
const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => { const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => {
const whatsapp = await Whatsapp.findByPk(id, { const whatsapp = await Whatsapp.findByPk(id, {
include: [ include: [
{ {
model: WhatsappQueue, model: Queue,
as: "whatsappQueues", as: "queues",
attributes: ["optionNumber"], attributes: ["id", "name", "color", "greetingMessage"]
include: [
{
model: Queue,
as: "queue",
attributes: ["id", "name", "color", "greetingMessage"]
}
]
} }
], ],
order: [["whatsappQueues", "optionNumber", "ASC"]] order: [["queues", "name", "ASC"]]
}); });
if (!whatsapp) { if (!whatsapp) {

View File

@@ -6,17 +6,13 @@ import Whatsapp from "../../models/Whatsapp";
import ShowWhatsAppService from "./ShowWhatsAppService"; import ShowWhatsAppService from "./ShowWhatsAppService";
import AssociateWhatsappQueue from "../QueueService/AssociateWhatsappQueue"; import AssociateWhatsappQueue from "../QueueService/AssociateWhatsappQueue";
interface QueueData {
id: number;
optionNumber: number;
}
interface WhatsappData { interface WhatsappData {
name?: string; name?: string;
status?: string; status?: string;
session?: string; session?: string;
isDefault?: boolean; isDefault?: boolean;
greetingMessage?: string; greetingMessage?: string;
queuesData?: QueueData[]; queueIds?: number[];
} }
interface Request { interface Request {
@@ -34,7 +30,7 @@ const UpdateWhatsAppService = async ({
whatsappId whatsappId
}: Request): Promise<Response> => { }: Request): Promise<Response> => {
const schema = Yup.object().shape({ const schema = Yup.object().shape({
name: Yup.string().min(2), name: Yup.string().min(2).required(),
isDefault: Yup.boolean() isDefault: Yup.boolean()
}); });
@@ -44,7 +40,7 @@ const UpdateWhatsAppService = async ({
isDefault, isDefault,
session, session,
greetingMessage, greetingMessage,
queuesData = [] queueIds = []
} = whatsappData; } = whatsappData;
try { try {
@@ -53,6 +49,10 @@ const UpdateWhatsAppService = async ({
throw new AppError(err.message); throw new AppError(err.message);
} }
if (queueIds.length > 1 && !greetingMessage) {
throw new AppError("ERR_WAPP_GREETING_REQUIRED");
}
let oldDefaultWhatsapp: Whatsapp | null = null; let oldDefaultWhatsapp: Whatsapp | null = null;
if (isDefault) { if (isDefault) {
@@ -74,9 +74,7 @@ const UpdateWhatsAppService = async ({
isDefault isDefault
}); });
await AssociateWhatsappQueue(whatsapp, queuesData); await AssociateWhatsappQueue(whatsapp, queueIds);
await whatsapp.reload();
return { whatsapp, oldDefaultWhatsapp }; return { whatsapp, oldDefaultWhatsapp };
}; };

View File

@@ -75,7 +75,7 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => {
const { data } = await api.get(`whatsapp/${whatsAppId}`); const { data } = await api.get(`whatsapp/${whatsAppId}`);
setWhatsApp(data); setWhatsApp(data);
const whatsQueueIds = data.whatsappQueues?.map(q => q.queue.id); const whatsQueueIds = data.queues?.map(queue => queue.id);
setSelectedQueueIds(whatsQueueIds); setSelectedQueueIds(whatsQueueIds);
} catch (err) { } catch (err) {
toastError(err); toastError(err);
@@ -94,10 +94,10 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => {
await api.post("/whatsapp", whatsappData); await api.post("/whatsapp", whatsappData);
} }
toast.success(i18n.t("whatsappModal.success")); toast.success(i18n.t("whatsappModal.success"));
handleClose();
} catch (err) { } catch (err) {
toastError(err); toastError(err);
} }
handleClose();
}; };
const handleClose = () => { const handleClose = () => {