Skip to content

Commit

Permalink
NotificationTemplateParamsTrait - Refactor + Added supplier bank acco…
Browse files Browse the repository at this point in the history
…unt details

- Added supplier params to notification mailing. 

remp/novydenik#1269
  • Loading branch information
burithetech committed Sep 2, 2024
1 parent 99801b5 commit 5d40936
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/Commands/TestUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \InvalidArgumentException('Missing --user_id option');
}
$this->hermesEmitter->emit(new HermesMessage(TestUserTriggerHandler::EVENT_TYPE, [
'user_id' => $userId
'user_id' => (int) $userId,
]));

$output->writeln("<info>Event 'scenarios-test-user' with UserID={$userId} fired</info>");
Expand Down
213 changes: 173 additions & 40 deletions src/Events/NotificationTemplateParamsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,157 @@

namespace Crm\ScenariosModule\Events;

use Crm\ApplicationModule\Models\Config\ApplicationConfig;
use Crm\PaymentsModule\Models\RecurrentPaymentsResolver;
use Crm\PaymentsModule\Repositories\PaymentsRepository;
use Crm\PaymentsModule\Repositories\RecurrentPaymentsRepository;
use Crm\SubscriptionsModule\Repositories\SubscriptionsRepository;
use Crm\UsersModule\Repositories\AddressesRepository;
use Crm\UsersModule\Repositories\UsersRepository;
use Nette\Database\Table\ActiveRow;

/**
* @property SubscriptionsRepository $subscriptionsRepository
* @property PaymentsRepository $paymentsRepository
* @property RecurrentPaymentsRepository $recurrentPaymentsRepository
* @property UsersRepository $usersRepository
* @property AddressesRepository $addressesRepository
* @property RecurrentPaymentsResolver $recurrentPaymentsResolver
* @property ApplicationConfig $applicationConfig
*/
trait NotificationTemplateParamsTrait
{
public function getNotificationTemplateParams(object $scenarioJobParams): array
{
$user = isset($scenarioJobParams->user_id) ? $this->usersRepository->find($scenarioJobParams->user_id) : null;
$password = $scenarioJobParams->password ?? null;
$subscription = $this->getSubscription($scenarioJobParams);
$payment = $this->getPayment($scenarioJobParams, $subscription);
$recurrentPayment = $this->getRecurrentPayment($scenarioJobParams, $payment);

return [
...$this->getUserParams($scenarioJobParams),
...$this->getPasswordParams($scenarioJobParams),
...$this->getRenewalPaymentParams($scenarioJobParams),
...$this->getRecurrentPaymentParams($recurrentPayment),
...$this->getRecurrentParentPaymentParams($recurrentPayment),
...$this->getAddressParams($scenarioJobParams),
...$this->getSubscriptionParams($subscription),
...$this->getSubscriptionTypeParams($subscription, $payment, $recurrentPayment),
...$this->getPaymentParams($payment),
...$this->getSupplierParams(),
];
}

private function getSubscription(object $scenarioJobParams): ?ActiveRow
{
$subscription = isset($scenarioJobParams->subscription_id) ? $this->subscriptionsRepository->find($scenarioJobParams->subscription_id) : null;
$payment = isset($scenarioJobParams->payment_id) ? $this->paymentsRepository->find($scenarioJobParams->payment_id) : null;
// this is here to allow `Crm\PaymentsModule\Events\CreateNewPaymentEventHandler` add `renewal_payment` job parameter remp/novydenik#1147
$renewalPayment = isset($scenarioJobParams->renewal_payment_id) ? $this->paymentsRepository->find($scenarioJobParams->renewal_payment_id) : null;
$address = isset($scenarioJobParams->address_id) ? $this->addressesRepository->find($scenarioJobParams->address_id) : null;

if ($payment && !$subscription && isset($payment->subscription)) {
$subscription = $payment->subscription;
if (!$subscription && $payment && isset($payment->subscription)) {
return $payment->subscription;
}

return $subscription;
}

private function getPayment(object $scenarioJobParams, ?ActiveRow $subscription): ?ActiveRow
{
$payment = isset($scenarioJobParams->payment_id) ? $this->paymentsRepository->find($scenarioJobParams->payment_id) : null;
if (!$payment && $subscription) {
$payment = $this->paymentsRepository->subscriptionPayment($subscription);
return $this->paymentsRepository->subscriptionPayment($subscription);
}

$recurrentPayment = null;
return $payment;
}

private function getRecurrentPayment(object $scenarioJobParams, ?ActiveRow $payment): ?ActiveRow
{
if (isset($scenarioJobParams->recurrent_payment_id)) {
$recurrentPayment = $this->recurrentPaymentsRepository->find($scenarioJobParams->recurrent_payment_id);
} elseif ($payment !== null) {
$recurrentPayment = $this->recurrentPaymentsRepository->recurrent($payment);
return $this->recurrentPaymentsRepository->find($scenarioJobParams->recurrent_payment_id);
}

$recurrentParentPayment = $recurrentPayment?->parent_payment;
if ($payment !== null) {
return $this->recurrentPaymentsRepository->recurrent($payment);
}

return null;
}

private function getUserParams(object $scenarioJobParams): array
{
$user = $scenarioJobParams->user_id ? $this->usersRepository->find($scenarioJobParams->user_id) : null;
if ($user === null) {
return [];
}

return [
'user' => $user->toArray(),
'email' => $user->email,
];
}

private function getPasswordParams(object $scenarioJobParams): array
{
$password = $scenarioJobParams->password ?? null;
if ($password === null) {
return [];
}

return [
'password' => $password,
];
}

private function getRenewalPaymentParams(object $scenarioJobParams): array
{
// this is here to allow `Crm\PaymentsModule\Events\CreateNewPaymentEventHandler` add `renewal_payment` job parameter remp/novydenik#1147
$renewalPayment = isset($scenarioJobParams->renewal_payment_id) ? $this->paymentsRepository->find($scenarioJobParams->renewal_payment_id) : null;
if ($renewalPayment === null) {
return [];
}

return [
'renewal_payment' => $renewalPayment->toArray(),
];
}

private function getAddressParams(object $scenarioJobParams): array
{
$address = isset($scenarioJobParams->address_id) ? $this->addressesRepository->find($scenarioJobParams->address_id) : null;
if ($address === null) {
return [];
}

return [
'address' => $address->toArray(),
];
}

private function getSubscriptionParams(?ActiveRow $subscription): array
{
if ($subscription === null) {
return [];
}

return [
'subscription' => $subscription->toArray(),
];
}

private function getPaymentParams(?ActiveRow $payment): array
{
if ($payment === null) {
return [];
}

return [
'payment' => $payment->toArray(),
];
}

private function getSubscriptionTypeParams(?ActiveRow $subscription, ?ActiveRow $payment, ?ActiveRow $recurrentPayment): array
{
$subscriptionType = null;

if ($subscription !== null) {
$subscriptionType = $subscription->subscription_type;
} elseif ($payment !== null) {
Expand All @@ -39,37 +161,48 @@ public function getNotificationTemplateParams(object $scenarioJobParams): array
$subscriptionType = $this->recurrentPaymentsResolver->resolveSubscriptionType($recurrentPayment);
}

$templateParams = [];
if ($user) {
$templateParams['user'] = $user->toArray();
$templateParams['email'] = $user->email;
if ($subscriptionType === null) {
return [];
}

if ($password) {
$templateParams['password'] = $password;
}
if ($subscription) {
$templateParams['subscription'] = $subscription->toArray();
}
if ($subscriptionType) {
$templateParams['subscription_type'] = $subscriptionType->toArray();
}
if ($payment) {
$templateParams['payment'] = $payment->toArray();
}
if ($renewalPayment) {
$templateParams['renewal_payment'] = $renewalPayment->toArray();
}
if ($recurrentPayment) {
$templateParams['recurrent_payment'] = $recurrentPayment->toArray();
}
if ($recurrentParentPayment) {
$templateParams['recurrent_parent_payment'] = $recurrentParentPayment->toArray();
return [
'subscription_type' => $subscriptionType->toArray(),
];
}

private function getRecurrentPaymentParams(?ActiveRow $recurrentPayment): array
{
if ($recurrentPayment === null) {
return [];
}
if ($address) {
$templateParams['address'] = $address->toArray();

return [
'recurrent_payment' => $recurrentPayment->toArray(),
];
}

private function getRecurrentParentPaymentParams(?ActiveRow $recurrentPayment): array
{
$recurrentParentPayment = $recurrentPayment?->parent_payment;
if ($recurrentParentPayment === null) {
return [];
}

return $templateParams;
return [
'recurrent_parent_payment' => $recurrentParentPayment->toArray(),
];
}

private function getSupplierParams(): array
{
return [
'supplier' => [
'bank_account' => [
'number' => $this->applicationConfig->get('supplier_bank_account_number'),
'iban' => $this->applicationConfig->get('supplier_iban'),
'swift' => $this->applicationConfig->get('supplier_swift'),
],
],
];
}
}
18 changes: 10 additions & 8 deletions src/Events/SendEmailEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Crm\ScenariosModule\Events;

use Crm\ApplicationModule\Hermes\HermesMessage;
use Crm\ApplicationModule\Models\Config\ApplicationConfig;
use Crm\PaymentsModule\Models\RecurrentPaymentsResolver;
use Crm\PaymentsModule\Repositories\PaymentsRepository;
use Crm\PaymentsModule\Repositories\RecurrentPaymentsRepository;
Expand All @@ -25,14 +26,15 @@ class SendEmailEventHandler extends ScenariosJobsHandler

public function __construct(
JobsRepository $jobsRepository,
private Emitter $emitter,
private UsersRepository $usersRepository,
private SubscriptionsRepository $subscriptionsRepository,
private RecurrentPaymentsRepository $recurrentPaymentsRepository,
private PaymentsRepository $paymentsRepository,
private RecurrentPaymentsResolver $recurrentPaymentsResolver,
private ReachChecker $reachChecker,
private AddressesRepository $addressesRepository,
private readonly Emitter $emitter,
private readonly UsersRepository $usersRepository,
private readonly SubscriptionsRepository $subscriptionsRepository,
private readonly RecurrentPaymentsRepository $recurrentPaymentsRepository,
private readonly PaymentsRepository $paymentsRepository,
private readonly RecurrentPaymentsResolver $recurrentPaymentsResolver,
private readonly ReachChecker $reachChecker,
private readonly AddressesRepository $addressesRepository,
private readonly ApplicationConfig $applicationConfig,
) {
parent::__construct($jobsRepository);
}
Expand Down

0 comments on commit 5d40936

Please sign in to comment.