1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
StartChattingOneDay.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Collab\Onboarding\Entity\Type;
6
7use Bitrix\Main\Error;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Main\Type\DateTime;
10use Bitrix\Socialnetwork\Collab\Onboarding\Entity\AbstractJob;
11use Bitrix\Socialnetwork\Collab\Onboarding\Integration\Im\ChatNotification;
12use Bitrix\Socialnetwork\Collab\Onboarding\Integration\Im\ChatService;
13use Bitrix\Socialnetwork\Collab\Onboarding\Internals\Type;
14use Bitrix\Socialnetwork\Collab\Onboarding\Execution\Result\JobResult;
15use Bitrix\Socialnetwork\Collab\Onboarding\Notification\NotificationService;
16use Bitrix\Socialnetwork\Collab\Provider\CollabProvider;
17use Bitrix\Socialnetwork\Integration\Calendar\ClosestWorkDate\ClosestWorkDate;
18use CTimeZone;
19
21{
22 public const JOB_OFFSET = self::SECONDS_PER_DAY;
23
24 public function getType(): Type
25 {
26 return Type::StartChattingOneDay;
27 }
28
29 public function isImmediatelyExecuted(): bool
30 {
31 return true;
32 }
33
34 protected function createNextExecutionDate(): DateTime
35 {
36 $closestWorkDate = (new ClosestWorkDate())->get(self::JOB_OFFSET);
37
38 $userTimeOffset = CTimeZone::GetOffset($this->userId);
39 $closestWorkDate->add("-{$userTimeOffset} seconds");
40
41 return $closestWorkDate;
42 }
43
44 public function __invoke(): JobResult
45 {
46 $jobResult = new JobResult();
47
48 if ($this->createdDate === null)
49 {
50 $jobResult->addError(new Error('The created date cannot be null'));
51
52 return $jobResult;
53 }
54
55 if (!$this->isStartChatting())
56 {
57 $this->sendNotification();
58 }
59
60 return $jobResult;
61 }
62
63 private function isStartChatting(): bool
64 {
65 $collab = CollabProvider::getInstance()->getCollab($this->collabId);
66 if ($collab === null)
67 {
68 return false;
69 }
70
71 return ChatService::getInstance()->isExistUserMessageByPeriod(
72 $collab->getChatId(),
73 $this->createdDate,
74 $this->nextExecution,
75 );
76 }
77
78 private function sendNotification(): void
79 {
80 $notificationService = new NotificationService([
81 ChatNotification::getInstance(),
82 ]);
83
84 $notificationService->send(
85 Loc::getMessage('SOCIALNETWORK_COLLAB_ONBOARDING_START_CHATTING'),
86 $this->userId,
87 $this->collabId
88 );
89 }
90}
Определения error.php:15