Parcourir la source

Notificaciones separadas por grupos

ElPoteito il y a 1 an
Parent
commit
53ad7288b9
1 fichiers modifiés avec 49 ajouts et 46 suppressions
  1. 49 46
      commands/EventoController.php

+ 49 - 46
commands/EventoController.php

@@ -813,14 +813,16 @@ class EventoController extends Controller {
     $eventos = (new Query())
       ->select([
         "nombre",
-        "tag"
+        "tag",
+        "{{EventoGrupo}}.[[idGrupo]]"
       ])
       ->from("Evento")
+      ->innerJoin("EventoGrupo", "{{EventoGrupo}}.[[idEvento]] = {{Evento}}.id")
       ->andWhere([">=", "fechaFinal", new Expression("now() - interval '7 days'")])
       // ->andWhere([">=", "fechaFinal", '2023-10-02 00:00:00'])
       // ->andWhere(["<=", "fechaFinal", '2023-10-08 00:00:00'])
       ->andWhere(["eliminado" => null])
-      ->groupBy(["tag", "nombre"])
+      ->groupBy(["tag", "nombre", "[[idGrupo]]"])
       ->indexBy("tag")
       ->column();
 
@@ -834,13 +836,6 @@ class EventoController extends Controller {
       ->indexBy("id")
       ->column();
 
-    $totalEventos = count($eventos);
-    $this->stdout("{$totalEventos}\n");
-    if ($totalEventos === 0) {
-      # ¿Si no hubo eventos en el período de tiempo también hay que avisar?
-      return;
-    }
-
     $query2 = (new Query())
       ->select([
         "tag",
@@ -963,6 +958,14 @@ class EventoController extends Controller {
       # Es mejor guardar el valor del count en lugar de evaluarlo multiples veces
       $totalParticipaciones = count($usuario["eventosParticipo"]);
 
+      $totalEventos = 0;
+      $totalEventos = count($usuario["eventosParticipo"]) + count($usuario["eventosFaltantes"]);
+      $this->stdout("{$totalEventos}\n");
+      if ($totalEventos === 0) {
+        # ¿Si no hubo eventos en el período de tiempo también hay que avisar?
+        return;
+      }
+
       $parametros = [
         [
           "type" => "text",
@@ -1287,51 +1290,51 @@ class EventoController extends Controller {
       $notificacion = new Notificacion();
       $notificacion->creado = new Expression("now()");
 
-    if (!$notificacion->save()) {
-      $transaccion->rollBack();
-      $errores = json_encode($notificacion->getFirstErrors());
-      $this->stdoutln('Hubo un problema al generar la notificación: ' . $errores);
-      return;
-    }
+      if (!$notificacion->save()) {
+        $transaccion->rollBack();
+        $errores = json_encode($notificacion->getFirstErrors());
+        $this->stdoutln('Hubo un problema al generar la notificación: ' . $errores);
+        return;
+      }
 
-    foreach ($query->each() as $usuario) {
-      $notificacionUsuario = new NotificacionUsuario();
+      foreach ($query->each() as $usuario) {
+        $notificacionUsuario = new NotificacionUsuario();
 
-      $this->stdoutln("idUsuario: {$usuario['id']}");
-      $this->stdoutln("nombre: {$usuario['nombre']}");
-      $this->stdoutln("teléfono: {$usuario['telefono']}");
+        $this->stdoutln("idUsuario: {$usuario['id']}");
+        $this->stdoutln("nombre: {$usuario['nombre']}");
+        $this->stdoutln("teléfono: {$usuario['telefono']}");
 
-      $notificacionUsuario->idNotificacion = $notificacion->id;
-      $notificacionUsuario->idUsuario = intval($usuario['id']);
-      $notificacionUsuario->nombre = $usuario['nombre'];
-      $notificacionUsuario->telefono = $usuario['telefono'];
-      $notificacionUsuario->plantilla = Notificacion::PLANTILLA_LIDER_GLOBAL_URL;
+        $notificacionUsuario->idNotificacion = $notificacion->id;
+        $notificacionUsuario->idUsuario = intval($usuario['id']);
+        $notificacionUsuario->nombre = $usuario['nombre'];
+        $notificacionUsuario->telefono = $usuario['telefono'];
+        $notificacionUsuario->plantilla = Notificacion::PLANTILLA_LIDER_GLOBAL_URL;
 
-      $parametros = [
-        [
-          "type" => "text",
-          "text" => "El día de ayer hubo una falla en el sistema al enviar los reportes de la semana, se esta solucionando. "
-        ],
-        [
-          "type" => "text",
-          "text" => "Gracias por su compresión."
-        ],
-      ];
-      $notificacionUsuario->parametros = $parametros;
+        $parametros = [
+          [
+            "type" => "text",
+            "text" => "El día de ayer hubo una falla en el sistema al enviar los reportes de la semana, se esta solucionando. "
+          ],
+          [
+            "type" => "text",
+            "text" => "Gracias por su compresión."
+          ],
+        ];
+        $notificacionUsuario->parametros = $parametros;
 
-      if (!$notificacionUsuario->save()) {
-        $transaccion->rollBack();
-        $errores = json_encode($notificacionUsuario->getFirstErrors());
-        $this->stdoutln('Hubo un problema al generar la notificación: ' . $errores);
-        return;
+        if (!$notificacionUsuario->save()) {
+          $transaccion->rollBack();
+          $errores = json_encode($notificacionUsuario->getFirstErrors());
+          $this->stdoutln('Hubo un problema al generar la notificación: ' . $errores);
+          return;
+        }
       }
-    }
 
-    $notificacion->envio = new Expression('now()');
-    $notificacion->estatus = 'Nuevo';
-    $notificacion->save();
+      $notificacion->envio = new Expression('now()');
+      $notificacion->estatus = 'Nuevo';
+      $notificacion->save();
 
-    $transaccion->commit();
+      $transaccion->commit();
     } catch (\Exception $e) {
       $transaccion->rollBack();
       $this->stdoutln("Exception: {$e->getMessage()}\n");