Skip to content

Commit

Permalink
add duplicate service and tests
Browse files Browse the repository at this point in the history
Signed-off-by: mesilov <mesilov.maxim@gmail.com>
  • Loading branch information
mesilov committed Aug 13, 2023
1 parent 8243632 commit 6317849
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* `Bitrix24\SDK\Services\User\Service\User::get` - get user
* `Bitrix24\SDK\Services\User\Service\User::update` - update user
* `Bitrix24\SDK\Services\User\Service\User::search` - search users
* add [crm item support](https://github.com/mesilov/bitrix24-php-sdk/issues/330)
* add [crm item support](https://github.com/mesilov/bitrix24-php-sdk/issues/330)
* add Duplicate search support for `Bitrix24\SDK\Services\CRM\Duplicates\Service\Duplicate`

### Changed

Expand Down
12 changes: 12 additions & 0 deletions src/Services/CRM/CRMServiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,16 @@ public function item(): Item\Service\Item

return $this->serviceCache[__METHOD__];
}

public function duplicate(): Duplicates\Service\Duplicate
{
if (!isset($this->serviceCache[__METHOD__])) {
$this->serviceCache[__METHOD__] = new Duplicates\Service\Duplicate(
$this->core,
$this->log
);
}

return $this->serviceCache[__METHOD__];
}
}
50 changes: 50 additions & 0 deletions src/Services/CRM/Duplicates/Result/DuplicateResult.php
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'];
}
}
47 changes: 47 additions & 0 deletions src/Services/CRM/Duplicates/Service/Duplicate.php
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
]));
}
}
12 changes: 12 additions & 0 deletions src/Services/CRM/Duplicates/Service/EntityType.php
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';
}
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();

}
}
8 changes: 7 additions & 1 deletion tests/Unit/Stubs/NullBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ class NullBatch implements BatchOperationsInterface
{

/**
* @param string $apiMethod
* @param array $order
* @param array $filter
* @param array $select
* @param int|null $limit
* @param array|null $additionalParameters
* @inheritDoc
*/
public function getTraversableList(string $apiMethod, array $order, array $filter, array $select, ?int $limit = null): Generator
public function getTraversableList(string $apiMethod, array $order, array $filter, array $select, ?int $limit = null, ?array $additionalParameters = null): Generator
{
yield [];
}
Expand Down

0 comments on commit 6317849

Please sign in to comment.