2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
|
|
|
|
import { getIO } from "../libs/socket";
|
|
|
|
import AppError from "../errors/AppError";
|
|
|
|
|
|
|
|
import UpdateSettingService from "../services/SettingServices/UpdateSettingService";
|
|
|
|
import ListSettingsService from "../services/SettingServices/ListSettingsService";
|
2023-07-28 12:25:13 +00:00
|
|
|
import loadSettings from "../helpers/LoadSettings";
|
2023-08-08 15:09:03 +00:00
|
|
|
import updateSettingTicket from "../services/SettingServices/UpdateSettingTicket";
|
|
|
|
import SettingTicket from "../models/SettingTicket";
|
2023-09-16 14:45:44 +00:00
|
|
|
import Whatsapp from "../models/Whatsapp";
|
|
|
|
import whatsappOfficialNumberInfo from "../helpers/WhatsappOfficialNumberInfo";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
2023-07-20 15:30:26 +00:00
|
|
|
// if (req.user.profile !== "master") {
|
|
|
|
// throw new AppError("ERR_NO_PERMISSION", 403);
|
|
|
|
// }
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const settings = await ListSettingsService();
|
|
|
|
|
2024-02-01 20:16:56 +00:00
|
|
|
// const config = await SettingTicket.findAll();
|
2023-08-09 11:30:03 +00:00
|
|
|
|
2024-04-01 12:00:15 +00:00
|
|
|
return res.status(200).json({ settings });
|
2024-02-01 20:16:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const ticketSettings = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2024-04-01 12:00:15 +00:00
|
|
|
const { number } = req.params;
|
2024-02-01 20:16:56 +00:00
|
|
|
|
|
|
|
const config = await SettingTicket.findAll({ where: { number } });
|
|
|
|
|
|
|
|
return res.status(200).json({ config });
|
2023-08-08 15:09:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const updateTicketSettings = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2023-08-09 11:30:03 +00:00
|
|
|
const {
|
2024-02-01 20:16:56 +00:00
|
|
|
number,
|
2024-04-01 12:00:15 +00:00
|
|
|
saturdayBusinessTime,
|
2023-08-09 11:30:03 +00:00
|
|
|
outBusinessHours,
|
|
|
|
ticketExpiration,
|
|
|
|
weekend,
|
|
|
|
saturday,
|
|
|
|
sunday,
|
|
|
|
holiday
|
|
|
|
} = req.body;
|
2024-04-01 12:00:15 +00:00
|
|
|
|
2024-02-01 20:16:56 +00:00
|
|
|
if (!number) throw new AppError("No number selected", 400);
|
2023-08-08 15:09:03 +00:00
|
|
|
|
|
|
|
if (outBusinessHours && Object.keys(outBusinessHours).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...outBusinessHours,
|
2024-02-01 20:16:56 +00:00
|
|
|
key: "outBusinessHours",
|
|
|
|
number
|
2023-08-08 15:09:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-01 12:00:15 +00:00
|
|
|
if (saturdayBusinessTime && Object.keys(saturdayBusinessTime).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...saturdayBusinessTime,
|
|
|
|
key: "saturdayBusinessTime",
|
|
|
|
number
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-08 15:09:03 +00:00
|
|
|
if (ticketExpiration && Object.keys(ticketExpiration).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...ticketExpiration,
|
2024-02-01 20:16:56 +00:00
|
|
|
key: "ticketExpiration",
|
|
|
|
number
|
2023-08-08 15:09:03 +00:00
|
|
|
});
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-08-09 11:30:03 +00:00
|
|
|
if (weekend && Object.keys(weekend).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...weekend,
|
2024-02-01 20:16:56 +00:00
|
|
|
key: "weekend",
|
|
|
|
number
|
2023-08-09 11:30:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saturday && Object.keys(saturday).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...saturday,
|
2024-02-01 20:16:56 +00:00
|
|
|
key: "saturday",
|
|
|
|
number
|
2023-08-09 11:30:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sunday && Object.keys(sunday).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...sunday,
|
2024-02-01 20:16:56 +00:00
|
|
|
key: "sunday",
|
|
|
|
number
|
2023-08-09 11:30:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (holiday && Object.keys(holiday).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...holiday,
|
2024-02-01 20:16:56 +00:00
|
|
|
key: "holiday",
|
|
|
|
number
|
2023-08-09 11:30:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-16 14:45:44 +00:00
|
|
|
return res.status(200).json({
|
|
|
|
outBusinessHours,
|
|
|
|
ticketExpiration,
|
|
|
|
weekend,
|
|
|
|
saturday,
|
|
|
|
sunday,
|
|
|
|
holiday
|
|
|
|
});
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const update = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2022-01-13 10:05:08 +00:00
|
|
|
if (req.user.profile !== "master") {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
|
|
|
}
|
|
|
|
const { settingKey: key } = req.params;
|
|
|
|
const { value } = req.body;
|
|
|
|
|
|
|
|
const setting = await UpdateSettingService({
|
|
|
|
key,
|
|
|
|
value
|
|
|
|
});
|
|
|
|
|
2023-09-16 14:45:44 +00:00
|
|
|
if (key && key == "whatsaAppCloudApi") {
|
|
|
|
let whatsapps: any = await Whatsapp.findAll();
|
|
|
|
|
|
|
|
if (whatsapps) {
|
|
|
|
for (let i in whatsapps) {
|
|
|
|
const { id, wabaId, isOfficial } = whatsapps[i];
|
|
|
|
|
|
|
|
if (isOfficial && wabaId) {
|
|
|
|
try {
|
|
|
|
const whatsapp = await Whatsapp.findByPk(id);
|
|
|
|
if (whatsapp) {
|
|
|
|
if (value == "disabled") {
|
|
|
|
whatsapp.update({ status: "OPENING" });
|
|
|
|
} else if (value == "enabled") {
|
|
|
|
const info = await whatsappOfficialNumberInfo(wabaId);
|
|
|
|
if (info) {
|
|
|
|
whatsapp.update({
|
|
|
|
classification: info.quality_rating,
|
|
|
|
status: "CONNECTED"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(
|
|
|
|
"error on try update classification number from oficial whatsapp in SettingControllers.ts: ",
|
|
|
|
error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
loadSettings();
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("settings", {
|
|
|
|
action: "update",
|
|
|
|
setting
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).json(setting);
|
|
|
|
};
|