SettingController.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { Request, Response } from "express";
  2. import { getIO } from "../libs/socket";
  3. import AppError from "../errors/AppError";
  4. import UpdateSettingService from "../services/SettingServices/UpdateSettingService";
  5. import ListSettingsService from "../services/SettingServices/ListSettingsService";
  6. export const index = async (req: Request, res: Response): Promise<Response> => {
  7. const { companyId } = req.user;
  8. // if (req.user.profile !== "admin") {
  9. // throw new AppError("ERR_NO_PERMISSION", 403);
  10. // }
  11. const settings = await ListSettingsService({ companyId });
  12. return res.status(200).json(settings);
  13. };
  14. export const update = async (
  15. req: Request,
  16. res: Response
  17. ): Promise<Response> => {
  18. if (req.user.profile !== "admin") {
  19. throw new AppError("ERR_NO_PERMISSION", 403);
  20. }
  21. const { settingKey: key } = req.params;
  22. const { value } = req.body;
  23. const { companyId } = req.user;
  24. const setting = await UpdateSettingService({
  25. key,
  26. value,
  27. companyId
  28. });
  29. const io = getIO();
  30. io.to(`company-${companyId}-mainchannel`).emit(`company-${companyId}-settings`, {
  31. action: "update",
  32. setting
  33. });
  34. return res.status(200).json(setting);
  35. };