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";
|
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();
|
|
|
|
|
2023-08-09 11:30:03 +00:00
|
|
|
const config = await SettingTicket.findAll();
|
|
|
|
|
|
|
|
return res.status(200).json({ settings, 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 {
|
|
|
|
outBusinessHours,
|
|
|
|
ticketExpiration,
|
|
|
|
weekend,
|
|
|
|
saturday,
|
|
|
|
sunday,
|
|
|
|
holiday
|
|
|
|
} = req.body;
|
2023-08-08 15:09:03 +00:00
|
|
|
|
|
|
|
if (outBusinessHours && Object.keys(outBusinessHours).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...outBusinessHours,
|
|
|
|
key: "outBusinessHours"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ticketExpiration && Object.keys(ticketExpiration).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...ticketExpiration,
|
|
|
|
key: "ticketExpiration"
|
|
|
|
});
|
|
|
|
}
|
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,
|
|
|
|
key: "weekend"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saturday && Object.keys(saturday).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...saturday,
|
|
|
|
key: "saturday"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sunday && Object.keys(sunday).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...sunday,
|
|
|
|
key: "sunday"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (holiday && Object.keys(holiday).length > 0) {
|
|
|
|
await updateSettingTicket({
|
|
|
|
...holiday,
|
|
|
|
key: "holiday"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
};
|