SlaSaveConfigController.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Controllers;
  3. use Libs\Payload;
  4. use Libs\Validator;
  5. use Models\SlaConfigsModel;
  6. use Models\UserModel;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. class SlaSaveConfigController
  9. {
  10. private UserModel $userModel;
  11. private SlaConfigsModel $slaConfigsModel;
  12. public function __construct()
  13. {
  14. $this->userModel = new UserModel();
  15. $this->slaConfigsModel = new SlaConfigsModel();
  16. }
  17. public function __invoke(ServerRequestInterface $request)
  18. {
  19. $userId = (int) ($request->getAttribute('user_id') ?? 0);
  20. if ($userId <= 0) {
  21. return Payload::fail('Unauthorized: Missing authenticated user', [], 'E_VALIDATE', 401);
  22. }
  23. $body = json_decode((string) $request->getBody(), true) ?: [];
  24. $id = trim((string) ($body['id'] ?? ''));
  25. $name = trim((string) ($body['name'] ?? ''));
  26. $firstResponseH = $body['firstResponseH'] ?? null;
  27. $firstResponseM = $body['firstResponseM'] ?? null;
  28. $resolutionH = $body['resolutionH'] ?? null;
  29. $alertPct = $body['alertPct'] ?? null;
  30. $validator = (new Validator([
  31. 'firstResponseH' => $firstResponseH,
  32. 'firstResponseM' => $firstResponseM,
  33. 'resolutionH' => $resolutionH,
  34. 'alertPct' => $alertPct,
  35. 'name' => $name,
  36. ]))
  37. ->required('firstResponseH')->intRange('firstResponseH', 0, 72)
  38. ->required('firstResponseM')->intRange('firstResponseM', 0, 59)
  39. ->required('resolutionH')->intRange('resolutionH', 1, 720)
  40. ->required('alertPct')->intRange('alertPct', 1, 99)
  41. ->maxLength('name', 20);
  42. if ($id === '') {
  43. $validator->required('name');
  44. }
  45. if ($validator->fails()) {
  46. return Payload::fail($validator->firstError() ?? 'Invalid payload', [], 'E_VALIDATE', 400);
  47. }
  48. $companyId = $this->userModel->getCompanyIdByUserId($userId);
  49. if ($companyId === null) {
  50. return Payload::fail('User not found', [], 'E_NOT_FOUND', 404);
  51. }
  52. try {
  53. $result = $this->slaConfigsModel->saveConfig($companyId, $body);
  54. } catch (\Throwable $e) {
  55. return Payload::fail('Failed to save SLA config', [], 'E_GENERIC', 500);
  56. }
  57. if (($result['status'] ?? '') === 'not_found') {
  58. return Payload::fail('SLA config not found', [], 'E_NOT_FOUND', 404);
  59. }
  60. if (($result['status'] ?? '') === 'invalid') {
  61. return Payload::fail('Invalid payload', [], 'E_VALIDATE', 400);
  62. }
  63. if (($result['status'] ?? '') === 'duplicate') {
  64. return Payload::fail('Department already has an SLA config', [], 'E_VALIDATE', 400);
  65. }
  66. if (($result['status'] ?? '') === 'error' || !isset($result['item'])) {
  67. return Payload::fail('Failed to save SLA config', [], 'E_GENERIC', 500);
  68. }
  69. if (($result['status'] ?? '') === 'created') {
  70. return Payload::ok($result['item'], 'S_CREATED', 'SLA config created.');
  71. }
  72. return Payload::ok($result['item'], 'S_OK', 'SLA config updated.');
  73. }
  74. }