|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace totum\commands; |
| 5 | + |
| 6 | +use Symfony\Component\Console\Command\Command; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use totum\common\configs\ConfParent; |
| 11 | +use totum\common\configs\MultiTrait; |
| 12 | +use totum\common\Services\Services; |
| 13 | +use totum\config\Conf; |
| 14 | + |
| 15 | +class SwitchOffExtraNotifications extends Command |
| 16 | +{ |
| 17 | + protected function configure() |
| 18 | + { |
| 19 | + $this->setName('switch-off-extra-notifications') |
| 20 | + ->setDescription('Set off status for older notifications more than max argument') |
| 21 | + ->addArgument('max', InputOption::VALUE_REQUIRED, 'Max limit for notifications'); |
| 22 | + if (key_exists(MultiTrait::class, class_uses(Conf::class, false))) { |
| 23 | + $this->addArgument('schema', InputOption::VALUE_REQUIRED, 'Enter schema name'); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 28 | + { |
| 29 | + $Conf = new Conf(); |
| 30 | + |
| 31 | + if (is_callable([$Conf, 'setHostSchema'])) { |
| 32 | + if ($schema = $input->getArgument('schema')) { |
| 33 | + } |
| 34 | + } |
| 35 | + if (empty($schema)) { |
| 36 | + $schema = $Conf->getSchema(true); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + $sql = $Conf->getSql(true, false); |
| 41 | + |
| 42 | + $max = $input->getArgument('max') ?? ''; |
| 43 | + if (!ctype_digit($max)){ |
| 44 | + throw new \Exception('Argument max must be integer'); |
| 45 | + } |
| 46 | + $max = (int)$max; |
| 47 | + if ($max < 1){ |
| 48 | + throw new \Exception('Argument max must be > 0'); |
| 49 | + } |
| 50 | + |
| 51 | + $sql->exec('WITH ranked_entries AS ( |
| 52 | + SELECT |
| 53 | + id, |
| 54 | + ROW_NUMBER() OVER (PARTITION BY user_id->>\'v\' ORDER BY active_dt_from->>\'v\' DESC) AS rnk |
| 55 | + FROM |
| 56 | + notifications |
| 57 | + WHERE active->>\'v\' = \'true\' |
| 58 | +) |
| 59 | +UPDATE notifications set active = \'{"v":false}\' |
| 60 | +WHERE id IN ( |
| 61 | + SELECT id FROM ranked_entries WHERE rnk > '.$max.' |
| 62 | +)'); |
| 63 | + |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments