NotificationsController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Carbon;
  7. use ElephantIO\Client;
  8. class NotificationsController extends Controller{
  9. private $responseController;
  10. private $encryptionController;
  11. private $functionsController;
  12. private $documentManagementController;
  13. public function __construct(){
  14. $this->responseController = new ResponseController();
  15. $this->encryptionController = new EncryptionController();
  16. $this->functionsController = new FunctionsController();
  17. $this->documentManagementController = new DocumentManagementController();
  18. }
  19. public function emitNotification(
  20. string $module,
  21. string $title,
  22. string $content,
  23. array $actions,
  24. array $audience,
  25. string $idUser,
  26. int $line,
  27. object $socket,
  28. int $idOrder = null,
  29. string $orderType = null
  30. ) : bool {
  31. $moduleObj = DB::table('S002V01TMODU')->where([
  32. ['MODU_NULI', '=', $line],
  33. ['MODU_IDMO', '=', $module]
  34. ])->first();
  35. if(is_null($moduleObj)) return false;
  36. if(strlen($title) < 1 || strlen($title) > 150) return false;
  37. $scope = [];
  38. foreach($audience as $user){
  39. $scope[] = [
  40. 'USUARIO' => $user,
  41. 'ESTADO' => 'No leído'
  42. ];
  43. }
  44. $now = $this->functionsController->now();
  45. $nowStr = $now->toDateTimeString();
  46. $audienceStr = json_encode($audience);
  47. $idNotification = DB::table('S002V01TNOTI')->insertGetId([
  48. 'NOTI_NULI' => $line,
  49. 'NOTI_IDMO' => $module,
  50. 'NOTI_ASUN' => $title,
  51. 'NOTI_CONT' => $content,
  52. 'NOTI_IDOT' => $idOrder,
  53. 'NOTI_TIOR' => $orderType,
  54. 'NOTI_ACCI' => json_encode($actions),
  55. 'NOTI_AUDI' => $audienceStr,
  56. 'NOTI_ALCA' => json_encode($scope),
  57. 'NOTI_USRE' => $idUser,
  58. 'NOTI_FERE' => $nowStr,
  59. ]);
  60. $idNotificationEnc = $this->encryptionController->encrypt($idNotification);
  61. $audienceEnc = $this->encryptionController->encrypt($audienceStr);
  62. $notificationData = ['idNotification' => $idNotificationEnc, 'audience' => $audienceEnc];
  63. $socket->emit('new_notification', $notificationData);
  64. return true;
  65. }
  66. public function getNotification($idNotification, $setRead, $idUser, $line) {
  67. DB::enableQueryLog();
  68. $idUser = $this->encryptionController->decrypt($idUser);
  69. if(!$idUser){
  70. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  71. }
  72. $usr = DB::table('S002V01TUSUA')->where([
  73. ['USUA_NULI', '=', $line],
  74. ['USUA_IDUS', '=', $idUser],
  75. ])->first();
  76. if(is_null($usr)){
  77. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  78. }
  79. $idNotification = $this->encryptionController->decrypt($idNotification);
  80. if(!$idNotification){
  81. return $this->responseController->makeResponse(true, 'El ID de la notificación solicitada no está encriptado correctamente', [], 400);
  82. }
  83. $notification = DB::table('S002V01TNOTI')->select([
  84. 'NOTI_IDNO AS ID_NOTIFICACION',
  85. 'NOTI_IDMO AS ID_MODULO',
  86. 'MODU_NOMO AS MODULO',
  87. 'NOTI_ASUN AS TITULO',
  88. 'NOTI_CONT AS CUERPO_NOTIFICACION',
  89. 'NOTI_IDOT AS ID_ORDEN',
  90. 'NOTI_TIOR AS TIPO_ORDEN',
  91. 'NOTI_ACCI AS ACCIONES',
  92. 'NOTI_AUDI AS AUDIENCIA',
  93. 'NOTI_ALCA AS ALCANCE',
  94. 'NOTI_USRE AS USRREG',
  95. 'NOTI_FERE AS FECREG',
  96. ])->join('S002V01TMODU', 'MODU_IDMO', '=', 'NOTI_IDMO')->where([
  97. ['NOTI_NULI', '=', $line],
  98. ['NOTI_IDNO', '=', $idNotification]
  99. ])->first();
  100. if(is_null($notification)){
  101. return $this->responseController->makeResponse(true, 'La notificación consultada no está registrada.', [], 404);
  102. }
  103. $notification->ID_NOTIFICACION = $this->encryptionController->encrypt($notification->ID_NOTIFICACION);
  104. $notification->ID_MODULO = $this->encryptionController->encrypt($notification->ID_MODULO);
  105. if(!is_null($notification->ID_ORDEN)){
  106. $notification->ID_ORDEN = $this->encryptionController->encrypt($notification->ID_ORDEN);
  107. }
  108. $audienceArr = json_decode($notification->AUDIENCIA, true);
  109. if(!in_array($idUser, $audienceArr)){
  110. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no puede leer la notificación.', [], 401);
  111. }
  112. foreach($audienceArr as $key=>$user){
  113. $audienceArr[$key] = $this->encryptionController->encrypt($user);
  114. }
  115. $notification->AUDIENCIA = json_encode($audienceArr);
  116. $scopeArr = json_decode($notification->ALCANCE, true);
  117. foreach($scopeArr as $key=>$item){
  118. $item['USUARIO'] = $this->encryptionController->encrypt($item['USUARIO']);
  119. $scopeArr[$key] = $item;
  120. }
  121. if($setRead == 'S'){
  122. $scopeArrUpd = json_decode($notification->ALCANCE, true);
  123. foreach($scopeArrUpd as $key=>$item){
  124. if($item['USUARIO'] == $idUser){
  125. $item['ESTADO'] = 'Leído';
  126. $scopeArrUpd[$key] = $item;
  127. }
  128. }
  129. $scopeStrUpd = json_encode($scopeArrUpd);
  130. DB::table('S002V01TNOTI')->where([
  131. ['NOTI_NULI', '=', $line],
  132. ['NOTI_IDNO', '=', $idNotification]
  133. ])->update([
  134. 'NOTI_ALCA' => $scopeStrUpd,
  135. ]);
  136. }
  137. $notification->ALCANCE = json_encode($scopeArr);
  138. $usrReg = DB::table('S002V01TUSUA')->where([
  139. ['USUA_NULI', '=', $line],
  140. ['USUA_IDUS', '=', $notification->USRREG]
  141. ])->first();
  142. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  143. $notification->USRREG = $nameReg . ' (' . $notification->USRREG . ')';
  144. $now = $this->functionsController->now();
  145. $nowStr = $now->toDateTimeString();
  146. $actions = DB::getQueryLog();
  147. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  148. $idac = $this->functionsController->registerActivity(
  149. $line,
  150. '-',
  151. '-',
  152. '-',
  153. 'Consulta',
  154. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la notificación #$idNotification.",
  155. $idUser,
  156. $nowStr,
  157. );
  158. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  159. return $this->responseController->makeResponse(false, 'EXITO.', $notification);
  160. }
  161. public function getNotificationsByUser($idUser, $line) {
  162. DB::enableQueryLog();
  163. $idUser = $this->encryptionController->decrypt($idUser);
  164. if(!$idUser){
  165. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  166. }
  167. $usr = DB::table('S002V01TUSUA')->where([
  168. ['USUA_NULI', '=', $line],
  169. ['USUA_IDUS', '=', $idUser],
  170. ])->first();
  171. if(is_null($usr)){
  172. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  173. }
  174. $notifications = DB::table('S002V01TNOTI')->select([
  175. 'NOTI_IDNO AS ID_NOTIFICACION',
  176. 'NOTI_IDMO AS ID_MODULO',
  177. 'MODU_NOMO AS MODULO',
  178. 'NOTI_ASUN AS TITULO',
  179. 'NOTI_CONT AS CUERPO_NOTIFICACION',
  180. 'NOTI_IDOT AS ID_ORDEN',
  181. 'NOTI_TIOR AS TIPO_ORDEN',
  182. 'NOTI_AUDI AS AUDIENCIA',
  183. 'NOTI_ALCA AS ALCANCE',
  184. 'NOTI_USRE AS USRREG',
  185. 'NOTI_FERE AS FECREG',
  186. ])->join('S002V01TMODU', 'MODU_IDMO', '=', 'NOTI_IDMO')
  187. ->where('NOTI_NULI', '=', $line)
  188. ->whereJsonContains('NOTI_AUDI', $idUser)
  189. ->orderBy('NOTI_FERE', 'desc')->get()->all();
  190. foreach($notifications as $key=>$notification){
  191. $notification->ID_NOTIFICACION = $this->encryptionController->encrypt($notification->ID_NOTIFICACION);
  192. $notification->ID_MODULO = $this->encryptionController->encrypt($notification->ID_MODULO);
  193. if(!is_null($notification->ID_ORDEN)){
  194. $notification->ID_ORDEN = $this->encryptionController->encrypt($notification->ID_ORDEN);
  195. }
  196. $audienceArr = json_decode($notification->AUDIENCIA, true);
  197. foreach($audienceArr as $k=>$v){
  198. $audienceArr[$k] = $this->encryptionController->encrypt($v);
  199. }
  200. $notification->AUDIENCIA = json_encode($audienceArr);
  201. $scopeArr = json_decode($notification->ALCANCE, true);
  202. foreach($scopeArr as $k=>$v){
  203. $v['USUARIO'] = $this->encryptionController->encrypt($v['USUARIO']);
  204. $scopeArr[$k] = $v;
  205. }
  206. $notification->ALCANCE = json_encode($scopeArr);
  207. $usrReg = DB::table('S002V01TUSUA')->where([
  208. ['USUA_NULI', '=', $line],
  209. ['USUA_IDUS', '=', $notification->USRREG]
  210. ])->first();
  211. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  212. $notification->USRREG = $nameReg . " (" . $notification->USRREG . ")";
  213. $notifications[$key] = $notification;
  214. }
  215. $now = $this->functionsController->now();
  216. $nowStr = $now->toDateTimeString();
  217. $actions = DB::getQueryLog();
  218. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  219. $idac = $this->functionsController->registerActivity(
  220. $line,
  221. '-',
  222. '-',
  223. '-',
  224. 'Consulta',
  225. "El usuario $name (" . $usr->USUA_IDUS . ") consultó su feed de notificaciones.",
  226. $idUser,
  227. $nowStr,
  228. );
  229. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  230. return $this->responseController->makeResponse(false, 'EXITO.', $notifications);
  231. }
  232. }