Skip to content

Commit 9507ba6

Browse files
authored
Merge pull request #171 from Info-Expert-B24/feature/170-add-lead-contact
Feature/170 Add lead contact
2 parents ae7b36b + 62165c0 commit 9507ba6

File tree

9 files changed

+444
-0
lines changed

9 files changed

+444
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
### Added
66

7+
- Added service `Services\CRM\Lead\Service\LeadContact` with support methods,
8+
see [crm.lead.contact.* methods](https://github.com/bitrix24/b24phpsdk/issues/170):
9+
- `fields` get fiels for lead contact connection
10+
- `setItems` set contacts related with lead
11+
- `get` get contacts related to lead
12+
- `deleteItems` delete all relations for lead
13+
- `add` add contact relation with lead
14+
- `delete` delete contact relation with lead
715
- Added service `CRM\Item\Service\ItemDetailsConfiguration` with support methods,
816
see [add crm.item.details.* methods](https://github.com/bitrix24/b24phpsdk/issues/168):
917
- `getPersonal` method retrieves the settings of item cards for personal user

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ integration_tests_scope_crm_currency:
206206
.PHONY: integration_tests_deal_recurring
207207
integration_tests_deal_recurring:
208208
docker-compose run --rm php-cli vendor/bin/phpunit --testsuite integration_tests_deal_recurring
209+
210+
.PHONY: integration_tests_lead_contacts
211+
integration_tests_lead_contacts:
212+
docker-compose run --rm php-cli vendor/bin/phpunit --testsuite integration_tests_lead_contacts
209213

210214
.PHONY: integration_tests_lead_details
211215
integration_tests_lead_details:

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ parameters:
1111
- tests/Integration/Services/IMOpenLines
1212
- tests/Integration/Services/Main
1313
- tests/Integration/Services/Placement
14+
- tests/Integration/Services/CRM/Deal/Service/DealRecurringTest.php
15+
- tests/Integration/Services/CRM/Lead/Service/LeadContactTest.php
1416
- tests/Integration/Services/CRM/Item/Service/ItemDetailsConfigurationTest.php
1517
- tests/Integration/Services/CRM/Deal/Service/DealDetailsConfigurationTest.php
1618
- tests/Integration/Services/CRM/Lead/Service/LeadDetailsConfigurationTest.php

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
<testsuite name="integration_tests_deal_recurring">
6565
<file>./tests/Integration/Services/CRM/Deal/Service/DealRecurringTest.php</file>
6666
</testsuite>
67+
<testsuite name="integration_tests_lead_contacts">
68+
<file>./tests/Integration/Services/CRM/Lead/Service/LeadContactTest.php</file>
69+
</testsuite>
6770
<testsuite name="integration_tests_item_details">
6871
<file>./tests/Integration/Services/CRM/Item/Service/ItemDetailsConfigurationTest.php</file>
6972
</testsuite>

src/Services/CRM/CRMServiceBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ public function lead(): Lead\Service\Lead
360360
return $this->serviceCache[__METHOD__];
361361
}
362362

363+
public function leadContact(): Lead\Service\LeadContact
364+
{
365+
if (!isset($this->serviceCache[__METHOD__])) {
366+
$this->serviceCache[__METHOD__] = new Lead\Service\LeadContact(
367+
$this->core,
368+
$this->log
369+
);
370+
}
371+
372+
return $this->serviceCache[__METHOD__];
373+
}
374+
363375
/**
364376
* @return Lead\Service\LeadProductRows
365377
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the bitrix24-php-sdk package.
5+
*
6+
* © Vadim Soluyanov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the MIT-LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Bitrix24\SDK\Services\CRM\Lead\Result;
15+
16+
use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem;
17+
18+
/**
19+
* @property-read int $CONTACT_ID
20+
* @property-read int $SORT
21+
* @property-read bool $IS_PRIMARY
22+
*/
23+
class LeadContactConnectionItemResult extends AbstractCrmItem
24+
{
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the bitrix24-php-sdk package.
5+
*
6+
* © Vadim Soluyanov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the MIT-LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
13+
declare(strict_types=1);
14+
15+
namespace Bitrix24\SDK\Services\CRM\Lead\Result;
16+
17+
use Bitrix24\SDK\Core\Exceptions\BaseException;
18+
use Bitrix24\SDK\Core\Result\AbstractResult;
19+
20+
class LeadContactConnectionResult extends AbstractResult
21+
{
22+
/**
23+
* @return LeadContactConnectionItemResult[]
24+
* @throws BaseException
25+
*/
26+
public function getContactConnections(): array
27+
{
28+
$res = [];
29+
foreach ($this->getCoreResponse()->getResponseData()->getResult() as $item) {
30+
$res[] = new LeadContactConnectionItemResult($item);
31+
}
32+
33+
return $res;
34+
}
35+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the bitrix24-php-sdk package.
5+
*
6+
* © Vadim Soluyanov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the MIT-LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Bitrix24\SDK\Services\CRM\Lead\Service;
15+
16+
use Bitrix24\SDK\Attributes\ApiServiceMetadata;
17+
use Bitrix24\SDK\Core\Contracts\CoreInterface;
18+
use Bitrix24\SDK\Core\Credentials\Scope;
19+
use Bitrix24\SDK\Core\Exceptions\BaseException;
20+
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
21+
use Bitrix24\SDK\Core\Exceptions\TransportException;
22+
use Bitrix24\SDK\Core\Result\DeletedItemResult;
23+
use Bitrix24\SDK\Core\Result\FieldsResult;
24+
use Bitrix24\SDK\Core\Result\UpdatedItemResult;
25+
use Bitrix24\SDK\Services\AbstractService;
26+
use Bitrix24\SDK\Services\CRM\Common\ContactConnection;
27+
use Bitrix24\SDK\Services\CRM\Lead\Result\LeadContactConnectionResult;
28+
use Psr\Log\LoggerInterface;
29+
use Bitrix24\SDK\Attributes\ApiEndpointMetadata;
30+
31+
#[ApiServiceMetadata(new Scope(['crm']))]
32+
class LeadContact extends AbstractService
33+
{
34+
/**
35+
* Retrieves the description of fields for the lead-contact link used by the methods in the crm.lead.contact.* family
36+
*
37+
* @link https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-fields.html
38+
*
39+
* @throws BaseException
40+
* @throws TransportException
41+
*/
42+
#[ApiEndpointMetadata(
43+
'crm.lead.contact.fields',
44+
'https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-fields.html',
45+
'Retrieves the description of fields for the lead-contact link used by the methods in the crm.lead.contact.* family'
46+
)]
47+
public function fields(): FieldsResult
48+
{
49+
return new FieldsResult($this->core->call('crm.lead.contact.fields'));
50+
}
51+
52+
/**
53+
* Attaches a list of contacts to the specified lead
54+
*
55+
* @param non-negative-int $leadId
56+
* @param ContactConnection[] $contactConnections
57+
* @throws InvalidArgumentException
58+
* @link https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-items-set.html
59+
*/
60+
#[ApiEndpointMetadata(
61+
'crm.lead.contact.items.set',
62+
'https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-items-set.html',
63+
'Attaches a list of contacts to the specified lead'
64+
)]
65+
public function setItems(int $leadId, array $contactConnections): UpdatedItemResult
66+
{
67+
$items = [];
68+
foreach ($contactConnections as $contactConnection) {
69+
if (!$contactConnection instanceof ContactConnection) {
70+
throw new InvalidArgumentException(
71+
sprintf('array item «%s» must be «%s» type', gettype($contactConnection), ContactConnection::class)
72+
);
73+
}
74+
75+
$items[] = [
76+
'CONTACT_ID' => $contactConnection->contactId,
77+
'SORT' => $contactConnection->sort,
78+
'IS_PRIMARY' => $contactConnection->isPrimary ? 'Y' : 'N'
79+
];
80+
}
81+
82+
if ($items === []) {
83+
throw new InvalidArgumentException('empty contact connections array');
84+
}
85+
86+
return new UpdatedItemResult(
87+
$this->core->call('crm.lead.contact.items.set', [
88+
'id' => $leadId,
89+
'items' => $items
90+
])
91+
);
92+
}
93+
94+
/**
95+
* Retrieves a list of contacts linked to the lead
96+
*
97+
* @param non-negative-int $leadId
98+
* @link https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-items-get.html
99+
*/
100+
#[ApiEndpointMetadata(
101+
'crm.lead.contact.items.get',
102+
'https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-items-get.html',
103+
'Retrieves a list of contacts linked to the lead'
104+
)]
105+
public function get(int $leadId): LeadContactConnectionResult
106+
{
107+
return new LeadContactConnectionResult($this->core->call('crm.lead.contact.items.get', [
108+
'id' => $leadId
109+
]));
110+
}
111+
112+
/**
113+
* Removes a list of contacts from the lead
114+
*
115+
* @param non-negative-int $leadId
116+
* @link https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-items-delete.html
117+
*/
118+
#[ApiEndpointMetadata(
119+
'crm.lead.contact.items.delete',
120+
'https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-items-delete.html',
121+
'Removes a list of contacts from the lead'
122+
)]
123+
public function deleteItems(int $leadId): DeletedItemResult
124+
{
125+
return new DeletedItemResult($this->core->call('crm.lead.contact.items.delete', [
126+
'id' => $leadId
127+
]));
128+
}
129+
130+
/**
131+
* Adds a contact link to the specified lead
132+
*
133+
* @param non-negative-int $leadId
134+
* @link https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/index.html
135+
*/
136+
#[ApiEndpointMetadata(
137+
'crm.lead.contact.add',
138+
'https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/index.html',
139+
'Adds a contact link to the specified lead'
140+
)]
141+
public function add(int $leadId, ContactConnection $contactConnection): UpdatedItemResult
142+
{
143+
return new UpdatedItemResult($this->core->call('crm.lead.contact.add', [
144+
'id' => $leadId,
145+
'fields' => [
146+
'CONTACT_ID' => $contactConnection->contactId,
147+
'SORT' => $contactConnection->sort,
148+
'IS_PRIMARY' => $contactConnection->isPrimary ? 'Y' : 'N'
149+
]
150+
]));
151+
}
152+
153+
/**
154+
* Removes a contact link from the specified lead
155+
*
156+
* @param non-negative-int $leadId
157+
* @param non-negative-int $contactId
158+
* @link https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-delete.html
159+
*/
160+
#[ApiEndpointMetadata(
161+
'crm.lead.contact.delete',
162+
'https://apidocs.bitrix24.com/api-reference/crm/leads/management-communication/crm-lead-contact-delete.html',
163+
'Removes a contact link from the specified lead'
164+
)]
165+
public function delete(int $leadId, int $contactId): DeletedItemResult
166+
{
167+
return new DeletedItemResult($this->core->call('crm.lead.contact.delete', [
168+
'id' => $leadId,
169+
'fields' => [
170+
'CONTACT_ID' => $contactId
171+
]
172+
]));
173+
}
174+
}

0 commit comments

Comments
 (0)