-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: mesilov <mesilov.maxim@gmail.com>
- Loading branch information
Showing
7 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bitrix24\SDK\Services\CRM\Duplicates\Result; | ||
|
||
use Bitrix24\SDK\Core\Exceptions\BaseException; | ||
use Bitrix24\SDK\Core\Result\AbstractResult; | ||
|
||
class DuplicateResult extends AbstractResult | ||
{ | ||
public function hasDuplicateContacts(): bool | ||
{ | ||
if (!array_key_exists('CONTACT', $this->getCoreResponse()->getResponseData()->getResult())) { | ||
return false; | ||
} | ||
|
||
if (count($this->getCoreResponse()->getResponseData()->getResult()['CONTACT']) > 1) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function hasOneContact(): bool | ||
{ | ||
if (!array_key_exists('CONTACT', $this->getCoreResponse()->getResponseData()->getResult())) { | ||
return false; | ||
} | ||
|
||
if (count($this->getCoreResponse()->getResponseData()->getResult()['CONTACT']) === 1) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return array<int> | ||
* @throws BaseException | ||
*/ | ||
public function getContactsId(): array | ||
{ | ||
if (!array_key_exists('CONTACT', $this->getCoreResponse()->getResponseData()->getResult())) { | ||
return []; | ||
} | ||
|
||
return $this->getCoreResponse()->getResponseData()->getResult()['CONTACT']; | ||
} | ||
} |
This file contains 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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bitrix24\SDK\Services\CRM\Duplicates\Service; | ||
|
||
use Bitrix24\SDK\Core\Exceptions\BaseException; | ||
use Bitrix24\SDK\Core\Exceptions\TransportException; | ||
use Bitrix24\SDK\Services\AbstractService; | ||
use Bitrix24\SDK\Services\CRM\Duplicates\Result\DuplicateResult; | ||
|
||
class Duplicate extends AbstractService | ||
{ | ||
/** | ||
* @param array<string> $phones | ||
* @param EntityType|null $entityType | ||
* @return DuplicateResult | ||
* @throws BaseException | ||
* @throws TransportException | ||
*/ | ||
public function findByPhone(array $phones, ?EntityType $entityType = null): mixed | ||
{ | ||
return new DuplicateResult($this->core->call('crm.duplicate.findbycomm', | ||
[ | ||
'type' => 'PHONE', | ||
'values' => $phones, | ||
'entity_type' => $entityType?->value | ||
])); | ||
} | ||
|
||
/** | ||
* @param array<string> $emails | ||
* @param EntityType|null $entityType | ||
* @return DuplicateResult | ||
* @throws BaseException | ||
* @throws TransportException | ||
*/ | ||
public function findByEmail(array $emails, ?EntityType $entityType = null): DuplicateResult | ||
{ | ||
return new DuplicateResult($this->core->call('crm.duplicate.findbycomm', | ||
[ | ||
'type' => 'EMAIL', | ||
'values' => $emails, | ||
'entity_type' => $entityType?->value | ||
])); | ||
} | ||
} |
This file contains 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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bitrix24\SDK\Services\CRM\Duplicates\Service; | ||
|
||
enum EntityType: string | ||
{ | ||
case Lead = 'LEAD'; | ||
case Contact = 'CONTACT'; | ||
case Company = 'COMPANY'; | ||
} |
80 changes: 80 additions & 0 deletions
80
tests/Integration/Services/CRM/Duplicates/Service/DuplicateTest.php
This file contains 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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bitrix24\SDK\Tests\Integration\Services\CRM\Duplicates\Service; | ||
|
||
use Bitrix24\SDK\Core\Exceptions\BaseException; | ||
use Bitrix24\SDK\Core\Exceptions\TransportException; | ||
use Bitrix24\SDK\Services\CRM\Contact\Service\Contact; | ||
use Bitrix24\SDK\Services\CRM\Duplicates\Service\Duplicate; | ||
use Bitrix24\SDK\Tests\Integration\Fabric; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DuplicateTest extends TestCase | ||
{ | ||
protected Contact $contactService; | ||
protected Duplicate $duplicate; | ||
|
||
/** | ||
* @return void | ||
* @throws BaseException | ||
* @throws TransportException | ||
* @covers \Bitrix24\SDK\Services\CRM\Duplicates\Service\Duplicate::findByEmail | ||
*/ | ||
public function testDuplicatesByEmailNotFound(): void | ||
{ | ||
$res = $this->duplicate->findByEmail([sprintf('%s@gmail.com', time())]); | ||
$this->assertFalse($res->hasDuplicateContacts()); | ||
$this->assertFalse($res->hasOneContact()); | ||
$this->assertCount(0, $res->getContactsId()); | ||
} | ||
|
||
/** | ||
* @return void | ||
* @throws BaseException | ||
* @throws TransportException | ||
* @covers \Bitrix24\SDK\Services\CRM\Duplicates\Service\Duplicate::findByEmail | ||
*/ | ||
public function testDuplicatesByEmailOneItemFound(): void | ||
{ | ||
$email = sprintf('%s@gmail.com', time()); | ||
$b24ContactId = $this->contactService->add([ | ||
'NAME' => 'Test', | ||
'LAST_NAME' => 'Test', | ||
'EMAIL' => [ | ||
[ | ||
'VALUE' => $email, | ||
'TYPE' => 'WORK' | ||
] | ||
] | ||
])->getId(); | ||
|
||
$res = $this->duplicate->findByEmail([$email]); | ||
$this->assertFalse($res->hasDuplicateContacts()); | ||
$this->assertTrue($res->hasOneContact()); | ||
$this->assertCount(1, $res->getContactsId()); | ||
} | ||
|
||
/** | ||
* @return void | ||
* @throws BaseException | ||
* @throws TransportException | ||
* @covers \Bitrix24\SDK\Services\CRM\Duplicates\Service\Duplicate::findByPhone | ||
*/ | ||
public function testDuplicatesByPhoneNotFound(): void | ||
{ | ||
$res = $this->duplicate->findByPhone([sprintf('+1%s', time())]); | ||
$this->assertFalse($res->hasDuplicateContacts()); | ||
$this->assertFalse($res->hasOneContact()); | ||
$this->assertCount(0, $res->getContactsId()); | ||
} | ||
|
||
|
||
public function setUp(): void | ||
{ | ||
$this->contactService = Fabric::getServiceBuilder()->getCRMScope()->contact(); | ||
$this->duplicate = Fabric::getServiceBuilder()->getCRMScope()->duplicate(); | ||
|
||
} | ||
} |
This file contains 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