From 6e31f3f784d48c4118fe2190b6d8a80e83894dfd Mon Sep 17 00:00:00 2001 From: Nikita Date: Thu, 11 May 2023 20:52:00 +0500 Subject: [PATCH] calls fixes --- examples/calls.php | 3 +- src/AmoCRM/Models/CallModel.php | 13 ++- src/AmoCRM/Models/NoteType/CallNote.php | 36 ++----- src/AmoCRM/Models/Traits/CallTrait.php | 30 ++++++ src/AmoCRM/Models/Unsorted/SipMetadata.php | 116 +++------------------ 5 files changed, 67 insertions(+), 131 deletions(-) diff --git a/examples/calls.php b/examples/calls.php index 3b2f766c..d895c6b5 100644 --- a/examples/calls.php +++ b/examples/calls.php @@ -28,13 +28,14 @@ function (AccessTokenInterface $accessToken, string $baseDomain) { //Добавим входящий звонок $call = new CallModel(); $call - ->setPhone('+7912312321') + ->setPhone('+7912312321') // кто звонил ->setCallStatus(CallInterface::CALL_STATUS_SUCCESS_CONVERSATION) ->setCallResult('Разговор состоялся') ->setDuration(148) ->setUniq(Uuid::uuid4()) ->setSource('integration name') ->setDirection(CallInterface::CALL_DIRECTION_IN) + ->setCallResponsible('+79161234567') // кому звонили, можно id пользователя, или строку ->setLink('https://example.test/test.mp3'); try { diff --git a/src/AmoCRM/Models/CallModel.php b/src/AmoCRM/Models/CallModel.php index 73c37e4a..f1283da5 100644 --- a/src/AmoCRM/Models/CallModel.php +++ b/src/AmoCRM/Models/CallModel.php @@ -113,7 +113,9 @@ public function fromArray(array $call): CallModel $this->setDirection($call['direction']); } - + if (isset($call['call_responsible'])) { + $this->setCallResponsible($call['call_responsible']); + } return $model; } @@ -132,6 +134,7 @@ public function toArray(): array 'phone' => $this->getPhone(), 'call_result' => $this->getCallResult(), 'call_status' => $this->getCallStatus(), + 'call_responsible' => $this->getCallResponsible(), 'direction' => $this->getDirection(), 'entity_id' => $this->getEntityId(), 'entity_type' => $this->getEntityType(), @@ -166,10 +169,18 @@ public function toApi(?string $requestId = "0"): array 'request_id' => $this->getRequestId(), ]; + // Кто может работать с событием, например, удалять if ($responsibleUserId = $this->getResponsibleUserId()) { $call['responsible_user_id'] = $responsibleUserId; } + // Кто отображается + // Для входящего - кто получил звонок (для отображения в интерфейсе) + // Для исходящего - кто совершил звонок (для отображения в интерфейсе) + if ($callResponsible = $this->getCallResponsible()) { + $call['call_responsible'] = $callResponsible; + } + if ($createdBy = $this->getCreatedBy()) { $call['created_by'] = $createdBy; } diff --git a/src/AmoCRM/Models/NoteType/CallNote.php b/src/AmoCRM/Models/NoteType/CallNote.php index c146135d..ae1ed390 100644 --- a/src/AmoCRM/Models/NoteType/CallNote.php +++ b/src/AmoCRM/Models/NoteType/CallNote.php @@ -11,35 +11,6 @@ abstract class CallNote extends NoteModel { use CallTrait; - /** @deprecated */ - public const CALL_STATUS_LEAVE_MESSAGE = 1; - /** @deprecated */ - public const CALL_STATUS_SUCCESS_RECALL = 2; - /** @deprecated */ - public const CALL_STATUS_SUCCESS_NOT_IN_STOCK = 3; - /** @deprecated */ - public const CALL_STATUS_SUCCESS_CONVERSATION = 4; - /** @deprecated */ - public const CALL_STATUS_FAIL_WRONG_NUMBER = 5; - /** @deprecated */ - public const CALL_STATUS_FAIL_NOT_PHONED = 6; - /** @deprecated */ - public const CALL_STATUS_FAIL_BUSY = 7; - /** @deprecated */ - public const CALL_STATUS_UNDEFINED = 8; - - /** @deprecated */ - protected const AVAILABLE_CALL_STATUSES = [ - self::CALL_STATUS_LEAVE_MESSAGE, - self::CALL_STATUS_SUCCESS_RECALL, - self::CALL_STATUS_SUCCESS_NOT_IN_STOCK, - self::CALL_STATUS_SUCCESS_CONVERSATION, - self::CALL_STATUS_FAIL_WRONG_NUMBER, - self::CALL_STATUS_FAIL_NOT_PHONED, - self::CALL_STATUS_FAIL_BUSY, - self::CALL_STATUS_UNDEFINED, - ]; - /** * @param array $note * @@ -47,6 +18,7 @@ abstract class CallNote extends NoteModel */ public function fromArray(array $note): NoteModel { + /** @var CallNote $model */ $model = parent::fromArray($note); if (isset($note['params']['uniq']) && is_string($note['params']['uniq'])) { @@ -77,6 +49,10 @@ public function fromArray(array $note): NoteModel $model->setCallStatus($note['params']['call_status']); } + if (isset($note['params']['call_responsible'])) { + $model->setCallResponsible($note['params']['call_responsible']); + } + return $model; } @@ -95,6 +71,7 @@ public function toArray(): array 'phone' => $this->getPhone(), 'call_result' => $this->getCallResult(), 'call_status' => $this->getCallStatus(), + 'call_responsible' => $this->getCallResponsible(), ]; return $result; @@ -116,6 +93,7 @@ public function toApi(?string $requestId = "0"): array 'phone' => $this->getPhone(), 'call_result' => $this->getCallResult(), 'call_status' => $this->getCallStatus(), + 'call_responsible' => $this->getCallResponsible(), ]; return $result; diff --git a/src/AmoCRM/Models/Traits/CallTrait.php b/src/AmoCRM/Models/Traits/CallTrait.php index c0d99339..e543e4ca 100644 --- a/src/AmoCRM/Models/Traits/CallTrait.php +++ b/src/AmoCRM/Models/Traits/CallTrait.php @@ -41,6 +41,16 @@ trait CallTrait */ protected $callStatus; + /** + * Для входящего - кто получил звонок (для отображения в интерфейсе) + * Для исходящего - кто совершил звонок (для отображения в интерфейсе) + * + * Можно передать id пользователя или имя (будет зафиксировано) + * + * @var string|int|null + */ + protected $callResponsible; + /** * @return string|null */ @@ -177,4 +187,24 @@ public function setCallStatus(?int $callStatus): self return $this; } + + /** + * @return int|string|null + */ + public function getCallResponsible() + { + return $this->callResponsible; + } + + /** + * @param int|string|null $callResponsible + * + * @return self + */ + public function setCallResponsible($callResponsible): self + { + $this->callResponsible = $callResponsible; + + return $this; + } } diff --git a/src/AmoCRM/Models/Unsorted/SipMetadata.php b/src/AmoCRM/Models/Unsorted/SipMetadata.php index 59ed96d0..bece915b 100644 --- a/src/AmoCRM/Models/Unsorted/SipMetadata.php +++ b/src/AmoCRM/Models/Unsorted/SipMetadata.php @@ -2,49 +2,31 @@ namespace AmoCRM\Models\Unsorted; +use AmoCRM\Models\Traits\CallTrait; use AmoCRM\Models\Unsorted\Interfaces\UnsortedMetadataInterface; use AmoCRM\Models\BaseApiModel; use AmoCRM\Contracts\Support\Arrayable; -use function is_scalar; - class SipMetadata extends BaseApiModel implements Arrayable, UnsortedMetadataInterface { + use CallTrait; + /** + * @deprecated * @var int|string|null */ protected $from; - /** - * @var string|null - */ - protected $phone; - /** * @var int|null */ protected $calledAt; - /** - * @var int|null - */ - protected $duration; - - /** - * @var string|null - */ - protected $link; - /** * @var string|null */ protected $serviceCode; - /** - * @var string|null - */ - protected $uniq; - /** * @var bool|null */ @@ -66,6 +48,7 @@ public static function fromArray(array $metadata): self $model->setLink($metadata['link'] ?? null); $model->setServiceCode($metadata['service_code'] ?? null); $model->setUniq($metadata['uniq'] ?? null); + $model->setCallResponsible($metadata['call_responsible'] ?? null); return $model; } @@ -83,10 +66,12 @@ public function toArray(): array 'link' => $this->getLink(), 'service_code' => $this->getServiceCode(), 'uniq' => $this->getUniq(), + 'call_responsible' => $this->getCallResponsible(), ]; } /** + * @deprecated * @return int|string|null */ public function getFrom() @@ -95,6 +80,7 @@ public function getFrom() } /** + * @deprecated * @param int|null|string $from * @return SipMetadata */ @@ -105,25 +91,6 @@ public function setFrom($from): SipMetadata return $this; } - /** - * @return string|null - */ - public function getPhone(): ?string - { - return $this->phone; - } - - /** - * @param string|null $phone - * @return SipMetadata - */ - public function setPhone(?string $phone): SipMetadata - { - $this->phone = $phone; - - return $this; - } - /** * @return int|null */ @@ -143,44 +110,6 @@ public function setCalledAt(?int $calledAt): SipMetadata return $this; } - /** - * @return int|null - */ - public function getDuration(): ?int - { - return $this->duration; - } - - /** - * @param int|null $duration - * @return SipMetadata - */ - public function setDuration(?int $duration): SipMetadata - { - $this->duration = $duration; - - return $this; - } - - /** - * @return string|null - */ - public function getLink(): ?string - { - return $this->link; - } - - /** - * @param string|null $link - * @return SipMetadata - */ - public function setLink(?string $link): SipMetadata - { - $this->link = $link; - - return $this; - } - /** * @return string|null */ @@ -200,25 +129,6 @@ public function setServiceCode(?string $serviceCode): SipMetadata return $this; } - /** - * @return string|null - */ - public function getUniq(): ?string - { - return $this->uniq; - } - - /** - * @param string|null $uniq - * @return SipMetadata - */ - public function setUniq(?string $uniq): SipMetadata - { - $this->uniq = $uniq; - - return $this; - } - /** * @return bool|null */ @@ -245,8 +155,7 @@ public function setIsCallEventNeeded(?bool $isCallEventNeeded): SipMetadata */ public function toApi(?string $requestId = "0"): array { - return [ - 'from' => $this->getFrom(), + $result = [ 'phone' => $this->getPhone(), 'called_at' => $this->getCalledAt() ?? time(), 'duration' => $this->getDuration(), @@ -254,7 +163,14 @@ public function toApi(?string $requestId = "0"): array 'service_code' => $this->getServiceCode(), 'uniq' => $this->getUniq(), 'is_call_event_needed' => $this->getIsCallEventNeeded() ?? true, + 'call_responsible' => $this->getCallResponsible(), ]; + + if (!empty($this->getFrom())) { + $result['from'] = $this->getFrom(); + } + + return $result; } /**