diff --git a/src/Command/ContractEndNotificationCommand.php b/src/Command/ContractEndNotificationCommand.php new file mode 100644 index 0000000..d52d8a3 --- /dev/null +++ b/src/Command/ContractEndNotificationCommand.php @@ -0,0 +1,78 @@ +addOption( + 'date', + null, + InputOption::VALUE_REQUIRED, + 'Override the reference day (YYYY-MM-DD) for testing or manual catch-up.' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $dateOption = $input->getOption('date'); + + try { + $today = is_string($dateOption) && '' !== $dateOption + ? new DateTimeImmutable($dateOption) + : new DateTimeImmutable('today'); + } catch (Throwable $exception) { + $io->error(sprintf('Invalid --date value: %s', $exception->getMessage())); + + return Command::INVALID; + } + + $result = $this->service->run($today); + + $this->logger->info('Contract end notifications generated.', [ + 'date' => $today->format('Y-m-d'), + 'contractsMatched' => $result->contractsMatched, + 'notificationsCreated' => $result->notificationsCreated, + ]); + + $io->success(sprintf( + '%d notification(s) créée(s) pour %d fin(s) de contrat (%s).', + $result->notificationsCreated, + $result->contractsMatched, + $today->format('Y-m-d'), + )); + + return Command::SUCCESS; + } +} diff --git a/src/Service/Notification/ContractEndNotificationResult.php b/src/Service/Notification/ContractEndNotificationResult.php new file mode 100644 index 0000000..cf0464e --- /dev/null +++ b/src/Service/Notification/ContractEndNotificationResult.php @@ -0,0 +1,13 @@ +periodRepository->findLatestPeriodsForAllEmployees(); + $notices = $this->planner->plan($latestPeriods, $today); + + if ([] === $notices) { + return new ContractEndNotificationResult(0, 0); + } + + $admins = $this->userRepository->findAllAdmins(); + $created = 0; + + foreach ($notices as $notice) { + if (null === $notice->employeeId) { + continue; + } + + $target = '/employees/'.$notice->employeeId; + + foreach ($admins as $admin) { + if ($this->notificationRepository->existsForRecipientCategoryTargetMessage( + $admin, + self::CATEGORY, + $target, + $notice->message, + )) { + continue; + } + + $notification = new Notification(); + $notification->setRecipient($admin) + ->setMessage($notice->message) + ->setCategory(self::CATEGORY) + ->setTarget($target) + ; + + $this->entityManager->persist($notification); + ++$created; + } + } + + $this->entityManager->flush(); + + return new ContractEndNotificationResult($created, count($notices)); + } +}