Skip to content

Commit

Permalink
add cli command for zeroing bonuses
Browse files Browse the repository at this point in the history
Signed-off-by: B24io <[email protected]>
  • Loading branch information
b24io-sdk committed Jan 12, 2025
1 parent afc779f commit 4cd689a
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default:
@egrep -e '^\S+' ./Makefile | grep -v default | sed -r 's/://' | sed -r 's/^/ - /'

lint-phpstan:
vendor/bin/phpstan analyse
vendor/bin/phpstan --memory-limit=1G analyse -v
lint-phpinsights:
vendor/bin/phpinsights analyse ./src
lint-rector:
Expand Down
22 changes: 15 additions & 7 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ $log->pushHandler(new StreamHandler($_ENV['LOYALTY_SDK_CLI_LOGS_FILE'], (int)$_E
$log->pushProcessor(new MemoryUsageProcessor(true, true));

$application = new Application();
$application->add(new Commands\Transactions\LoadTransactionsFromFile(
$application->add(
new Commands\Transactions\LoadTransactionsFromFile(
new Infrastructure\Filesystem\TransactionsReader($log),
new DecimalMoneyFormatter(new ISOCurrencies()),
$log));
$application->add(new Commands\Cards\ExportCards(
new CardItemFormatter(),
new CardLevelItemFormatter(),
new ContactItemFormatter(),
$log));
$log
)
);
$application->add(
new Commands\Cards\ExportCards(
new CardItemFormatter(),
new CardLevelItemFormatter(),
new ContactItemFormatter(),
$log
)
);
$application->add(new Commands\Transactions\BulkTransactions($log));
$application->add(new Commands\Transactions\BurnBonuses($log));
$application->add(new Commands\Transactions\ExportTransactions(new TransactionItemFormatter(), $log));
$application->add(new Commands\Transactions\ZeroingBonuses($log));

$application->run($input);
185 changes: 185 additions & 0 deletions src/Commands/Transactions/ZeroingBonuses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

declare(strict_types=1);

namespace B24io\Loyalty\SDK\Commands\Transactions;

use B24io\Loyalty\SDK\Common\Reason;
use B24io\Loyalty\SDK\Common\Requests\ItemsOrder;
use B24io\Loyalty\SDK\Common\Requests\OrderDirection;
use B24io\Loyalty\SDK\Services\ServiceBuilderFactory;
use Money\Currency;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

#[AsCommand(
name: 'transactions:zeroing-bonuses',
description: 'Reset bonuses on active cards')]
class ZeroingBonuses extends Command
{
public function __construct(
private readonly LoggerInterface $logger
) {
parent::__construct();
}

protected function configure(): void
{
$this->addOption(
'api-endpoint-url',
null,
InputOption::VALUE_REQUIRED,
'API endpoint URL',
);
$this->addOption(
'api-client-id',
null,
InputOption::VALUE_REQUIRED,
'API client id from application settings'
);
$this->addOption(
'api-admin-key',
null,
InputOption::VALUE_REQUIRED,
'API admin key from application settings'
);
$this->addOption(
'currency',
null,
InputOption::VALUE_REQUIRED,
'ISO currency code'
);
$this->addOption(
'reason-code',
null,
InputOption::VALUE_REQUIRED,
'reason code'
);
$this->addOption(
'reason-comment',
null,
InputOption::VALUE_REQUIRED,
'reason comment'
);
$this->addOption(
'dryrun',
null,
InputOption::VALUE_NONE,
'validate input and show information',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln([
'Apply transaction to all active cards',
'============',
'',
]);
$io = new SymfonyStyle($input, $output);
$apiEndpointUrl = $input->getOption('api-endpoint-url');
if ($apiEndpointUrl === null) {
$io->error('you must set «api-endpoint-url» option');

return Command::INVALID;
}

$apiClientId = $input->getOption('api-client-id');
if ($apiClientId === null) {
$io->error('you must set «api-client-id» option');

return Command::INVALID;
}

$apiAdminKey = $input->getOption('api-admin-key');
if ($apiAdminKey === null) {
$io->error('you must set «api-admin-key» option');

return Command::INVALID;
}

$currency = $input->getOption('currency');
if ($currency === null) {
$io->error('you must set «currency» option, use ISO-currency codes');

return Command::INVALID;
}
$currency = new Currency($currency);

$reasonCode = $input->getOption('reason-code');
if ($reasonCode === null) {
$io->error('you must set «reason-code» option');

return Command::INVALID;
}

$reasonComment = $input->getOption('reason-comment');
if ($reasonComment === null) {
$io->error('you must set «reason-comment» option');

return Command::INVALID;
}
$isDryrun = $input->getOption('dryrun');


// add result trx log

$admSb = ServiceBuilderFactory::createAdminRoleServiceBuilder(
$apiEndpointUrl,
$apiClientId,
$apiAdminKey,
$this->logger
);
$cardsTotal = $admSb->cardsScope()->cards()->count();
$io->info([
'',
sprintf('cards affected: %s', $cardsTotal),
sprintf('reason code: %s', $reasonCode),
sprintf('reason comment: %s', $reasonComment),
]);

$progressBar = new ProgressBar($output, $cardsTotal);
foreach ($admSb->cardsScope()->fetcher()->list(new ItemsOrder('created', OrderDirection::desc)) as $card) {
try {
if ($card->balance->isZero()) {
$progressBar->advance();
continue;
}

$trxResult = $admSb->transactionsScope()->transactions()->processPaymentTransactionByCardNumber(
$card->number,
$card->balance,
new Reason(
'loyalty-php-sdk',
$reasonCode,
$reasonComment
)
);
$this->logger->info(
sprintf('transaction for card %s processed', $card->number),
[
'trxId' => $trxResult->getTransaction()->id->toRfc4122()
]
);
} catch (Throwable $exception) {
$io->error([
'',
$exception->getMessage(),
$exception->getTraceAsString()
]);
}

$progressBar->advance();
}
$progressBar->finish();

return Command::SUCCESS;
}
}

0 comments on commit 4cd689a

Please sign in to comment.