Skip to content

Commit

Permalink
Complete onboarding goals when user subscribes to mail
Browse files Browse the repository at this point in the history
RempMailerModule

- Added parameter `$subscribed` to `UserMailSubscriptionsChanged` event
  to pass subscribed/unsubscribed change of user's mail subscription.

DenniknModule:

- Seeded onboarding goals for user subscription to newsfilter and
  economic newsfilter.
- Handler `UserMailSubscriptionsChangedHandler` completes user's
  onboarding goals related to mail subscriptions.

remp/crm#1732
  • Loading branch information
markoph committed Mar 1, 2021
1 parent 7971564 commit 9e3f57f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
23 changes: 22 additions & 1 deletion src/Events/UserMailSubscriptionsChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@

class UserMailSubscriptionsChanged extends AbstractEvent
{
public const SUBSCRIBED = 'subscribed';
public const UNSUBSCRIBED = 'unsubscribed';

private $userId;

private $mailTypeId;

public function __construct($userId, $mailTypeId)
private $subscribed;

/**
* @param int $userId
* @param int $mailTypeId
* @param string $subscribed Change of user's mail subscription. Allowed values UserMailSubscriptionsChanged::SUBSCRIBED / UserMailSubscriptionsChanged::UNSUBSCRIBED.
* @throws \Exception
*/
public function __construct(int $userId, int $mailTypeId, string $subscribed)
{
$this->userId = $userId;
$this->mailTypeId = $mailTypeId;

if (!in_array($subscribed, [self::SUBSCRIBED, self::UNSUBSCRIBED])) {
throw new \Exception("User can subscribe or unsubscribe. [{$subscribed}] received.");
}
$this->subscribed = $subscribed;
}

public function getUserId()
Expand All @@ -25,4 +41,9 @@ public function getMailTypeId()
{
return $this->mailTypeId;
}

public function getSubscribed()
{
return $this->subscribed;
}
}
37 changes: 26 additions & 11 deletions src/Repositories/MailUserSubscriptionsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ final public function userPreferences(int $userId, ?bool $subscribed = null)
final public function subscribeUser($user, $mailTypeId, $variantId = null, $rtmParams = [])
{
$result = $this->apiClient->subscribeUser($user->id, $user->email, $mailTypeId, $variantId, $rtmParams);
$this->emitter->emit(new UserMailSubscriptionsChanged($user->id, $mailTypeId));
$this->emitter->emit(new UserMailSubscriptionsChanged(
$user->id,
$mailTypeId,
UserMailSubscriptionsChanged::SUBSCRIBED
));
return $result;
}

final public function unSubscribeUser($user, $mailTypeId, $rtmParams = [])
{
$result = $this->apiClient->unSubscribeUser($user->id, $user->email, $mailTypeId, $rtmParams);
$this->emitter->emit(new UserMailSubscriptionsChanged($user->id, $mailTypeId));
$this->emitter->emit(new UserMailSubscriptionsChanged(
$user->id,
$mailTypeId,
UserMailSubscriptionsChanged::UNSUBSCRIBED
));
return $result;
}

Expand All @@ -68,9 +76,11 @@ final public function subscribeUserAll($user)

/** @var MailSubscribeRequest $subscribeRequest */
foreach ($subscribeRequests as $subscribeRequest) {
$this->emitter->emit(
new UserMailSubscriptionsChanged($subscribeRequest->getUserId(), $subscribeRequest->getMailTypeId())
);
$this->emitter->emit(new UserMailSubscriptionsChanged(
$subscribeRequest->getUserId(),
$subscribeRequest->getMailTypeId(),
UserMailSubscriptionsChanged::SUBSCRIBED
));
}

return $result;
Expand All @@ -93,9 +103,11 @@ final public function unsubscribeUserAll($user)

/** @var MailSubscribeRequest $subscribeRequest */
foreach ($subscribeRequests as $subscribeRequest) {
$this->emitter->emit(
new UserMailSubscriptionsChanged($subscribeRequest->getUserId(), $subscribeRequest->getMailTypeId())
);
$this->emitter->emit(new UserMailSubscriptionsChanged(
$subscribeRequest->getUserId(),
$subscribeRequest->getMailTypeId(),
UserMailSubscriptionsChanged::UNSUBSCRIBED
));
}

return $result;
Expand All @@ -107,9 +119,12 @@ final public function bulkSubscriptionChange(array $subscribeRequests)

/** @var MailSubscribeRequest $subscribeRequest */
foreach ($subscribeRequests as $subscribeRequest) {
$this->emitter->emit(
new UserMailSubscriptionsChanged($subscribeRequest->getUserId(), $subscribeRequest->getMailTypeId())
);
$this->emitter->emit(new UserMailSubscriptionsChanged(
$subscribeRequest->getUserId(),
$subscribeRequest->getMailTypeId(),
$subscribeRequest->getSubscribed() ?
UserMailSubscriptionsChanged::SUBSCRIBED : UserMailSubscriptionsChanged::UNSUBSCRIBED
));
}

return $result;
Expand Down

0 comments on commit 9e3f57f

Please sign in to comment.