-
Notifications
You must be signed in to change notification settings - Fork 3
Add contact persons use case #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mesilov
merged 34 commits into
mesilov:dev
from
KarlsonComplete:add-contact-persons-use-case
Mar 4, 2026
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
3741842
.
KarlsonComplete 961a68f
.
KarlsonComplete c0d5d9e
.
KarlsonComplete cdb31d4
.
KarlsonComplete 09dafb9
.
KarlsonComplete 6c330fc
.
KarlsonComplete 4b91351
Merge branch 'dev' into add-contact-persons-use-case
camaxtly d083cf1
.
KarlsonComplete 5d67d98
Merge remote-tracking branch 'origin/add-contact-persons-use-case' in…
KarlsonComplete fe89ec4
.
KarlsonComplete 289aba9
.
KarlsonComplete 601fbd4
.
KarlsonComplete c4f727a
.
KarlsonComplete 896cdf9
.
KarlsonComplete 8e43b54
Remove Install/Unlink PartnerContactPerson use cases and related tests
KarlsonComplete bf37bf5
Add email validation and verification timestamp handling in ContactPe…
KarlsonComplete 8a548d6
Validate UUID instance for unlink methods, enhance phone verification…
KarlsonComplete 42311c1
Remove redundant phone number validation, streamline `ContactPerson` …
KarlsonComplete 7b2551e
Enhance phone and email verification handling, streamline validation,…
KarlsonComplete 4b585e2
Refactor namespaces for consistency, enhance mobile number validation…
KarlsonComplete a5d03de
Update email and mobile phone change methods to reset verification st…
KarlsonComplete 81720e9
Update `UnlinkContactPerson` use case to include application installa…
KarlsonComplete 5864fc1
Refactor exception handling and logging across ContactPerson use case…
KarlsonComplete 688ce0f
Replace UUID instance checks with null checks in `unlinkContactPerson…
KarlsonComplete 76c7c2e
Refactor handling of email and phone verification, streamline null ch…
KarlsonComplete 1c91009
Refactor tests and handlers for improved consistency, streamline null…
KarlsonComplete 67238ed
Refactor and enhance test coverage for email and phone verification u…
KarlsonComplete d8fe02b
Add comprehensive unit tests for various `Command` classes, improve e…
KarlsonComplete 1498195
Remove outdated unit tests for unused `UnlinkContactPerson` and `Mark…
KarlsonComplete e22af57
Refactor tests for improved consistency: rename variables, enhance as…
KarlsonComplete de9ed5c
Improve email validation and trimming logic in `MarkEmailAsVerified` …
KarlsonComplete 9f69eb7
Normalize email handling in `ContactPerson` entity and related use ca…
KarlsonComplete 025434a
Update `bitrix24/b24phpsdk` dependency to stable version `^3` in `com…
KarlsonComplete 4324cf9
Merge branch 'dev' into add-contact-persons-use-case
KarlsonComplete File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/ApplicationInstallations/UseCase/InstallContactPerson/Command.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bitrix24\Lib\ApplicationInstallations\UseCase\InstallContactPerson; | ||
|
|
||
| use Bitrix24\SDK\Application\Contracts\ContactPersons\Entity\FullName; | ||
| use Bitrix24\SDK\Application\Contracts\ContactPersons\Entity\UserAgentInfo; | ||
| use libphonenumber\PhoneNumber; | ||
| use Symfony\Component\Uid\Uuid; | ||
|
|
||
| readonly class Command | ||
| { | ||
| public function __construct( | ||
| public Uuid $applicationInstallationId, | ||
| public FullName $fullName, | ||
| public int $bitrix24UserId, | ||
| public UserAgentInfo $userAgentInfo, | ||
| public ?string $email, | ||
| public ?PhoneNumber $mobilePhoneNumber, | ||
| public ?string $comment, | ||
| public ?string $externalId, | ||
| public ?Uuid $bitrix24PartnerId, | ||
| ) { | ||
| $this->validate(); | ||
| } | ||
|
|
||
| private function validate(): void | ||
| { | ||
| if (null !== $this->email && !filter_var($this->email, FILTER_VALIDATE_EMAIL)) { | ||
| throw new \InvalidArgumentException('Invalid email format.'); | ||
| } | ||
|
|
||
| if (null !== $this->externalId && '' === trim($this->externalId)) { | ||
| throw new \InvalidArgumentException('External ID cannot be empty if provided.'); | ||
| } | ||
|
|
||
| if ($this->bitrix24UserId <= 0) { | ||
| throw new \InvalidArgumentException('Bitrix24 User ID must be a positive integer.'); | ||
| } | ||
| } | ||
| } |
122 changes: 122 additions & 0 deletions
122
src/ApplicationInstallations/UseCase/InstallContactPerson/Handler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bitrix24\Lib\ApplicationInstallations\UseCase\InstallContactPerson; | ||
|
|
||
| use Bitrix24\Lib\ContactPersons\Entity\ContactPerson; | ||
| use Bitrix24\Lib\Services\Flusher; | ||
| use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Entity\ApplicationInstallationInterface; | ||
| use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Exceptions\ApplicationInstallationNotFoundException; | ||
| use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Repository\ApplicationInstallationRepositoryInterface; | ||
| use Bitrix24\SDK\Application\Contracts\ContactPersons\Entity\ContactPersonStatus; | ||
| use Bitrix24\SDK\Application\Contracts\ContactPersons\Repository\ContactPersonRepositoryInterface; | ||
| use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; | ||
| use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; | ||
| use libphonenumber\PhoneNumber; | ||
| use libphonenumber\PhoneNumberType; | ||
| use libphonenumber\PhoneNumberUtil; | ||
| use Psr\Log\LoggerInterface; | ||
| use Symfony\Component\Uid\Uuid; | ||
|
|
||
| readonly class Handler | ||
| { | ||
| public function __construct( | ||
| private ApplicationInstallationRepositoryInterface $applicationInstallationRepository, | ||
| private ContactPersonRepositoryInterface $contactPersonRepository, | ||
| private PhoneNumberUtil $phoneNumberUtil, | ||
| private Flusher $flusher, | ||
| private LoggerInterface $logger | ||
| ) {} | ||
|
|
||
| public function handle(Command $command): void | ||
| { | ||
| $this->logger->info('ContactPerson.InstallContactPerson.start', [ | ||
| 'applicationInstallationId' => $command->applicationInstallationId, | ||
| 'bitrix24UserId' => $command->bitrix24UserId, | ||
| 'bitrix24PartnerId' => $command->bitrix24PartnerId?->toRfc4122() ?? '', | ||
| ]); | ||
|
|
||
| $createdContactPersonId = ''; | ||
|
|
||
| try { | ||
| if (null !== $command->mobilePhoneNumber) { | ||
| try { | ||
| $this->guardMobilePhoneNumber($command->mobilePhoneNumber); | ||
| } catch (InvalidArgumentException) { | ||
| // Ошибка уже залогирована внутри гарда. | ||
| // Прерываем создание контакта, но не останавливаем установку приложения. | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| /** @var null|AggregateRootEventsEmitterInterface|ApplicationInstallationInterface $applicationInstallation */ | ||
| $applicationInstallation = $this->applicationInstallationRepository->getById($command->applicationInstallationId); | ||
|
|
||
| $uuidV7 = Uuid::v7(); | ||
|
|
||
| $contactPerson = new ContactPerson( | ||
| $uuidV7, | ||
| ContactPersonStatus::active, | ||
| $command->fullName, | ||
| $command->email, | ||
| null, | ||
| $command->mobilePhoneNumber, | ||
| null, | ||
| $command->comment, | ||
| $command->externalId, | ||
| $command->bitrix24UserId, | ||
| $command->bitrix24PartnerId, | ||
| $command->userAgentInfo, | ||
| true | ||
| ); | ||
|
|
||
| $this->contactPersonRepository->save($contactPerson); | ||
|
|
||
| if ($contactPerson->isPartner()) { | ||
| $applicationInstallation->linkBitrix24PartnerContactPerson($uuidV7); | ||
| } else { | ||
| $applicationInstallation->linkContactPerson($uuidV7); | ||
| } | ||
|
|
||
| $this->applicationInstallationRepository->save($applicationInstallation); | ||
|
|
||
| $this->flusher->flush($contactPerson, $applicationInstallation); | ||
|
|
||
| $createdContactPersonId = $uuidV7->toRfc4122(); | ||
| } catch (ApplicationInstallationNotFoundException $applicationInstallationNotFoundException) { | ||
| $this->logger->warning('ContactPerson.InstallContactPerson.applicationInstallationNotFound', [ | ||
| 'applicationInstallationId' => $command->applicationInstallationId, | ||
| 'message' => $applicationInstallationNotFoundException->getMessage(), | ||
| ]); | ||
|
|
||
| throw $applicationInstallationNotFoundException; | ||
| } finally { | ||
| $this->logger->info('ContactPerson.InstallContactPerson.finish', [ | ||
| 'applicationInstallationId' => $command->applicationInstallationId, | ||
| 'bitrix24UserId' => $command->bitrix24UserId, | ||
| 'bitrix24PartnerId' => $command->bitrix24PartnerId?->toRfc4122() ?? '', | ||
| 'contact_person_id' => $createdContactPersonId, | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| private function guardMobilePhoneNumber(PhoneNumber $mobilePhoneNumber): void | ||
| { | ||
| if (!$this->phoneNumberUtil->isValidNumber($mobilePhoneNumber)) { | ||
| $this->logger->warning('ContactPerson.InstallContactPerson.InvalidMobilePhoneNumber', [ | ||
| 'mobilePhoneNumber' => (string) $mobilePhoneNumber, | ||
| ]); | ||
|
|
||
| throw new InvalidArgumentException('Invalid mobile phone number.'); | ||
| } | ||
|
|
||
| if (PhoneNumberType::MOBILE !== $this->phoneNumberUtil->getNumberType($mobilePhoneNumber)) { | ||
| $this->logger->warning('ContactPerson.InstallContactPerson.MobilePhoneNumberMustBeMobile', [ | ||
| 'mobilePhoneNumber' => (string) $mobilePhoneNumber, | ||
| ]); | ||
|
|
||
| throw new InvalidArgumentException('Phone number must be mobile.'); | ||
| } | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
src/ApplicationInstallations/UseCase/UnlinkContactPerson/Command.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bitrix24\Lib\ApplicationInstallations\UseCase\UnlinkContactPerson; | ||
|
|
||
| use Symfony\Component\Uid\Uuid; | ||
|
|
||
| readonly class Command | ||
| { | ||
| public function __construct( | ||
KarlsonComplete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public Uuid $contactPersonId, | ||
KarlsonComplete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public Uuid $applicationInstallationId, | ||
| public ?string $comment = null, | ||
| ) { | ||
| $this->validate(); | ||
| } | ||
|
|
||
| private function validate(): void | ||
| { | ||
| // no-op for now, but keep a place for future checks | ||
| } | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
src/ApplicationInstallations/UseCase/UnlinkContactPerson/Handler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bitrix24\Lib\ApplicationInstallations\UseCase\UnlinkContactPerson; | ||
|
|
||
| use Bitrix24\Lib\Services\Flusher; | ||
| use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Entity\ApplicationInstallationInterface; | ||
| use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Exceptions\ApplicationInstallationNotFoundException; | ||
| use Bitrix24\SDK\Application\Contracts\ApplicationInstallations\Repository\ApplicationInstallationRepositoryInterface; | ||
| use Bitrix24\SDK\Application\Contracts\ContactPersons\Entity\ContactPersonInterface; | ||
| use Bitrix24\SDK\Application\Contracts\ContactPersons\Exceptions\ContactPersonNotFoundException; | ||
| use Bitrix24\SDK\Application\Contracts\ContactPersons\Repository\ContactPersonRepositoryInterface; | ||
| use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| readonly class Handler | ||
KarlsonComplete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| public function __construct( | ||
| private ApplicationInstallationRepositoryInterface $applicationInstallationRepository, | ||
| private ContactPersonRepositoryInterface $contactPersonRepository, | ||
| private Flusher $flusher, | ||
| private LoggerInterface $logger | ||
| ) {} | ||
|
|
||
| public function handle(Command $command): void | ||
| { | ||
| $this->logger->info('ContactPerson.UnlinkContactPerson.start', [ | ||
| 'contactPersonId' => $command->contactPersonId, | ||
| 'applicationInstallationId' => $command->applicationInstallationId, | ||
| ]); | ||
|
|
||
| try { | ||
| /** @var AggregateRootEventsEmitterInterface|ContactPersonInterface $contactPerson */ | ||
| $contactPerson = $this->contactPersonRepository->getById($command->contactPersonId); | ||
|
|
||
| /** @var AggregateRootEventsEmitterInterface|ApplicationInstallationInterface $applicationInstallation */ | ||
| $applicationInstallation = $this->applicationInstallationRepository->getById($command->applicationInstallationId); | ||
|
|
||
| $entitiesToFlush = []; | ||
|
|
||
| if ($contactPerson->isPartner()) { | ||
KarlsonComplete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $applicationInstallation->unlinkBitrix24PartnerContactPerson(); | ||
| } else { | ||
| $applicationInstallation->unlinkContactPerson(); | ||
| } | ||
|
|
||
| $this->applicationInstallationRepository->save($applicationInstallation); | ||
| $entitiesToFlush[] = $applicationInstallation; | ||
|
|
||
| $contactPerson->markAsDeleted($command->comment); | ||
| $this->contactPersonRepository->save($contactPerson); | ||
| $entitiesToFlush[] = $contactPerson; | ||
|
|
||
| $this->flusher->flush(...$entitiesToFlush); | ||
| } catch (ApplicationInstallationNotFoundException|ContactPersonNotFoundException $e) { | ||
| $this->logger->warning('ContactPerson.UnlinkContactPerson.notFound', [ | ||
| 'message' => $e->getMessage(), | ||
| ]); | ||
|
|
||
| throw $e; | ||
| } finally { | ||
| $this->logger->info('ContactPerson.UnlinkContactPerson.finish', [ | ||
| 'contactPersonId' => $command->contactPersonId, | ||
| 'applicationInstallationId' => $command->applicationInstallationId, | ||
| ]); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.