NotificationsController.php 11 KB

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