feat(crm): implement unsubscribe from newsletter functionality
- Added UnsubscribeFromNewsletter action and input/output classes. - Introduced CrmHashcode value object and its cast. - Updated NewsletterController to use the new unsubscribe action. - Enhanced validation in UnsubscribeNewsletterRequest.
This commit is contained in:
parent
42bdae01cc
commit
2aa4fa8aed
12 changed files with 234 additions and 45 deletions
39
app/Actions/Crm/UnsubscribeFromNewsletter.php
Normal file
39
app/Actions/Crm/UnsubscribeFromNewsletter.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Crm;
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Services\CrmService;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Action<\App\Actions\Crm\UnsubscribeFromNewsletterInput, \App\Actions\Crm\UnsubscribeFromNewsletterOutput>
|
||||
*/
|
||||
final class UnsubscribeFromNewsletter implements Action
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CrmService $crmService
|
||||
) {
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): UnsubscribeFromNewsletterOutput
|
||||
{
|
||||
Assert::isInstanceOf($input, UnsubscribeFromNewsletterInput::class);
|
||||
|
||||
try {
|
||||
$isUnsubscribed = $this->crmService->unsubscribeCustomer(
|
||||
$input->hashcode
|
||||
);
|
||||
|
||||
return $isUnsubscribed ? UnsubscribeFromNewsletterOutput::SUCCESS : UnsubscribeFromNewsletterOutput::FAILURE;
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
|
||||
return UnsubscribeFromNewsletterOutput::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
app/Actions/Crm/UnsubscribeFromNewsletterInput.php
Normal file
16
app/Actions/Crm/UnsubscribeFromNewsletterInput.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Crm;
|
||||
|
||||
use App\Contracts\ActionInput;
|
||||
use Shared\ValueObjects\CrmHashcode;
|
||||
|
||||
final readonly class UnsubscribeFromNewsletterInput implements ActionInput
|
||||
{
|
||||
public function __construct(
|
||||
public CrmHashcode $hashcode
|
||||
) {
|
||||
}
|
||||
}
|
||||
12
app/Actions/Crm/UnsubscribeFromNewsletterOutput.php
Normal file
12
app/Actions/Crm/UnsubscribeFromNewsletterOutput.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Crm;
|
||||
|
||||
enum UnsubscribeFromNewsletterOutput
|
||||
{
|
||||
case SUCCESS;
|
||||
case INVALIDLINK;
|
||||
case FAILURE;
|
||||
}
|
||||
41
app/Casts/CrmHashcodeCast.php
Normal file
41
app/Casts/CrmHashcodeCast.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Casts;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\ValueObjects\CrmHashcode;
|
||||
|
||||
/** @implements CastsAttributes<CrmHashcode,CrmHashcode> */
|
||||
class CrmHashcodeCast implements CastsAttributes
|
||||
{
|
||||
/** @param array<string, mixed> $attributes */
|
||||
public function get(Model $model, string $key, $value, array $attributes): CrmHashcode
|
||||
{
|
||||
|
||||
if ($value === null || ! is_string($value)) {
|
||||
throw new \UnexpectedValueException('CrmHashcode must be a string.');
|
||||
}
|
||||
|
||||
$uuid = Uuid::fromString($value);
|
||||
|
||||
return new CrmHashcode($uuid);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $attributes */
|
||||
public function set(Model $model, string $key, $value, array $attributes): string
|
||||
{
|
||||
if ($value === null) {
|
||||
throw new \InvalidArgumentException('CrmHashcode cannot be null.');
|
||||
}
|
||||
|
||||
if (! ($value instanceof CrmHashcode)) {
|
||||
throw new \InvalidArgumentException('The given value is not an instance of CrmHashcode.');
|
||||
}
|
||||
|
||||
return $value->value;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,36 +4,39 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Http\Controllers\Fo;
|
||||
|
||||
use App\Actions\Crm\UnsubscribeFromNewsletter;
|
||||
use App\Actions\Crm\UnsubscribeFromNewsletterInput;
|
||||
use App\Actions\Crm\UnsubscribeFromNewsletterOutput;
|
||||
use App\Contracts\Executor;
|
||||
use App\Http\Requests\Fo\UnsubscribeNewsletterRequest;
|
||||
use App\Services\CrmService;
|
||||
use Illuminate\Contracts\View\View;
|
||||
|
||||
class NewsletterController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CrmService $crmService
|
||||
private readonly Executor $executor,
|
||||
) {
|
||||
}
|
||||
|
||||
public function unsubscribe(UnsubscribeNewsletterRequest $request): View
|
||||
public function unsubscribe(UnsubscribeNewsletterRequest $request, UnsubscribeFromNewsletter $action): View
|
||||
{
|
||||
try {
|
||||
$isUnsubscribed = false;
|
||||
$isUnsubscribed = UnsubscribeFromNewsletterOutput::INVALIDLINK;
|
||||
|
||||
if ($request->getHashcode()) {
|
||||
$isUnsubscribed = $this->crmService->unsubscribeCustomer(
|
||||
$request->getHashcode()
|
||||
);
|
||||
}
|
||||
|
||||
$message = $isUnsubscribed
|
||||
? trans('Your newsletter unsubscription has been taken into account.')
|
||||
: trans('The unsubscription link is invalid.');
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
$message = trans('An error occurred, please try again later.');
|
||||
if ($request->getHashcode()) {
|
||||
$isUnsubscribed = $this->executor->execute(
|
||||
$action,
|
||||
new UnsubscribeFromNewsletterInput(
|
||||
hashcode: $request->getHashcode()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$message = match ($isUnsubscribed) {
|
||||
UnsubscribeFromNewsletterOutput::SUCCESS => trans('Your newsletter unsubscription has been taken into account.'),
|
||||
UnsubscribeFromNewsletterOutput::FAILURE => trans('An error occurred, please try again later.'),
|
||||
UnsubscribeFromNewsletterOutput::INVALIDLINK => trans('The unsubscription link is invalid.'),
|
||||
};
|
||||
|
||||
return view('fo.news.singleoptin-unsubscribe', compact('message'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,13 +4,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Http\Requests\Fo;
|
||||
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\ValueObjects\CrmHashcode;
|
||||
|
||||
class UnsubscribeNewsletterRequest extends FormRequest
|
||||
{
|
||||
private bool $validated = false;
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
|
|
@ -19,20 +18,16 @@ class UnsubscribeNewsletterRequest extends FormRequest
|
|||
];
|
||||
}
|
||||
|
||||
public function getHashcode(): ?string
|
||||
public function getHashcode(): ?CrmHashcode
|
||||
{
|
||||
return $this->validated ? $this->string('hashcode')->value() : null;
|
||||
}
|
||||
$hashcode = null;
|
||||
|
||||
#[\Override]
|
||||
protected function passedValidation()
|
||||
{
|
||||
$this->validated = true;
|
||||
}
|
||||
$this->string('hashcode')->whenIsUuid(
|
||||
function (string $value) use (&$hashcode) {
|
||||
$hashcode = new CrmHashcode(Uuid::fromString($value));
|
||||
}
|
||||
);
|
||||
|
||||
#[\Override]
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$this->validated = false;
|
||||
return $hashcode;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Casts\CrmHashcodeCast;
|
||||
use App\Casts\EmailCast;
|
||||
use App\Traits\Models\HasUuidV7AsPrimaryKey;
|
||||
use Domain\Contracts\CustomerDetails;
|
||||
|
|
@ -18,7 +19,7 @@ use Ramsey\Uuid\Uuid;
|
|||
/**
|
||||
* @property string $id
|
||||
* @property \Domain\Models\ValueObjects\Email $email
|
||||
* @property string $hashcode
|
||||
* @property \Shared\ValueObjects\CrmHashcode $hashcode
|
||||
* @property string|null $remember_token
|
||||
* @property \Carbon\CarbonInterface|null $created_at
|
||||
* @property \Illuminate\Support\Carbon|null $updated_at
|
||||
|
|
@ -43,6 +44,7 @@ class Customer extends Authenticatable implements CustomerDetails, MustVerifyEma
|
|||
{
|
||||
return [
|
||||
'email' => EmailCast::class,
|
||||
'hashcode' => CrmHashcodeCast::class,
|
||||
'last_login_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
|
@ -50,7 +52,7 @@ class Customer extends Authenticatable implements CustomerDetails, MustVerifyEma
|
|||
#[\Override]
|
||||
public function getAuthPassword(): string
|
||||
{
|
||||
return $this->hashcode;
|
||||
return $this->hashcode->value;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|||
use LDC\LDCCRM\Exceptions\CrmException;
|
||||
use LDC\LDCCRM\Exceptions\ValidationException;
|
||||
use LDC\LDCCRM\Models\Customer;
|
||||
use Shared\ValueObjects\CrmHashcode;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class CrmService
|
||||
|
|
@ -169,7 +170,7 @@ class CrmService
|
|||
if (! $crmCustomer instanceof CrmCustomer) {
|
||||
throw $crmCustomer instanceof \Throwable
|
||||
? $crmCustomer
|
||||
: new UnexpectedValueException('CRM customer not found for hashcode: ' . $customer->hashcode);
|
||||
: new UnexpectedValueException('CRM customer not found for hashcode: ' . (string) $customer->hashcode);
|
||||
}
|
||||
|
||||
$updatedData = [
|
||||
|
|
@ -197,7 +198,7 @@ class CrmService
|
|||
}
|
||||
}
|
||||
|
||||
public function unsubscribeCustomer(string $hashcode): bool
|
||||
public function unsubscribeCustomer(CrmHashcode $hashcode): bool
|
||||
{
|
||||
$crmCustomer = $this->getCustomerFromHashCode($hashcode);
|
||||
|
||||
|
|
@ -221,13 +222,13 @@ class CrmService
|
|||
}
|
||||
}
|
||||
|
||||
public function getCustomerFromHashCode(string $hashcode): null | CrmCustomer | CrmServiceException
|
||||
public function getCustomerFromHashCode(CrmHashcode $hashcode): null | CrmCustomer | CrmServiceException
|
||||
{
|
||||
if (array_key_exists($hashcode, $this->customerByHashcodeCache)) {
|
||||
return $this->customerByHashcodeCache[$hashcode];
|
||||
if (array_key_exists($hashcode->value, $this->customerByHashcodeCache)) {
|
||||
return $this->customerByHashcodeCache[$hashcode->value];
|
||||
}
|
||||
|
||||
$cacheKey = $this->getCustomerHashcodeCacheKey($hashcode);
|
||||
$cacheKey = $this->getCustomerHashcodeCacheKey($hashcode->value);
|
||||
|
||||
/** @var null|CrmCustomer|CrmServiceException $result */
|
||||
$result = $this->cacheRepository->remember(
|
||||
|
|
@ -235,7 +236,7 @@ class CrmService
|
|||
now()->addMinutes(15),
|
||||
function () use ($hashcode): null | CrmCustomer | CrmServiceException {
|
||||
try {
|
||||
$customer = $this->crmApi->getCustomer($hashcode);
|
||||
$customer = $this->crmApi->getCustomer($hashcode->value);
|
||||
|
||||
return $customer ?
|
||||
$this->toAppCrmCustomer($customer) :
|
||||
|
|
@ -250,7 +251,7 @@ class CrmService
|
|||
}
|
||||
);
|
||||
|
||||
$this->customerByHashcodeCache[$hashcode] = $result;
|
||||
$this->customerByHashcodeCache[$hashcode->value] = $result;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,9 +16,10 @@ use Domain\Models\ValueObjects\Matricule;
|
|||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\ValueObjects\CrmHashcode;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class CustomerRegistrationService
|
||||
|
|
@ -60,7 +61,7 @@ class CustomerRegistrationService
|
|||
'email' => new Email($data['email']),
|
||||
]);
|
||||
// * Temp hashcode to satisfy constraint
|
||||
$customer->hashcode = Str::uuid()->toString();
|
||||
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
|
||||
$customer->saveOrFail();
|
||||
|
||||
$employee = $this->employeeRepository
|
||||
|
|
@ -105,7 +106,7 @@ class CustomerRegistrationService
|
|||
'CRM did not return a hashcode for the created customer'
|
||||
);
|
||||
}
|
||||
$customer->hashcode = $result->hashcode;
|
||||
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
|
||||
$customer->saveOrFail();
|
||||
|
||||
$customer->sendEmailVerificationNotification();
|
||||
|
|
|
|||
13
shared/Exceptions/InvalidUuidException.php
Normal file
13
shared/Exceptions/InvalidUuidException.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shared\Helpers\Exceptions;
|
||||
|
||||
final class InvalidUuidException extends \InvalidArgumentException
|
||||
{
|
||||
public static function forValue(string $uuid): self
|
||||
{
|
||||
return new self("Invalid UUID: {$uuid}");
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,9 @@ declare(strict_types=1);
|
|||
namespace Shared\Helpers;
|
||||
|
||||
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
use Shared\Helpers\Exceptions\InvalidUuidException;
|
||||
|
||||
final readonly class UuidHelper
|
||||
{
|
||||
|
|
@ -44,4 +46,39 @@ final readonly class UuidHelper
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function validateUuid(UuidInterface $uuid): void
|
||||
{
|
||||
if (! Uuid::isValid($uuid->toString())) {
|
||||
throw InvalidUuidException::forValue($uuid->toString());
|
||||
}
|
||||
}
|
||||
|
||||
public static function toDateTime(UuidInterface $uuid): \DateTimeImmutable
|
||||
{
|
||||
if (! self::isV7($uuid)) {
|
||||
throw new InvalidUuidException("Cannot extract datetime from non-V7 UUID: {$uuid->toString()}");
|
||||
}
|
||||
|
||||
$fields = $uuid->getFields();
|
||||
|
||||
if ($fields instanceof Rfc4122FieldsInterface) {
|
||||
$timeMsHex = $fields->getTimeLow()->toString() . $fields->getTimeMid()->toString();
|
||||
} else {
|
||||
$hex = str_replace('-', '', $uuid->toString());
|
||||
$timeMsHex = substr($hex, 0, 12);
|
||||
}
|
||||
|
||||
$timeMs = (int) hexdec($timeMsHex);
|
||||
$sec = intdiv($timeMs, 1000);
|
||||
$ms = $timeMs % 1000;
|
||||
|
||||
$dateTime = \DateTimeImmutable::createFromFormat('U.u', sprintf('%d.%06d', $sec, $ms * 1000));
|
||||
|
||||
if ($dateTime === false) {
|
||||
throw new InvalidUuidException("Failed to extract datetime from UUID: {$uuid->toString()}");
|
||||
}
|
||||
|
||||
return $dateTime->setTimezone(new \DateTimeZone('UTC'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
29
shared/ValueObjects/CrmHashcode.php
Normal file
29
shared/ValueObjects/CrmHashcode.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shared\ValueObjects;
|
||||
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
use Shared\Helpers\UuidHelper;
|
||||
|
||||
final readonly class CrmHashcode implements \Stringable
|
||||
{
|
||||
public string $value;
|
||||
|
||||
public function __construct(
|
||||
UuidInterface $value,
|
||||
) {
|
||||
UuidHelper::validateUuid($value);
|
||||
|
||||
if (! UuidHelper::isV4($value)) {
|
||||
throw new \DomainException('CrmHashcode is not a valid uuidv4');
|
||||
}
|
||||
$this->value = $value->toString();
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue