From 6cee3f80102f166dee9bd3b26eeaa32bf029ebee Mon Sep 17 00:00:00 2001 From: mesilov Date: Sat, 27 Nov 2021 23:36:21 +0300 Subject: [PATCH 1/7] update Scope.php Signed-off-by: mesilov --- src/Core/Credentials/Scope.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Core/Credentials/Scope.php b/src/Core/Credentials/Scope.php index f9b5d10f..3ddda1fa 100644 --- a/src/Core/Credentials/Scope.php +++ b/src/Core/Credentials/Scope.php @@ -16,11 +16,12 @@ class Scope /** * @var string[] */ - protected $availableScope = [ + protected array $availableScope = [ 'app', 'bizproc', 'calendar', 'call', + 'catalog', 'contact_center', 'crm', 'delivery', @@ -60,7 +61,7 @@ class Scope /** * @var array */ - protected $currentScope = []; + protected array $currentScope = []; /** * Scope constructor. From ac7738bb3224bda96e744f6e8cac0adbe3f96e45 Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 28 Nov 2021 00:19:01 +0300 Subject: [PATCH 2/7] add userfield type Signed-off-by: mesilov --- CHANGELOG.md | 5 +- src/Services/CRM/CRMServiceBuilder.php | 15 ++++ .../Result/UserfieldTypeItemResult.php | 15 ++++ .../Userfield/Result/UserfieldTypesResult.php | 25 +++++++ .../CRM/Userfield/Service/Userfield.php | 73 +++++++++++++++++++ .../CRM/Userfield/Service/UserfieldTest.php | 63 ++++++++++++++++ 6 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 src/Services/CRM/Userfield/Result/UserfieldTypeItemResult.php create mode 100644 src/Services/CRM/Userfield/Result/UserfieldTypesResult.php create mode 100644 src/Services/CRM/Userfield/Service/Userfield.php create mode 100644 tests/Integration/Services/CRM/Userfield/Service/UserfieldTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index cc5d3991..324069a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,13 @@ # bitrix24-php-sdk change log -## 2.0-alpha.5 – 26.11.2021 +## 2.0-alpha.5 – 28.11.2021 ### Added * add method `countByFilter` for all related services, see issue [Добавить для всех сущностей метод подсчёта количества элементов по фильтру #228](https://github.com/mesilov/bitrix24-php-sdk/issues/228) - +* add in scope «CRM» Userfield service and integration test + ### Removed * remove all `0.*` and `1.*` code from `2.*` branch diff --git a/src/Services/CRM/CRMServiceBuilder.php b/src/Services/CRM/CRMServiceBuilder.php index d8c2e0be..bde5dfd5 100644 --- a/src/Services/CRM/CRMServiceBuilder.php +++ b/src/Services/CRM/CRMServiceBuilder.php @@ -125,4 +125,19 @@ public function product(): Product\Service\Product return $this->serviceCache[__METHOD__]; } + + /** + * @return Userfield\Service\Userfield + */ + public function userfield(): Userfield\Service\Userfield + { + if (!isset($this->serviceCache[__METHOD__])) { + $this->serviceCache[__METHOD__] = new Userfield\Service\Userfield( + $this->core, + $this->log + ); + } + + return $this->serviceCache[__METHOD__]; + } } \ No newline at end of file diff --git a/src/Services/CRM/Userfield/Result/UserfieldTypeItemResult.php b/src/Services/CRM/Userfield/Result/UserfieldTypeItemResult.php new file mode 100644 index 00000000..57df56f0 --- /dev/null +++ b/src/Services/CRM/Userfield/Result/UserfieldTypeItemResult.php @@ -0,0 +1,15 @@ +getCoreResponse()->getResponseData()->getResult()->getResultData() as $item) { + $res[] = new UserfieldTypeItemResult($item); + } + + return $res; + } +} \ No newline at end of file diff --git a/src/Services/CRM/Userfield/Service/Userfield.php b/src/Services/CRM/Userfield/Service/Userfield.php new file mode 100644 index 00000000..8f436efb --- /dev/null +++ b/src/Services/CRM/Userfield/Service/Userfield.php @@ -0,0 +1,73 @@ +core->call('crm.userfield.types')); + } + + /** + * Returns field description for user fields. + * + * @link https://training.bitrix24.com/rest_help/crm/userfields/crm_userfield_fields.php + * @return \Bitrix24\SDK\Core\Result\FieldsResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + */ + public function fields(): FieldsResult + { + return new FieldsResult($this->core->call('crm.userfield.fields')); + } + + /** + * Returns field description for "enumeration" user field type (list). + * + * @link https://training.bitrix24.com/rest_help/crm/userfields/crm_userfield_enumeration_fields.php + * @return \Bitrix24\SDK\Core\Result\FieldsResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + */ + public function enumerationFields(): FieldsResult + { + return new FieldsResult($this->core->call('crm.userfield.enumeration.fields')); + } + + /** + * Returns settings field description for user field type. + * + * @link https://training.bitrix24.com/rest_help/crm/userfields/crm_userfield_settings_fields.php + * + * @param string $userfieldTypeId + * + * @return \Bitrix24\SDK\Core\Result\FieldsResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + */ + public function settingsFields(string $userfieldTypeId): FieldsResult + { + return new FieldsResult( + $this->core->call('crm.userfield.settings.fields', [ + 'type' => $userfieldTypeId, + ]) + ); + } +} \ No newline at end of file diff --git a/tests/Integration/Services/CRM/Userfield/Service/UserfieldTest.php b/tests/Integration/Services/CRM/Userfield/Service/UserfieldTest.php new file mode 100644 index 00000000..f1397ca5 --- /dev/null +++ b/tests/Integration/Services/CRM/Userfield/Service/UserfieldTest.php @@ -0,0 +1,63 @@ +userfieldService->fields()->getFieldsDescription()); + } + + /** + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @covers \Bitrix24\SDK\Services\CRM\Userfield\Service\Userfield::enumerationFields + */ + public function testEnumerationFields(): void + { + self::assertIsArray($this->userfieldService->enumerationFields()->getFieldsDescription()); + } + + /** + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @covers \Bitrix24\SDK\Services\CRM\Userfield\Service\Userfield::settingsFields + */ + public function testSettingsFields(): void + { + foreach ($this->userfieldService->types()->getTypes() as $typeItem) { + self::assertIsArray($this->userfieldService->settingsFields($typeItem->ID)->getFieldsDescription()); + } + } + + /** + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @covers \Bitrix24\SDK\Services\CRM\Userfield\Service\Userfield::types + */ + public function testTypes(): void + { + $ufTypes = $this->userfieldService->types(); + $this->assertGreaterThan(10, $ufTypes->getTypes()); + } + + + public function setUp(): void + { + $this->userfieldService = Fabric::getServiceBuilder()->getCRMScope()->userfield(); + } +} \ No newline at end of file From 68aea3d57998f6ff1abcc3bed37fb124a42d3782 Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 28 Nov 2021 02:21:58 +0300 Subject: [PATCH 3/7] add ContactUserfield service and test Signed-off-by: mesilov --- src/Services/CRM/CRMServiceBuilder.php | 15 ++ .../Result/ContactUserfieldItemResult.php | 11 + .../Contact/Result/ContactUserfieldResult.php | 18 ++ .../Result/ContactUserfieldsResult.php | 25 +++ .../CRM/Contact/Service/ContactUserfield.php | 202 ++++++++++++++++++ .../UserfieldNameIsTooLongException.php | 11 + .../Result/AbstractUserfieldItemResult.php | 32 +++ .../Contact/Service/ContactUserfieldTest.php | 142 ++++++++++++ 8 files changed, 456 insertions(+) create mode 100644 src/Services/CRM/Contact/Result/ContactUserfieldItemResult.php create mode 100644 src/Services/CRM/Contact/Result/ContactUserfieldResult.php create mode 100644 src/Services/CRM/Contact/Result/ContactUserfieldsResult.php create mode 100644 src/Services/CRM/Contact/Service/ContactUserfield.php create mode 100644 src/Services/CRM/Userfield/Exceptions/UserfieldNameIsTooLongException.php create mode 100644 src/Services/CRM/Userfield/Result/AbstractUserfieldItemResult.php create mode 100644 tests/Integration/Services/CRM/Contact/Service/ContactUserfieldTest.php diff --git a/src/Services/CRM/CRMServiceBuilder.php b/src/Services/CRM/CRMServiceBuilder.php index bde5dfd5..672fabf9 100644 --- a/src/Services/CRM/CRMServiceBuilder.php +++ b/src/Services/CRM/CRMServiceBuilder.php @@ -86,6 +86,21 @@ public function contact(): Contact\Service\Contact return $this->serviceCache[__METHOD__]; } + /** + * @return \Bitrix24\SDK\Services\CRM\Contact\Service\ContactUserfield + */ + public function contactUserfield(): Contact\Service\ContactUserfield + { + if (!isset($this->serviceCache[__METHOD__])) { + $this->serviceCache[__METHOD__] = new Contact\Service\ContactUserfield( + $this->core, + $this->log + ); + } + + return $this->serviceCache[__METHOD__]; + } + /** * @return Deal\Service\DealProductRows */ diff --git a/src/Services/CRM/Contact/Result/ContactUserfieldItemResult.php b/src/Services/CRM/Contact/Result/ContactUserfieldItemResult.php new file mode 100644 index 00000000..06058da6 --- /dev/null +++ b/src/Services/CRM/Contact/Result/ContactUserfieldItemResult.php @@ -0,0 +1,11 @@ +getCoreResponse()->getResponseData()->getResult()->getResultData()); + } +} \ No newline at end of file diff --git a/src/Services/CRM/Contact/Result/ContactUserfieldsResult.php b/src/Services/CRM/Contact/Result/ContactUserfieldsResult.php new file mode 100644 index 00000000..fc583cb4 --- /dev/null +++ b/src/Services/CRM/Contact/Result/ContactUserfieldsResult.php @@ -0,0 +1,25 @@ +getCoreResponse()->getResponseData()->getResult()->getResultData() as $item) { + $res[] = new ContactUserfieldItemResult($item); + } + + return $res; + } +} \ No newline at end of file diff --git a/src/Services/CRM/Contact/Service/ContactUserfield.php b/src/Services/CRM/Contact/Service/ContactUserfield.php new file mode 100644 index 00000000..4b873c17 --- /dev/null +++ b/src/Services/CRM/Contact/Service/ContactUserfield.php @@ -0,0 +1,202 @@ +core->call( + 'crm.contact.userfield.list', + [ + 'order' => $order, + 'filter' => $filter, + ] + ) + ); + } + + /** + * Creates a new user field for contacts. + * + * System limitation for field name - 20 characters. + * Prefix UF_CRM_is always added to the user field name. + * As a result, the actual name length - 13 characters. + * + * @param array{ + * FIELD_NAME?: string, + * USER_TYPE_ID?: string, + * XML_ID?: string, + * SORT?: string, + * MULTIPLE?: string, + * MANDATORY?: string, + * SHOW_FILTER?: string, + * SHOW_IN_LIST?: string, + * EDIT_IN_LIST?: string, + * IS_SEARCHABLE?: string, + * EDIT_FORM_LABEL?: string, + * LIST_COLUMN_LABEL?: string, + * LIST_FILTER_LABEL?: string, + * ERROR_MESSAGE?: string, + * HELP_MESSAGE?: string, + * LIST?: string, + * SETTINGS?: string, + * } $userfieldItemFields + * + * @return \Bitrix24\SDK\Core\Result\AddedItemResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @throws UserfieldNameIsTooLongException + * @link https://training.bitrix24.com/rest_help/crm/contacts/crm_contact_userfield_add.php + * + */ + public function add(array $userfieldItemFields): AddedItemResult + { + if (strlen($userfieldItemFields['FIELD_NAME']) > 13) { + throw new UserfieldNameIsTooLongException( + sprintf( + 'userfield name %s is too long %s, maximum length - 13 characters', + $userfieldItemFields['FIELD_NAME'], + strlen($userfieldItemFields['FIELD_NAME']) + ) + ); + } + + return new AddedItemResult( + $this->core->call( + 'crm.contact.userfield.add', + [ + 'fields' => $userfieldItemFields, + ] + ) + ); + } + + /** + * Deleted user field for contacts. + * + * @param int $userfieldId + * + * @return \Bitrix24\SDK\Core\Result\DeletedItemResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @link https://training.bitrix24.com/rest_help/crm/contacts/crm_contact_userfield_delete.php + * + */ + public function delete(int $userfieldId): DeletedItemResult + { + return new DeletedItemResult( + $this->core->call( + 'crm.contact.userfield.delete', + [ + 'id' => $userfieldId, + ] + ) + ); + } + + /** + * Returns a user field for contacts by ID. + * + * @param int $contactUserfieldItemId + * + * @return \Bitrix24\SDK\Services\CRM\Contact\Result\ContactUserfieldResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @link https://training.bitrix24.com/rest_help/crm/contacts/crm_contact_userfield_get.php + */ + public function get(int $contactUserfieldItemId): ContactUserfieldResult + { + return new ContactUserfieldResult( + $this->core->call( + 'crm.contact.userfield.get', + [ + 'id' => $contactUserfieldItemId, + ] + ) + ); + } + + /** + * Updates an existing user field for contacts. + * + * @param int $contactUserfieldItemId + * @param array $userfieldFieldsToUpdate + * + * @return \Bitrix24\SDK\Core\Result\UpdatedItemResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @link https://training.bitrix24.com/rest_help/crm/contacts/crm_contact_userfield_update.php + */ + public function update(int $contactUserfieldItemId, array $userfieldFieldsToUpdate): UpdatedItemResult + { + return new UpdatedItemResult( + $this->core->call( + 'crm.contact.userfield.update', + [ + 'id' => $contactUserfieldItemId, + 'fields' => $userfieldFieldsToUpdate, + ] + ) + ); + } +} \ No newline at end of file diff --git a/src/Services/CRM/Userfield/Exceptions/UserfieldNameIsTooLongException.php b/src/Services/CRM/Userfield/Exceptions/UserfieldNameIsTooLongException.php new file mode 100644 index 00000000..1c637c9e --- /dev/null +++ b/src/Services/CRM/Userfield/Exceptions/UserfieldNameIsTooLongException.php @@ -0,0 +1,11 @@ + [ + [ + 'FIELD_NAME' => sprintf('%s%s', substr((string)random_int(0, PHP_INT_MAX), 0, 3), time()), + 'EDIT_FORM_LABEL' => [ + 'ru' => 'тест uf тип string', + 'en' => 'test uf type string', + ], + 'LIST_COLUMN_LABEL' => [ + 'ru' => 'тест uf тип string', + 'en' => 'test uf type string', + ], + 'USER_TYPE_ID' => 'string', + 'XML_ID' => 'b24phpsdk_type_string', + 'SETTINGS' => [], + ], + ]; + + mt_srand(); + yield 'user type id integer' => [ + [ + 'FIELD_NAME' => sprintf('%s%s', substr((string)random_int(0, PHP_INT_MAX), 0, 3), time()), + 'EDIT_FORM_LABEL' => [ + 'ru' => 'тест uf тип integer', + 'en' => 'test uf type integer', + ], + 'LIST_COLUMN_LABEL' => [ + 'ru' => 'тест uf тип integer', + 'en' => 'test uf type integer', + ], + 'USER_TYPE_ID' => 'integer', + 'XML_ID' => 'b24phpsdk_type_integer', + 'SETTINGS' => [], + ], + ]; + } + + /** + * @param array $newUserFieldItem + * + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @throws \Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNameIsTooLongException + * @covers ContactUserfield::add + * @dataProvider systemUserfieldsDemoDataDataProvider + */ + public function testAdd(array $newUserFieldItem): void + { + self::assertGreaterThanOrEqual(1, $this->contactUserfieldService->add($newUserFieldItem)->getId()); + } + + /** + * @param array $newUserFieldItem + * + * @dataProvider systemUserfieldsDemoDataDataProvider + * @covers ContactUserfield::delete + */ + public function testDelete(array $newUserFieldItem): void + { + $newUserfieldId = $this->contactUserfieldService->add($newUserFieldItem)->getId(); + $this->assertTrue($this->contactUserfieldService->delete($newUserfieldId)->isSuccess()); + } + + /** + * @param array $newUserFieldItem + * + * @dataProvider systemUserfieldsDemoDataDataProvider + * @covers ContactUserfield::get + */ + public function testGet(array $newUserFieldItem): void + { + $newUserfieldId = $this->contactUserfieldService->add($newUserFieldItem)->getId(); + $ufField = $this->contactUserfieldService->get($newUserfieldId)->userfieldItem(); + $this->assertEquals($newUserfieldId, $ufField->ID); + $this->assertEquals($newUserFieldItem['USER_TYPE_ID'], $ufField->USER_TYPE_ID); + $this->assertEquals('UF_CRM_' . $newUserFieldItem['FIELD_NAME'], $ufField->FIELD_NAME); + $this->assertEquals($newUserFieldItem['XML_ID'], $ufField->XML_ID); + } + + /** + * @param array $newUserFieldItem + * + * @dataProvider systemUserfieldsDemoDataDataProvider + * @covers ContactUserfield::update + */ + public function testUpdate(array $newUserFieldItem): void + { + $newUserfieldId = $this->contactUserfieldService->add($newUserFieldItem)->getId(); + $ufFieldBefore = $this->contactUserfieldService->get($newUserfieldId)->userfieldItem(); + $this->assertEquals($newUserfieldId, $ufFieldBefore->ID); + $this->assertEquals($newUserFieldItem['USER_TYPE_ID'], $ufFieldBefore->USER_TYPE_ID); + $this->assertEquals('UF_CRM_' . $newUserFieldItem['FIELD_NAME'], $ufFieldBefore->FIELD_NAME); + $this->assertEquals($newUserFieldItem['XML_ID'], $ufFieldBefore->XML_ID); + + $this->assertTrue( + $this->contactUserfieldService->update( + $newUserfieldId, + [ + 'EDIT_FORM_LABEL' => $newUserFieldItem['EDIT_FORM_LABEL']['en'] . 'QQQ', + ] + )->isSuccess() + ); + + $ufFieldAfter = $this->contactUserfieldService->get($newUserfieldId)->userfieldItem(); + $this->assertEquals($ufFieldBefore->EDIT_FORM_LABEL['en'] . 'QQQ', $ufFieldAfter->EDIT_FORM_LABEL['en']); + } + + /** + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @covers \Bitrix24\SDK\Services\CRM\Contact\Service\ContactUserfield::list + */ + public function testList(): void + { + $ufFields = $this->contactUserfieldService->list([], []); + $this->assertGreaterThanOrEqual(0, count($ufFields->getUserfields())); + } + + public function setUp(): void + { + $this->contactUserfieldService = Fabric::getServiceBuilder()->getCRMScope()->contactUserfield(); + } +} \ No newline at end of file From 0c06ed55b79675c7012ab9e08e3c956dd078759d Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 28 Nov 2021 12:37:00 +0300 Subject: [PATCH 4/7] first contact userfields build Signed-off-by: mesilov --- CHANGELOG.md | 9 +- src/Core/Result/AbstractItem.php | 12 +- .../CRM/Common/Result/AbstractCrmItem.php | 65 +++++++++++ .../CRM/Contact/Result/ContactItemResult.php | 109 ++++++++++-------- src/Services/CRM/Contact/Service/Contact.php | 2 +- .../CRM/Deal/Result/DealItemResult.php | 4 +- .../CRM/Product/Result/ProductItemResult.php | 4 +- .../Exceptions/UserfieldNotFoundException.php | 11 ++ .../Result/AbstractUserfieldItemResult.php | 12 ++ .../Service/ContactUserfieldUseCaseTest.php | 92 +++++++++++++++ 10 files changed, 264 insertions(+), 56 deletions(-) create mode 100644 src/Services/CRM/Common/Result/AbstractCrmItem.php create mode 100644 src/Services/CRM/Userfield/Exceptions/UserfieldNotFoundException.php create mode 100644 tests/Integration/Services/CRM/Contact/Service/ContactUserfieldUseCaseTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 324069a6..cfe372e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,11 +7,18 @@ * add method `countByFilter` for all related services, see issue [Добавить для всех сущностей метод подсчёта количества элементов по фильтру #228](https://github.com/mesilov/bitrix24-php-sdk/issues/228) * add in scope «CRM» Userfield service and integration test - +* add in scope «CRM» ContactUserfield service and integration test +* add method getUserfieldByFieldName for `ContactItemResult` +* add exception `UserfieldNotFoundException` + ### Removed * remove all `0.*` and `1.*` code from `2.*` branch +### Changed + +* update type definition for `ContactItemResult`, now return types will be cast to real types: DateTimeInterface, int, boolean etc + ## 2.0-alpha.4 – 25.11.2021 ### Changed diff --git a/src/Core/Result/AbstractItem.php b/src/Core/Result/AbstractItem.php index 82b5cd81..ca18e485 100644 --- a/src/Core/Result/AbstractItem.php +++ b/src/Core/Result/AbstractItem.php @@ -13,7 +13,7 @@ */ abstract class AbstractItem implements \IteratorAggregate { - private array $data; + protected array $data; /** * AbstractItem constructor. @@ -75,4 +75,14 @@ public function getIterator() { return new \ArrayIterator($this->data); } + + /** + * @param string $key + * + * @return bool + */ + protected function isKeyExists(string $key): bool + { + return array_key_exists($key, $this->data); + } } \ No newline at end of file diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php new file mode 100644 index 00000000..e238ed90 --- /dev/null +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -0,0 +1,65 @@ +data[$offset]; + case 'EXPORT': + case 'HAS_PHONE': + case 'HAS_EMAIL': + case 'HAS_IMOL': + case 'OPENED': + return $this->data[$offset] === 'Y'; + case 'DATE_CREATE': + case 'DATE_MODIFY': + case 'BIRTHDATE': + if ($this->data[$offset] !== '') { + return DateTimeImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]); + } + + return null; + default: + return $this->data[$offset] ?? null; + } + } + + /** + * get userfield by field name + * + * @param string $fieldName + * + * @return mixed|null + * @throws \Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException + */ + protected function getKeyWithUserfieldByFieldName(string $fieldName) + { + $fieldName = self::CRM_USERFIELD_PREFIX . $fieldName; + if (!$this->isKeyExists($fieldName)) { + throw new UserfieldNotFoundException(sprintf('crm userfield not found by field name %s', $fieldName)); + } + + return $this->$fieldName; + } +} \ No newline at end of file diff --git a/src/Services/CRM/Contact/Result/ContactItemResult.php b/src/Services/CRM/Contact/Result/ContactItemResult.php index d2e50906..82293707 100644 --- a/src/Services/CRM/Contact/Result/ContactItemResult.php +++ b/src/Services/CRM/Contact/Result/ContactItemResult.php @@ -4,59 +4,70 @@ namespace Bitrix24\SDK\Services\CRM\Contact\Result; -use Bitrix24\SDK\Core\Result\AbstractItem; +use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem; +use DateTimeInterface; /** * Class ContactItemResult * - * @property-read int $ID - * @property-read string $HONORIFIC - * @property-read string $NAME - * @property-read string $SECOND_NAME - * @property-read string $LAST_NAME - * @property-read string $PHOTO - * @property-read string $BIRTHDATE - * @property-read string $TYPE_ID - * @property-read string $SOURCE_ID - * @property-read string $SOURCE_DESCRIPTION - * @property-read string $POST - * @property-read string $ADDRESS - * @property-read string $ADDRESS_2 - * @property-read string $ADDRESS_CITY - * @property-read string $ADDRESS_POSTAL_CODE - * @property-read string $ADDRESS_REGION - * @property-read string $ADDRESS_PROVINCE - * @property-read string $ADDRESS_COUNTRY - * @property-read string $ADDRESS_COUNTRY_CODE - * @property-read int $ADDRESS_LOC_ADDR_ID - * @property-read string $COMMENTS - * @property-read string $OPENED - * @property-read string $EXPORT - * @property-read string $HAS_PHONE - * @property-read string $HAS_EMAIL - * @property-read string $HAS_IMOL - * @property-read string $ASSIGNED_BY_ID - * @property-read string $CREATED_BY_ID - * @property-read string $MODIFY_BY_ID - * @property-read string $DATE_CREATE - * @property-read string $DATE_MODIFY - * @property-read string $COMPANY_ID - * @property-read string $COMPANY_IDS - * @property-read string $LEAD_ID - * @property-read string $ORIGINATOR_ID - * @property-read string $ORIGIN_ID - * @property-read string $ORIGIN_VERSION - * @property-read int $FACE_ID - * @property-read string $UTM_SOURCE - * @property-read string $UTM_MEDIUM - * @property-read string $UTM_CAMPAIGN - * @property-read string $UTM_CONTENT - * @property-read string $UTM_TERM - * @property-read string $PHONE - * @property-read string $EMAIL - * @property-read string $WEB - * @property-read string $IM + * @property-read int $ID + * @property-read string $HONORIFIC + * @property-read string $NAME + * @property-read string $SECOND_NAME + * @property-read string $LAST_NAME + * @property-read string $PHOTO + * @property-read null|DateTimeInterface $BIRTHDATE + * @property-read string $TYPE_ID + * @property-read string $SOURCE_ID + * @property-read string $SOURCE_DESCRIPTION + * @property-read string $POST + * @property-read string $ADDRESS + * @property-read string $ADDRESS_2 + * @property-read string $ADDRESS_CITY + * @property-read string $ADDRESS_POSTAL_CODE + * @property-read string $ADDRESS_REGION + * @property-read string $ADDRESS_PROVINCE + * @property-read string $ADDRESS_COUNTRY + * @property-read string $ADDRESS_COUNTRY_CODE + * @property-read int $ADDRESS_LOC_ADDR_ID + * @property-read string $COMMENTS + * @property-read string $OPENED + * @property-read bool $EXPORT + * @property-read string $HAS_PHONE + * @property-read string $HAS_EMAIL + * @property-read string $HAS_IMOL + * @property-read int $ASSIGNED_BY_ID + * @property-read int $CREATED_BY_ID + * @property-read int $MODIFY_BY_ID + * @property-read DateTimeInterface $DATE_CREATE + * @property-read DateTimeInterface $DATE_MODIFY + * @property-read string $COMPANY_ID + * @property-read string $COMPANY_IDS + * @property-read string $LEAD_ID + * @property-read string $ORIGINATOR_ID + * @property-read string $ORIGIN_ID + * @property-read string $ORIGIN_VERSION + * @property-read int $FACE_ID + * @property-read string $UTM_SOURCE + * @property-read string $UTM_MEDIUM + * @property-read string $UTM_CAMPAIGN + * @property-read string $UTM_CONTENT + * @property-read string $UTM_TERM + * @property-read string $PHONE + * @property-read string $EMAIL + * @property-read string $WEB + * @property-read string $IM */ -class ContactItemResult extends AbstractItem +class ContactItemResult extends AbstractCrmItem { + /** + * @param string $userfieldName + * + * @return mixed|null + * @throws \Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException + */ + public function getUserfieldByFieldName(string $userfieldName) + { + return $this->getKeyWithUserfieldByFieldName($userfieldName); + } } \ No newline at end of file diff --git a/src/Services/CRM/Contact/Service/Contact.php b/src/Services/CRM/Contact/Service/Contact.php index b3a7e1bc..211d6b6f 100644 --- a/src/Services/CRM/Contact/Service/Contact.php +++ b/src/Services/CRM/Contact/Service/Contact.php @@ -360,7 +360,7 @@ public function list(array $order, array $filter, array $select, int $start): Co * @throws BaseException * @throws TransportException */ - public function update(int $contactId, array $fields, array $params): UpdatedItemResult + public function update(int $contactId, array $fields, array $params = []): UpdatedItemResult { return new UpdatedItemResult( $this->core->call( diff --git a/src/Services/CRM/Deal/Result/DealItemResult.php b/src/Services/CRM/Deal/Result/DealItemResult.php index 123ffa90..e0341648 100644 --- a/src/Services/CRM/Deal/Result/DealItemResult.php +++ b/src/Services/CRM/Deal/Result/DealItemResult.php @@ -4,7 +4,7 @@ namespace Bitrix24\SDK\Services\CRM\Deal\Result; -use Bitrix24\SDK\Core\Result\AbstractItem; +use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem; /** * Class DealItemResult @@ -45,6 +45,6 @@ * @property-read string|null $UTM_CONTENT * @property-read string|null $UTM_TERM */ -class DealItemResult extends AbstractItem +class DealItemResult extends AbstractCrmItem { } \ No newline at end of file diff --git a/src/Services/CRM/Product/Result/ProductItemResult.php b/src/Services/CRM/Product/Result/ProductItemResult.php index 9e894646..9d81c9a1 100644 --- a/src/Services/CRM/Product/Result/ProductItemResult.php +++ b/src/Services/CRM/Product/Result/ProductItemResult.php @@ -4,7 +4,7 @@ namespace Bitrix24\SDK\Services\CRM\Product\Result; -use Bitrix24\SDK\Core\Result\AbstractItem; +use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem; /** * Class ProductItemResult @@ -31,6 +31,6 @@ * @property-read int $MODIFIED_BY * @property-read int $CREATED_BY */ -class ProductItemResult extends AbstractItem +class ProductItemResult extends AbstractCrmItem { } \ No newline at end of file diff --git a/src/Services/CRM/Userfield/Exceptions/UserfieldNotFoundException.php b/src/Services/CRM/Userfield/Exceptions/UserfieldNotFoundException.php new file mode 100644 index 00000000..eb551e77 --- /dev/null +++ b/src/Services/CRM/Userfield/Exceptions/UserfieldNotFoundException.php @@ -0,0 +1,11 @@ +FIELD_NAME, self::CRM_USERFIELD_PREFIX_LENGTH); + } } \ No newline at end of file diff --git a/tests/Integration/Services/CRM/Contact/Service/ContactUserfieldUseCaseTest.php b/tests/Integration/Services/CRM/Contact/Service/ContactUserfieldUseCaseTest.php new file mode 100644 index 00000000..0e0cfb53 --- /dev/null +++ b/tests/Integration/Services/CRM/Contact/Service/ContactUserfieldUseCaseTest.php @@ -0,0 +1,92 @@ +contactUserfieldService->get($this->contactUserfieldId)->userfieldItem(); + $ufOriginalFieldName = $ufMetadata->getOriginalFieldName(); + $ufFieldName = $ufMetadata->FIELD_NAME; + + // add contact with uf value + $fieldNameValue = 'test field value'; + $newContactId = $this->contactService->add( + [ + 'NAME' => 'test contact', + $ufFieldName => $fieldNameValue, + ] + )->getId(); + $contact = $this->contactService->get($newContactId)->contact(); + $this->assertEquals($fieldNameValue, $contact->getUserfieldByFieldName($ufOriginalFieldName)); + + // update contact userfield value + $newUfValue = 'test 2'; + $this->assertTrue( + $this->contactService->update( + $contact->ID, + [ + $ufFieldName => $newUfValue, + ] + )->isSuccess() + ); + $updatedContact = $this->contactService->get($contact->ID)->contact(); + $this->assertEquals($newUfValue, $updatedContact->getUserfieldByFieldName($ufOriginalFieldName)); + } + + /** + * @throws \Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNameIsTooLongException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @throws \Bitrix24\SDK\Core\Exceptions\InvalidArgumentException + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + */ + public function setUp(): void + { + $this->contactService = Fabric::getServiceBuilder()->getCRMScope()->contact(); + $this->contactUserfieldService = Fabric::getServiceBuilder()->getCRMScope()->contactUserfield(); + + $this->contactUserfieldId = $this->contactUserfieldService->add( + [ + 'FIELD_NAME' => sprintf('%s%s', substr((string)random_int(0, PHP_INT_MAX), 0, 3), time()), + 'EDIT_FORM_LABEL' => [ + 'ru' => 'тест uf тип string', + 'en' => 'test uf type string', + ], + 'LIST_COLUMN_LABEL' => [ + 'ru' => 'тест uf тип string', + 'en' => 'test uf type string', + ], + 'USER_TYPE_ID' => 'string', + 'XML_ID' => 'b24phpsdk_type_string', + 'SETTINGS' => [ + 'DEFAULT_VALUE' => 'hello world', + ], + ] + )->getId(); + } + + public function tearDown(): void + { + $this->contactUserfieldService->delete($this->contactUserfieldId); + } +} \ No newline at end of file From 88949f58e49034e554e802205049f93be9e29442 Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 28 Nov 2021 21:31:41 +0300 Subject: [PATCH 5/7] first deal userfields build Signed-off-by: mesilov --- CHANGELOG.md | 6 +- src/Services/CRM/CRMServiceBuilder.php | 15 ++ .../CRM/Deal/Result/DealItemResult.php | 10 + .../Deal/Result/DealUserfieldItemResult.php | 12 + .../CRM/Deal/Result/DealUserfieldResult.php | 19 ++ .../CRM/Deal/Result/DealUserfieldsResult.php | 26 +++ src/Services/CRM/Deal/Service/Deal.php | 2 +- .../CRM/Deal/Service/DealUserfield.php | 205 ++++++++++++++++++ .../CRM/Deal/Service/DealUserfieldTest.php | 142 ++++++++++++ .../Deal/Service/DealUserfieldUseCaseTest.php | 92 ++++++++ 10 files changed, 527 insertions(+), 2 deletions(-) create mode 100644 src/Services/CRM/Deal/Result/DealUserfieldItemResult.php create mode 100644 src/Services/CRM/Deal/Result/DealUserfieldResult.php create mode 100644 src/Services/CRM/Deal/Result/DealUserfieldsResult.php create mode 100644 src/Services/CRM/Deal/Service/DealUserfield.php create mode 100644 tests/Integration/Services/CRM/Deal/Service/DealUserfieldTest.php create mode 100644 tests/Integration/Services/CRM/Deal/Service/DealUserfieldUseCaseTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index cfe372e0..a7836731 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,12 @@ * add method `countByFilter` for all related services, see issue [Добавить для всех сущностей метод подсчёта количества элементов по фильтру #228](https://github.com/mesilov/bitrix24-php-sdk/issues/228) * add in scope «CRM» Userfield service and integration test -* add in scope «CRM» ContactUserfield service and integration test +* add in scope «CRM» ContactUserfield service and integration test, see + issue [Добавить для всех сущностей метод подсчёта количества элементов по фильтру #231](https://github.com/mesilov/bitrix24-php-sdk/issues/231) * add method getUserfieldByFieldName for `ContactItemResult` +* add in scope «CRM» DealUserfield service and integration test, see + issue [Добавить для всех сущностей метод подсчёта количества элементов по фильтру #232](https://github.com/mesilov/bitrix24-php-sdk/issues/232) +* add method getUserfieldByFieldName for `DealItemResult` * add exception `UserfieldNotFoundException` ### Removed diff --git a/src/Services/CRM/CRMServiceBuilder.php b/src/Services/CRM/CRMServiceBuilder.php index 672fabf9..82b5040e 100644 --- a/src/Services/CRM/CRMServiceBuilder.php +++ b/src/Services/CRM/CRMServiceBuilder.php @@ -70,6 +70,21 @@ public function deal(): Deal\Service\Deal return $this->serviceCache[__METHOD__]; } + /** + * @return \Bitrix24\SDK\Services\CRM\Deal\Service\DealUserfield + */ + public function dealUserfield(): Deal\Service\DealUserfield + { + if (!isset($this->serviceCache[__METHOD__])) { + $this->serviceCache[__METHOD__] = new Deal\Service\DealUserfield( + $this->core, + $this->log + ); + } + + return $this->serviceCache[__METHOD__]; + } + /** * @return Contact\Service\Contact */ diff --git a/src/Services/CRM/Deal/Result/DealItemResult.php b/src/Services/CRM/Deal/Result/DealItemResult.php index e0341648..0737bca9 100644 --- a/src/Services/CRM/Deal/Result/DealItemResult.php +++ b/src/Services/CRM/Deal/Result/DealItemResult.php @@ -47,4 +47,14 @@ */ class DealItemResult extends AbstractCrmItem { + /** + * @param string $userfieldName + * + * @return mixed|null + * @throws \Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException + */ + public function getUserfieldByFieldName(string $userfieldName) + { + return $this->getKeyWithUserfieldByFieldName($userfieldName); + } } \ No newline at end of file diff --git a/src/Services/CRM/Deal/Result/DealUserfieldItemResult.php b/src/Services/CRM/Deal/Result/DealUserfieldItemResult.php new file mode 100644 index 00000000..7531eae9 --- /dev/null +++ b/src/Services/CRM/Deal/Result/DealUserfieldItemResult.php @@ -0,0 +1,12 @@ +getCoreResponse()->getResponseData()->getResult()->getResultData()); + } +} \ No newline at end of file diff --git a/src/Services/CRM/Deal/Result/DealUserfieldsResult.php b/src/Services/CRM/Deal/Result/DealUserfieldsResult.php new file mode 100644 index 00000000..af523d1a --- /dev/null +++ b/src/Services/CRM/Deal/Result/DealUserfieldsResult.php @@ -0,0 +1,26 @@ +getCoreResponse()->getResponseData()->getResult()->getResultData() as $item) { + $res[] = new DealUserfieldItemResult($item); + } + + return $res; + } +} \ No newline at end of file diff --git a/src/Services/CRM/Deal/Service/Deal.php b/src/Services/CRM/Deal/Service/Deal.php index fac6432e..5f37aa57 100644 --- a/src/Services/CRM/Deal/Service/Deal.php +++ b/src/Services/CRM/Deal/Service/Deal.php @@ -236,7 +236,7 @@ public function list(array $order, array $filter, array $select, int $startItem * @throws BaseException * @throws TransportException */ - public function update(int $id, array $fields, array $params): UpdatedItemResult + public function update(int $id, array $fields, array $params = []): UpdatedItemResult { return new UpdatedItemResult( $this->core->call( diff --git a/src/Services/CRM/Deal/Service/DealUserfield.php b/src/Services/CRM/Deal/Service/DealUserfield.php new file mode 100644 index 00000000..e250db76 --- /dev/null +++ b/src/Services/CRM/Deal/Service/DealUserfield.php @@ -0,0 +1,205 @@ +core->call( + 'crm.deal.userfield.list', + [ + 'order' => $order, + 'filter' => $filter, + ] + ) + ); + } + + /** + * Created new user field for deals. + * + * System limitation for field name - 20 characters. + * Prefix UF_CRM_is always added to the user field name. + * As a result, the actual name length - 13 characters. + * + * @param array{ + * FIELD_NAME?: string, + * USER_TYPE_ID?: string, + * XML_ID?: string, + * SORT?: string, + * MULTIPLE?: string, + * MANDATORY?: string, + * SHOW_FILTER?: string, + * SHOW_IN_LIST?: string, + * EDIT_IN_LIST?: string, + * IS_SEARCHABLE?: string, + * EDIT_FORM_LABEL?: string, + * LIST_COLUMN_LABEL?: string, + * LIST_FILTER_LABEL?: string, + * ERROR_MESSAGE?: string, + * HELP_MESSAGE?: string, + * LIST?: string, + * SETTINGS?: string, + * } $userfieldItemFields + * + * @return \Bitrix24\SDK\Core\Result\AddedItemResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @throws UserfieldNameIsTooLongException + * @link https://training.bitrix24.com/rest_help/crm/deals/crm_deal_userfield_add.php + * + */ + public function add(array $userfieldItemFields): AddedItemResult + { + if (strlen($userfieldItemFields['FIELD_NAME']) > 13) { + throw new UserfieldNameIsTooLongException( + sprintf( + 'userfield name %s is too long %s, maximum length - 13 characters', + $userfieldItemFields['FIELD_NAME'], + strlen($userfieldItemFields['FIELD_NAME']) + ) + ); + } + + return new AddedItemResult( + $this->core->call( + 'crm.deal.userfield.add', + [ + 'fields' => $userfieldItemFields, + ] + ) + ); + } + + /** + * Deleted userfield for deals + * + * @param int $userfieldId + * + * @return \Bitrix24\SDK\Core\Result\DeletedItemResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @link https://training.bitrix24.com/rest_help/crm/deals/crm_deal_userfield_delete.php + * + */ + public function delete(int $userfieldId): DeletedItemResult + { + return new DeletedItemResult( + $this->core->call( + 'crm.deal.userfield.delete', + [ + 'id' => $userfieldId, + ] + ) + ); + } + + /** + * Returns a userfield for deal by ID. + * + * @param int $userfieldItemId + * + * @return \Bitrix24\SDK\Services\CRM\Deal\Result\DealUserfieldResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @link https://training.bitrix24.com/rest_help/crm/deals/crm_deal_userfield_get.php + */ + public function get(int $userfieldItemId): DealUserfieldResult + { + return new DealUserfieldResult( + $this->core->call( + 'crm.deal.userfield.get', + [ + 'id' => $userfieldItemId, + ] + ) + ); + } + + /** + * Updates an existing user field for deals. + * + * @param int $userfieldItemId + * @param array $userfieldFieldsToUpdate + * + * @return \Bitrix24\SDK\Core\Result\UpdatedItemResult + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @link https://training.bitrix24.com/rest_help/crm/deals/crm_deal_userfield_update.php + */ + public function update(int $userfieldItemId, array $userfieldFieldsToUpdate): UpdatedItemResult + { + return new UpdatedItemResult( + $this->core->call( + 'crm.deal.userfield.update', + [ + 'id' => $userfieldItemId, + 'fields' => $userfieldFieldsToUpdate, + ] + ) + ); + } +} \ No newline at end of file diff --git a/tests/Integration/Services/CRM/Deal/Service/DealUserfieldTest.php b/tests/Integration/Services/CRM/Deal/Service/DealUserfieldTest.php new file mode 100644 index 00000000..3055e8bc --- /dev/null +++ b/tests/Integration/Services/CRM/Deal/Service/DealUserfieldTest.php @@ -0,0 +1,142 @@ + [ + [ + 'FIELD_NAME' => sprintf('%s%s', substr((string)random_int(0, PHP_INT_MAX), 0, 3), time()), + 'EDIT_FORM_LABEL' => [ + 'ru' => 'тест uf тип string', + 'en' => 'test uf type string', + ], + 'LIST_COLUMN_LABEL' => [ + 'ru' => 'тест uf тип string', + 'en' => 'test uf type string', + ], + 'USER_TYPE_ID' => 'string', + 'XML_ID' => 'b24phpsdk_type_string', + 'SETTINGS' => [], + ], + ]; + + mt_srand(); + yield 'user type id integer' => [ + [ + 'FIELD_NAME' => sprintf('%s%s', substr((string)random_int(0, PHP_INT_MAX), 0, 3), time()), + 'EDIT_FORM_LABEL' => [ + 'ru' => 'тест uf тип integer', + 'en' => 'test uf type integer', + ], + 'LIST_COLUMN_LABEL' => [ + 'ru' => 'тест uf тип integer', + 'en' => 'test uf type integer', + ], + 'USER_TYPE_ID' => 'integer', + 'XML_ID' => 'b24phpsdk_type_integer', + 'SETTINGS' => [], + ], + ]; + } + + /** + * @param array $newUserFieldItem + * + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @throws \Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNameIsTooLongException + * @covers ContactUserfield::add + * @dataProvider systemUserfieldsDemoDataDataProvider + */ + public function testAdd(array $newUserFieldItem): void + { + self::assertGreaterThanOrEqual(1, $this->userfieldService->add($newUserFieldItem)->getId()); + } + + /** + * @param array $newUserFieldItem + * + * @dataProvider systemUserfieldsDemoDataDataProvider + * @covers ContactUserfield::delete + */ + public function testDelete(array $newUserFieldItem): void + { + $newUserfieldId = $this->userfieldService->add($newUserFieldItem)->getId(); + $this->assertTrue($this->userfieldService->delete($newUserfieldId)->isSuccess()); + } + + /** + * @param array $newUserFieldItem + * + * @dataProvider systemUserfieldsDemoDataDataProvider + * @covers ContactUserfield::get + */ + public function testGet(array $newUserFieldItem): void + { + $newUserfieldId = $this->userfieldService->add($newUserFieldItem)->getId(); + $ufField = $this->userfieldService->get($newUserfieldId)->userfieldItem(); + $this->assertEquals($newUserfieldId, $ufField->ID); + $this->assertEquals($newUserFieldItem['USER_TYPE_ID'], $ufField->USER_TYPE_ID); + $this->assertEquals('UF_CRM_' . $newUserFieldItem['FIELD_NAME'], $ufField->FIELD_NAME); + $this->assertEquals($newUserFieldItem['XML_ID'], $ufField->XML_ID); + } + + /** + * @param array $newUserFieldItem + * + * @dataProvider systemUserfieldsDemoDataDataProvider + * @covers ContactUserfield::update + */ + public function testUpdate(array $newUserFieldItem): void + { + $newUserfieldId = $this->userfieldService->add($newUserFieldItem)->getId(); + $ufFieldBefore = $this->userfieldService->get($newUserfieldId)->userfieldItem(); + $this->assertEquals($newUserfieldId, $ufFieldBefore->ID); + $this->assertEquals($newUserFieldItem['USER_TYPE_ID'], $ufFieldBefore->USER_TYPE_ID); + $this->assertEquals('UF_CRM_' . $newUserFieldItem['FIELD_NAME'], $ufFieldBefore->FIELD_NAME); + $this->assertEquals($newUserFieldItem['XML_ID'], $ufFieldBefore->XML_ID); + + $this->assertTrue( + $this->userfieldService->update( + $newUserfieldId, + [ + 'EDIT_FORM_LABEL' => $newUserFieldItem['EDIT_FORM_LABEL']['en'] . 'QQQ', + ] + )->isSuccess() + ); + + $ufFieldAfter = $this->userfieldService->get($newUserfieldId)->userfieldItem(); + $this->assertEquals($ufFieldBefore->EDIT_FORM_LABEL['en'] . 'QQQ', $ufFieldAfter->EDIT_FORM_LABEL['en']); + } + + /** + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @covers \Bitrix24\SDK\Services\CRM\Contact\Service\ContactUserfield::list + */ + public function testList(): void + { + $ufFields = $this->userfieldService->list([], []); + $this->assertGreaterThanOrEqual(0, count($ufFields->getUserfields())); + } + + public function setUp(): void + { + $this->userfieldService = Fabric::getServiceBuilder()->getCRMScope()->dealUserfield(); + } +} \ No newline at end of file diff --git a/tests/Integration/Services/CRM/Deal/Service/DealUserfieldUseCaseTest.php b/tests/Integration/Services/CRM/Deal/Service/DealUserfieldUseCaseTest.php new file mode 100644 index 00000000..88f9d1a1 --- /dev/null +++ b/tests/Integration/Services/CRM/Deal/Service/DealUserfieldUseCaseTest.php @@ -0,0 +1,92 @@ +dealUserfieldService->get($this->dealUserfieldId)->userfieldItem(); + $ufOriginalFieldName = $ufMetadata->getOriginalFieldName(); + $ufFieldName = $ufMetadata->FIELD_NAME; + + // add deal with uf value + $fieldNameValue = 'test field value'; + $newDealId = $this->dealService->add( + [ + 'TITLE' => 'test deal', + $ufFieldName => $fieldNameValue, + ] + )->getId(); + $deal = $this->dealService->get($newDealId)->deal(); + $this->assertEquals($fieldNameValue, $deal->getUserfieldByFieldName($ufOriginalFieldName)); + + // update deal userfield value + $newUfValue = 'test 2'; + $this->assertTrue( + $this->dealService->update( + $deal->ID, + [ + $ufFieldName => $newUfValue, + ] + )->isSuccess() + ); + $updatedDeal = $this->dealService->get($deal->ID)->deal(); + $this->assertEquals($newUfValue, $updatedDeal->getUserfieldByFieldName($ufOriginalFieldName)); + } + + /** + * @throws \Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNameIsTooLongException + * @throws \Bitrix24\SDK\Core\Exceptions\TransportException + * @throws \Bitrix24\SDK\Core\Exceptions\InvalidArgumentException + * @throws \Bitrix24\SDK\Core\Exceptions\BaseException + */ + public function setUp(): void + { + $this->dealService = Fabric::getServiceBuilder()->getCRMScope()->deal(); + $this->dealUserfieldService = Fabric::getServiceBuilder()->getCRMScope()->dealUserfield(); + + $this->dealUserfieldId = $this->dealUserfieldService->add( + [ + 'FIELD_NAME' => sprintf('%s%s', substr((string)random_int(0, PHP_INT_MAX), 0, 3), time()), + 'EDIT_FORM_LABEL' => [ + 'ru' => 'тест uf тип string', + 'en' => 'test uf type string', + ], + 'LIST_COLUMN_LABEL' => [ + 'ru' => 'тест uf тип string', + 'en' => 'test uf type string', + ], + 'USER_TYPE_ID' => 'string', + 'XML_ID' => 'b24phpsdk_type_string', + 'SETTINGS' => [ + 'DEFAULT_VALUE' => 'hello world', + ], + ] + )->getId(); + } + + public function tearDown(): void + { + $this->dealUserfieldService->delete($this->dealUserfieldId); + } +} \ No newline at end of file From ea1ea619ab0fe8569e5dded143a81aa0261d7311 Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 28 Nov 2021 22:53:23 +0300 Subject: [PATCH 6/7] add types for deal Signed-off-by: mesilov --- .../CRM/Common/Result/AbstractCrmItem.php | 30 +++++++- .../CRM/Deal/Result/DealItemResult.php | 71 ++++++++++--------- 2 files changed, 64 insertions(+), 37 deletions(-) diff --git a/src/Services/CRM/Common/Result/AbstractCrmItem.php b/src/Services/CRM/Common/Result/AbstractCrmItem.php index e238ed90..0737315f 100644 --- a/src/Services/CRM/Common/Result/AbstractCrmItem.php +++ b/src/Services/CRM/Common/Result/AbstractCrmItem.php @@ -19,22 +19,48 @@ class AbstractCrmItem extends AbstractItem */ public function __get($offset) { - //todo унести в отдельный класс и покрыть тестами + // todo унести в отдельный класс и покрыть тестами + // приведение полей к реальным типам данных для основных сущностей CRM switch ($offset) { case 'ID': case 'ASSIGNED_BY_ID': case 'CREATED_BY_ID': case 'MODIFY_BY_ID': - return (int)$this->data[$offset]; + // deal + case 'LEAD_ID': + case 'CONTACT_ID': + case 'QUOTE_ID': + if ($this->data[$offset] !== '' && $this->data[$offset] !== null) { + return (int)$this->data[$offset]; + } + + return null; + case 'COMPANY_ID': + if ($this->data[$offset] !== '' && $this->data[$offset] !== null && $this->data[$offset] !== '0') { + return (int)$this->data[$offset]; + } + + return null; + + // contact case 'EXPORT': case 'HAS_PHONE': case 'HAS_EMAIL': case 'HAS_IMOL': case 'OPENED': + // deal + case 'IS_MANUAL_OPPORTUNITY': + case 'CLOSED': + case 'IS_NEW': + case 'IS_RECURRING': + case 'IS_RETURN_CUSTOMER': + case 'IS_REPEATED_APPROACH': return $this->data[$offset] === 'Y'; case 'DATE_CREATE': case 'DATE_MODIFY': case 'BIRTHDATE': + case 'BEGINDATE': + case 'CLOSEDATE': if ($this->data[$offset] !== '') { return DateTimeImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]); } diff --git a/src/Services/CRM/Deal/Result/DealItemResult.php b/src/Services/CRM/Deal/Result/DealItemResult.php index 0737bca9..a77faa40 100644 --- a/src/Services/CRM/Deal/Result/DealItemResult.php +++ b/src/Services/CRM/Deal/Result/DealItemResult.php @@ -5,45 +5,46 @@ namespace Bitrix24\SDK\Services\CRM\Deal\Result; use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem; +use DateTimeInterface; /** * Class DealItemResult * - * @property-read int $ID - * @property-read string $TITLE deal title - * @property-read string|null $TYPE_ID - * @property-read string|null $CATEGORY_ID - * @property-read string $STAGE_ID - * @property-read string $STAGE_SEMANTIC_ID - * @property-read string $IS_NEW - * @property-read string $IS_RECURRING - * @property-read string|null $PROBABILITY - * @property-read string $CURRENCY_ID - * @property-read string $OPPORTUNITY - * @property-read string $IS_MANUAL_OPPORTUNITY - * @property-read string $TAX_VALUE - * @property-read string $LEAD_ID - * @property-read string $COMPANY_ID - * @property-read string $CONTACT_ID - * @property-read string $QUOTE_ID - * @property-read string $BEGINDATE - * @property-read string $CLOSEDATE - * @property-read string $OPENED - * @property-read string $CLOSED - * @property-read string|null $COMMENTS - * @property-read string|null $ADDITIONAL_INFO - * @property-read string|null $LOCATION_ID - * @property-read string $IS_RETURN_CUSTOMER - * @property-read string $IS_REPEATED_APPROACH - * @property-read int|null $SOURCE_ID - * @property-read string|null $SOURCE_DESCRIPTION - * @property-read string|null $ORIGINATOR_ID - * @property-read string|null $ORIGIN_ID - * @property-read string|null $UTM_SOURCE - * @property-read string|null $UTM_MEDIUM - * @property-read string|null $UTM_CAMPAIGN - * @property-read string|null $UTM_CONTENT - * @property-read string|null $UTM_TERM + * @property-read int $ID + * @property-read string $TITLE deal title + * @property-read string|null $TYPE_ID + * @property-read string|null $CATEGORY_ID + * @property-read string $STAGE_ID + * @property-read string $STAGE_SEMANTIC_ID + * @property-read bool $IS_NEW + * @property-read bool $IS_RECURRING + * @property-read string|null $PROBABILITY + * @property-read string $CURRENCY_ID + * @property-read string $OPPORTUNITY + * @property-read bool $IS_MANUAL_OPPORTUNITY + * @property-read string $TAX_VALUE + * @property-read int $LEAD_ID + * @property-read int $COMPANY_ID + * @property-read int $CONTACT_ID + * @property-read int $QUOTE_ID + * @property-read DateTimeInterface $BEGINDATE + * @property-read DateTimeInterface $CLOSEDATE + * @property-read bool $OPENED + * @property-read bool $CLOSED + * @property-read string|null $COMMENTS + * @property-read string|null $ADDITIONAL_INFO + * @property-read string|null $LOCATION_ID + * @property-read bool $IS_RETURN_CUSTOMER + * @property-read bool $IS_REPEATED_APPROACH + * @property-read int|null $SOURCE_ID + * @property-read string|null $SOURCE_DESCRIPTION + * @property-read string|null $ORIGINATOR_ID + * @property-read string|null $ORIGIN_ID + * @property-read string|null $UTM_SOURCE + * @property-read string|null $UTM_MEDIUM + * @property-read string|null $UTM_CAMPAIGN + * @property-read string|null $UTM_CONTENT + * @property-read string|null $UTM_TERM */ class DealItemResult extends AbstractCrmItem { From cf40ef9f2c89daff37e88076c04db76da37d4b97 Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 28 Nov 2021 22:55:19 +0300 Subject: [PATCH 7/7] update changelog Signed-off-by: mesilov --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7836731..8a0e0f6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,10 @@ issue [Добавить для всех сущностей метод подсчёта количества элементов по фильтру #228](https://github.com/mesilov/bitrix24-php-sdk/issues/228) * add in scope «CRM» Userfield service and integration test * add in scope «CRM» ContactUserfield service and integration test, see - issue [Добавить для всех сущностей метод подсчёта количества элементов по фильтру #231](https://github.com/mesilov/bitrix24-php-sdk/issues/231) + issue [Добавить сервис по работе с юзерфилдами контакта #231](https://github.com/mesilov/bitrix24-php-sdk/issues/231) * add method getUserfieldByFieldName for `ContactItemResult` * add in scope «CRM» DealUserfield service and integration test, see - issue [Добавить для всех сущностей метод подсчёта количества элементов по фильтру #232](https://github.com/mesilov/bitrix24-php-sdk/issues/232) + issue [Добавить сервис по работе с юзерфилдами cделки #232](https://github.com/mesilov/bitrix24-php-sdk/issues/232) * add method getUserfieldByFieldName for `DealItemResult` * add exception `UserfieldNotFoundException`