Commit 1d7dacbb authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

La commande 'notifications:rebuild' a été ajoutée, elle permet de recalculer...

La commande 'notifications:rebuild' a été ajoutée, elle permet de recalculer toutes les notifications des activités ayant le statut 'Active'.
Des options seront ajoutées pour étendre cette limitation
parent 5674d8f8
Loading
Loading
Loading
Loading
Loading
+32 −10
Changes for module/Oscar/src/Oscar/Command/OscarNotificationsRebuildCommand.php: 32 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -13,36 +13,58 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class OscarActivitySearchReindexCommand extends OscarAdvancedCommandAbstract
class OscarNotificationsRebuildCommand extends OscarAdvancedCommandAbstract
{
    protected static $defaultName = OscarCommandAbstract::COMMAND_ACTIVITY_SEARCH_REINDEX;
    protected static $defaultName = OscarCommandAbstract::COMMAND_NOTIFICATIONS_REBUILD;

    const ARGUMENT_ACTIVITY_ID = 'activityid';
    const OPTION_INCLUDE_PAST = 'include-past';
    const OPTION_PURGE = 'purge-old';

    protected function configure()
    {
        parent::configure();
        $this
            ->setDescription("Reconstruction de l'index de recherche pour une activité")
            ->addArgument(self::ARGUMENT_ACTIVITY_ID, InputArgument::REQUIRED, "ID de l'activité");
            ->setDescription("Reconstruction des notifications des activités de recherche")
            ->addOption(
                self::OPTION_INCLUDE_PAST,
                null,
                InputArgument::OPTIONAL,
                "Forcer le recalcule des notifications pour les activités"
            )
            ->addOption(
                self::OPTION_PURGE,
                null,
                InputArgument::OPTIONAL,
                "Supprime TOUTES les notifications avant de recalculer"
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        parent::execute($input, $output);

        $activityId = $input->getArgument(self::ARGUMENT_ACTIVITY_ID);
        $includePast = $input->getOption(self::OPTION_INCLUDE_PAST);
        $purgeBefore = $input->getOption(self::OPTION_PURGE);

        try {
            $activity = $this->getProjectGrantService()->getActivityById($activityId);

            if (!$this->ask("Réindexer l'activité '$activity' ?")) {
            // TODO Récupérer les activités en fonction des critères
            $activities = $this->getProjectGrantService()->getActivityRepository()->getActivitiesActive();

            if (!$this->ask("Recalculer les notifications pour ces ". count($activities) ." activité(s) ?")) {
                return 0;
            }

            $this->getProjectGrantService()->searchUpdate($activity);
            foreach ($activities as $activity) {
                $this->info("Recalcule des notifications pour l'activité '$activity'");
                try {
                    $this->getProjectGrantService()->getNotificationService()->updateNotificationsActivity($activity);
                } catch (Exception $e) {
                    return $this->finalFatalError($e);
                }
            }

            return $this->finalSuccess("Index de recherche mis à jour pour '$activity' mis à jour");
            return $this->finalSuccess("Les notifications ont été recalculées");
        } catch (Exception $e) {
            return $this->finalFatalError($e);
        }
+17 −0
Changes for module/Oscar/src/Oscar/Entity/ActivityRepository.php: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -17,6 +17,23 @@ use Oscar\Utils\DateTimeUtils;
 */
class ActivityRepository extends EntityRepository
{

    /**
     * @param null $limitEnd
     * @param bool $statusActive
     * @return Activity[]
     */
    public function getActivitiesActive( $limitEnd = null, $statusActive = true ){
        $queryBuilder = $this->createQueryBuilder('a')
            ->where('a.status = :status');

        $queryBuilder->setParameters([
            'status' => Activity::STATUS_ACTIVE
        ]);

        return $queryBuilder->getQuery()->getResult();
    }

    /**
     * Retourne la liste des IDS des activités impliquant l'organisation (avec un rôle principal).
     *
+20 −7
Changes for module/Oscar/src/Oscar/Service/NotificationService.php: 20 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -382,11 +382,10 @@ class NotificationService implements UseServiceContainer
        /** @var ActivityPayment $payment */
        foreach ($activity->getPayments() as $payment) {

            $dateNotification = $payment->getStatus() == ActivityPayment::STATUS_REALISE ? $payment->getDatePayment() : $payment->getDatePredicted();

            $message = "$payment dans l'activité " . $activity->log();
            $context = "payment:" . $payment->getId();

            // Prévisionnel
            if( $payment->getStatus() == ActivityPayment::STATUS_PREVISIONNEL ){
                if( $payment->getDatePredicted() < $now ) {
                    $message .= " EST EN RETARD";
@@ -394,11 +393,27 @@ class NotificationService implements UseServiceContainer
                    $message .= " EST PREVU";
                }
                $dateNotification = $payment->getDatePredicted();
            } else {
            }

            // écart de payment
            else if ( $payment->getStatus() == ActivityPayment::STATUS_ECART ){
                $dateNotification = $payment->getDatePredicted();
                if( $dateNotification == null ){
                    $dateNotification = $payment->getDatePayment();
                }
            }

            // Réalisé
            else {
                $message .= " EST REALISE";
                $dateNotification = $payment->getDatePayment();
            }

            if( $dateNotification == null ){
                $this->getLoggerService()->alert(sprintf("Le payment [%s]%s a un problème de date dans %s", $payment->getId(), $payment, $activity));
                continue;
            }

            $hashs[] = $this->buildNotificationCore(
                $message,
                Notification::OBJECT_ACTIVITY,
@@ -536,7 +551,6 @@ class NotificationService implements UseServiceContainer
     */
    public function updateNotificationsActivity(Activity $activity)
    {
        $this->getLoggerService()->debug("Mise à jour des notifications pour l'activité $activity");
        $this->updateNotificationCore($activity);
        $this->updateNotificationsMilestonePersonActivity($activity);
    }
@@ -548,7 +562,7 @@ class NotificationService implements UseServiceContainer
        }
    }

    public function updateNotificationsMilestonePersonActivity(Activity $activity)
    public function updateNotificationsMilestonePersonActivity(Activity $activity, $ignorePast = true)
    {
        $this->getLoggerService()->debug(" ## updateNotificationsMilestonePersonActivity $activity : ");
        // Liste des notifications Programmées
@@ -566,8 +580,7 @@ class NotificationService implements UseServiceContainer
            $idsExpectedSubscribers[] = $p->getId();
        }

        $ignorePast = false;
        $now = new \DateTime('now');
        $now = (new \DateTime('now'))->modify('-1 month');

        foreach ($notificationsActivity as $na) {
            $idsPersonInPlace = [];
+1 −1
Changes for module/Oscar/src/Oscar/Service/ProjectGrantService.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -152,7 +152,7 @@ class ProjectGrantService implements UseOscarConfigurationService, UseEntityMana
    /**
     * @return ActivityRepository
     */
    protected function getActivityRepository()
    public function getActivityRepository()
    {
        return $this->getEntityManager()->getRepository(Activity::class);
    }