Skip to content

Commit

Permalink
Add e Alter Color Whatsapp, Tag, Queue e Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Oct 29, 2024
1 parent ed479ca commit 6a3ec20
Show file tree
Hide file tree
Showing 17 changed files with 236 additions and 139 deletions.
14 changes: 8 additions & 6 deletions backend/src/controllers/WhatsAppController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Request, Response } from "express";
import AppError from "../errors/AppError";
import { getIO } from "../libs/socket";
import { removeWbot } from "../libs/wbot";
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
import AppError from "../errors/AppError";

import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
import DeleteWhatsAppService from "../services/WhatsappService/DeleteWhatsAppService";
Expand All @@ -17,6 +17,7 @@ interface WhatsappData {
farewellMessage?: string;
status?: string;
isDefault?: boolean;
color?: string;
}

export const index = async (req: Request, res: Response): Promise<Response> => {
Expand All @@ -26,9 +27,8 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
};

export const store = async (req: Request, res: Response): Promise<Response> => {
const WhatsApps = await ListWhatsAppsService();

const WhatsApps = await ListWhatsAppsService();

if (WhatsApps.length >= Number(process.env.CONNECTIONS_LIMIT)) {
throw new AppError("ERR_CONNECTION_CREATION_COUNT", 403);
}
Expand All @@ -39,7 +39,8 @@ const WhatsApps = await ListWhatsAppsService();
isDefault,
greetingMessage,
farewellMessage,
queueIds
queueIds,
color
}: WhatsappData = req.body;

const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({
Expand All @@ -48,7 +49,8 @@ const WhatsApps = await ListWhatsAppsService();
isDefault,
greetingMessage,
farewellMessage,
queueIds
queueIds,
color
});

StartWhatsAppSession(whatsapp);
Expand Down Expand Up @@ -121,4 +123,4 @@ export const remove = async (
});

return res.status(200).json({ message: "Whatsapp deleted." });
};
};
2 changes: 1 addition & 1 deletion backend/src/controllers/WhatsAppSessionController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response } from "express";
import { getWbot } from "../libs/wbot";
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";

const store = async (req: Request, res: Response): Promise<Response> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DataTypes, QueryInterface } from "sequelize";

module.exports = {
up: async (queryInterface: QueryInterface): Promise<void> => {
await queryInterface.addColumn("Whatsapps", "color", {
type: DataTypes.STRING,
allowNull: true,
defaultValue: "#5C59A0"
});
},

down: async (queryInterface: QueryInterface): Promise<void> => {
await queryInterface.removeColumn("Whatsapps", "color");
}
};
7 changes: 7 additions & 0 deletions backend/src/models/Whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ class Whatsapp extends Model<Whatsapp> {
@Column
isDisplay: boolean;

@AllowNull
@Column({
type: DataType.STRING,
defaultValue: "#5C59A0"
})
color: string;

@CreatedAt
createdAt: Date;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const CreateMessageService = async ({
{
model: Whatsapp,
as: "whatsapp",
attributes: ["name"]
attributes: ["name", "color"]
}
]
},
Expand Down
43 changes: 33 additions & 10 deletions backend/src/services/TicketServices/FindOrCreateTicketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { subSeconds } from "date-fns";
import { Op } from "sequelize";
import Contact from "../../models/Contact";
import Ticket from "../../models/Ticket";
import Whatsapp from "../../models/Whatsapp";
import ListSettingsServiceOne from "../SettingServices/ListSettingsServiceOne";
import ShowTicketService from "./ShowTicketService";

Expand All @@ -20,7 +21,13 @@ const FindOrCreateTicketService = async (
},
contactId: groupContact ? groupContact.id : contact.id,
whatsappId
}
},
include: [
{
model: Whatsapp,
attributes: ["color"]
}
]
});

if (ticket) {
Expand All @@ -33,6 +40,12 @@ const FindOrCreateTicketService = async (
contactId: groupContact.id,
whatsappId
},
include: [
{
model: Whatsapp,
attributes: ["color"]
}
],
order: [["updatedAt", "DESC"]]
});

Expand Down Expand Up @@ -75,15 +88,25 @@ const FindOrCreateTicketService = async (
}

if (!ticket) {
ticket = await Ticket.create({
contactId: groupContact ? groupContact.id : contact.id,
status: "pending",
isGroup: !!groupContact,
userId,
queueId,
unreadMessages,
whatsappId
});
ticket = await Ticket.create(
{
contactId: groupContact ? groupContact.id : contact.id,
status: "pending",
isGroup: !!groupContact,
userId,
queueId,
unreadMessages,
whatsappId
},
{
include: [
{
model: Whatsapp,
attributes: ["color"]
}
]
}
);
}

ticket = await ShowTicketService(ticket.id);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/TicketServices/ListTicketsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const ListTicketsService = async ({
{
model: Whatsapp,
as: "whatsapp",
attributes: ["name", "type"]
attributes: ["name", "type", "color"]
}
];

Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/TicketServices/ShowTicketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ShowTicketService = async (id: string | number): Promise<Ticket> => {
{
model: Whatsapp,
as: "whatsapp",
attributes: ["name", "type"]
attributes: ["name", "type", "color"]
}
]
});
Expand Down
8 changes: 6 additions & 2 deletions backend/src/services/UserServices/ListUsersService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Sequelize, Op } from "sequelize";
import { Op, Sequelize } from "sequelize";
import Queue from "../../models/Queue";
import User from "../../models/User";
import Whatsapp from "../../models/Whatsapp";
Expand Down Expand Up @@ -50,7 +50,11 @@ const ListUsersService = async ({
order: [["createdAt", "DESC"]],
include: [
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] },
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] }
{
model: Whatsapp,
as: "whatsapp",
attributes: ["id", "name", "type", "color"]
}
]
});

Expand Down
14 changes: 10 additions & 4 deletions backend/src/services/WhatsappService/CreateWhatsAppService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface Request {
status?: string;
isDefault?: boolean;
isDisplay?: boolean;
color?: string;
}

interface Response {
Expand All @@ -26,7 +27,8 @@ const CreateWhatsAppService = async ({
greetingMessage,
farewellMessage,
isDefault = false,
isDisplay = false
isDisplay = false,
color = "#5C59A0"
}: Request): Promise<Response> => {
const schema = Yup.object().shape({
name: Yup.string()
Expand All @@ -43,11 +45,14 @@ const CreateWhatsAppService = async ({
return !nameExists;
}
),
isDefault: Yup.boolean().required()
isDefault: Yup.boolean().required(),
color: Yup.string()
.matches(/^#(?:[0-9a-fA-F]{3}){1,2}$/, "Invalid color format")
.nullable()
});

try {
await schema.validate({ name, status, isDefault });
await schema.validate({ name, status, isDefault, color });
} catch (err) {
throw new AppError(err.message);
}
Expand Down Expand Up @@ -78,7 +83,8 @@ const CreateWhatsAppService = async ({
greetingMessage,
farewellMessage,
isDefault,
isDisplay
isDisplay,
color
},
{ include: ["queues"] }
);
Expand Down
11 changes: 7 additions & 4 deletions backend/src/services/WhatsappService/UpdateWhatsAppService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as Yup from "yup";
import { Op } from "sequelize";
import * as Yup from "yup";

import AppError from "../../errors/AppError";
import Whatsapp from "../../models/Whatsapp";
import ShowWhatsAppService from "./ShowWhatsAppService";
import AssociateWhatsappQueue from "./AssociateWhatsappQueue";
import ShowWhatsAppService from "./ShowWhatsAppService";

interface WhatsappData {
name?: string;
Expand All @@ -15,6 +15,7 @@ interface WhatsappData {
farewellMessage?: string;
queueIds?: number[];
isDisplay?: boolean;
color?: string;
}

interface Request {
Expand Down Expand Up @@ -45,7 +46,8 @@ const UpdateWhatsAppService = async ({
greetingMessage,
farewellMessage,
isDisplay,
queueIds = []
queueIds = [],
color
} = whatsappData;

try {
Expand Down Expand Up @@ -78,7 +80,8 @@ const UpdateWhatsAppService = async ({
greetingMessage,
farewellMessage,
isDefault,
isDisplay
isDisplay,
color
});

await AssociateWhatsappQueue(whatsapp, queueIds);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const App = () => {
toolbarIcon: { main: system.color.lightTheme.toolbarIcon || "#ffffff" },
divide: { main: system.color.lightTheme.divide || "#E0E0E0" },
background: {
default: system.color.lightTheme.palette.background.default || "#ffffff",
paper: system.color.lightTheme.palette.background.paper || "#eeeeee",
default: system.color.lightTheme.palette.background.default || "#eeeeee",
paper: system.color.lightTheme.palette.background.paper || "#ffffff",
},
},
backgroundImage: `url(${lightBackground})`,
Expand Down
Loading

0 comments on commit 6a3ec20

Please sign in to comment.