فهرست منبع

Optimizaciones

Hugo Quijada 2 سال پیش
والد
کامیت
084b3a9f22
1فایلهای تغییر یافته به همراه27 افزوده شده و 30 حذف شده
  1. 27 30
      commands/EventoController.php

+ 27 - 30
commands/EventoController.php

@@ -775,11 +775,17 @@ class EventoController extends Controller {
         "tag"
       ])
       ->from("Evento")
-      ->andWhere([">=", "fechaFinal", new Expression("now()-interval '7 days'")])
+      ->andWhere([">=", "fechaFinal", new Expression("now() - interval '7 days'")])
       ->groupBy(["tag", "nombre"])
       ->indexBy("tag")
       ->column();
 
+    $totalEventos = count($eventos);
+    if($totalEventos === 0) {
+      # ¿Si no hubo eventos en el período de tiempo también hay que avisar?
+      return;
+    }
+
       $query2 = (new Query())
         ->select([
           "tag",
@@ -835,7 +841,8 @@ class EventoController extends Controller {
           return (new Respuesta($notificacion))
             ->mensaje("Hubo un problema al generar la notificación");
         }
-        $notificacion->refresh();
+        # TODO: Revisar si es necesario consultar la información del modelo
+        # $notificacion->refresh();
 
         foreach ($aux as $usuario) {
           $notificacionUsuario = new NotificacionUsuario();
@@ -844,50 +851,40 @@ class EventoController extends Controller {
           $notificacionUsuario->idUsuario = $usuario['id'];
           $notificacionUsuario->nombre = $usuario['nombreUsuario'];
           $notificacionUsuario->telefono = $usuario['telefono'];
-          if (count($usuario["eventosParticipo"]) === count($eventos)){
-            $notificacionUsuario->parametros = [
-              [
-                "type" => "text",
-                "text" => count($usuario["eventosParticipo"])."/".count($eventos). "eventos,"
-              ],
+
+          # Es mejor guardar el valor del count en lugar de evaluarlo multiples veces
+          $totalParticipaciones = count($usuario["eventosParticipo"]);
+
+          $parametros = [
+            [
+              "type" => "text",
+              "text" => "{$totalParticipaciones}/{$totalEventos} eventos"
+            ],
+          ];
+          
+          if ($totalParticipaciones === $totalEventos){
+            $parametros[] = [
               [
                 "type" => "text",
                 "text" => "gracias por tu"
               ]
             ];
-          } else if (count($usuario["eventosParticipo"]) > 0 && count($usuario["eventosParticipo"]) < count($eventos)) {
-            $notificacionUsuario->parametros = [
-              [
-                "type" => "text",
-                "text" => count($usuario["eventosParticipo"])."/".count($eventos). "eventos,"
-              ],
+          } else if ($totalParticipaciones > 0) {
+            $parametros[] = [
               [
                 "type" => "text",
                 "text" => "gracias por tu participación. Vamos por el 100% de"
               ]
             ];
-          } else if (count($usuario["eventosParticipo"]) === 0) {
-            $notificacionUsuario->parametros = [
-              [
-                "type" => "text",
-                "text" => count($usuario["eventosParticipo"])."/".count($eventos). "eventos."
-              ],
+          } else {
+            $parametros[] = [
               [
                 "type" => "text",
                 "text" => "Vamos por el 100% de"
               ]
             ];
           }
-          /* $notificacionUsuario->parametros = [
-            [
-              "type" => "text",
-              "text" => count($usuario["eventosParticipo"])."/".count($eventos)
-            ],
-            [
-              "type" => "text",
-              "text" => "gracias por tu"
-            ]
-          ]; */
+          $notificacionUsuario->parametros = $parametros;
 
           if (!$notificacionUsuario->save()) {
             return (new Respuesta($notificacionUsuario))