|
| 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