projeto-hit/backend/src/controllers/SettingController.ts

73 lines
1.9 KiB
TypeScript
Raw Normal View History

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";
import updateSettingTicket from "../services/SettingServices/UpdateSettingTicket";
import SettingTicket from "../models/SettingTicket";
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);
// }
const settings = await ListSettingsService();
const outBusinessHours = await SettingTicket.findOne({
where: { key: "outBusinessHours" }
});
const ticketExpiration = await SettingTicket.findOne({
where: { key: "ticketExpiration" }
});
return res.status(200).json({ settings, outBusinessHours, ticketExpiration });
};
export const updateTicketSettings = async (
req: Request,
res: Response
): Promise<Response> => {
const { outBusinessHours, ticketExpiration } = req.body;
if (outBusinessHours && Object.keys(outBusinessHours).length > 0) {
await updateSettingTicket({
...outBusinessHours,
key: "outBusinessHours"
});
}
if (ticketExpiration && Object.keys(ticketExpiration).length > 0) {
await updateSettingTicket({
...ticketExpiration,
key: "ticketExpiration"
});
}
return res.status(200).json({ outBusinessHours, ticketExpiration });
};
export const update = async (
req: Request,
res: Response
): Promise<Response> => {
if (req.user.profile !== "master") {
throw new AppError("ERR_NO_PERMISSION", 403);
}
const { settingKey: key } = req.params;
const { value } = req.body;
const setting = await UpdateSettingService({
key,
value
});
const io = getIO();
io.emit("settings", {
action: "update",
setting
});
return res.status(200).json(setting);
};