From 9bc932d54f215824ffaca92c191954c9d0399026 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 2 May 2023 12:12:51 +0300 Subject: [PATCH] user is busy function --- .../ExtGroup/CalendarGroup/UserIsBusyType.php | 97 +++++++++++++++++++ .../Crm/Resources/metadata/app/formula.json | 20 +++- .../Crm/Tools/Calendar/Items/Event.php | 8 ++ .../Modules/Crm/Tools/Calendar/Service.php | 6 +- .../Espo/Core/Formula/FormulaTest.php | 57 +++++++++++ 5 files changed, 182 insertions(+), 6 deletions(-) create mode 100644 application/Espo/Modules/Crm/Classes/FormulaFunctions/ExtGroup/CalendarGroup/UserIsBusyType.php diff --git a/application/Espo/Modules/Crm/Classes/FormulaFunctions/ExtGroup/CalendarGroup/UserIsBusyType.php b/application/Espo/Modules/Crm/Classes/FormulaFunctions/ExtGroup/CalendarGroup/UserIsBusyType.php new file mode 100644 index 0000000000..7ea3c30557 --- /dev/null +++ b/application/Espo/Modules/Crm/Classes/FormulaFunctions/ExtGroup/CalendarGroup/UserIsBusyType.php @@ -0,0 +1,97 @@ +withSkipAcl(); + + $ignoreList = []; + + if ($entityType && $id) { + $ignoreList[] = (new Event(null, null, $entityType, []))->withId($id); + } + + try { + $ranges = $this->service->fetchBusyRanges($userId, $params, $ignoreList); + } + catch (Exception $e) { + throw new RuntimeException($e->getMessage()); + } + + return $ranges !== []; + } +} diff --git a/application/Espo/Modules/Crm/Resources/metadata/app/formula.json b/application/Espo/Modules/Crm/Resources/metadata/app/formula.json index 79197ca21c..7a34e2726b 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/app/formula.json +++ b/application/Espo/Modules/Crm/Resources/metadata/app/formula.json @@ -1,5 +1,19 @@ { "functionClassNameMap": { - "ext\\account\\findByEmailAddress": "Espo\\Modules\\Crm\\Classes\\FormulaFunctions\\ExtGroup\\AccountGroup\\FindByEmailAddressType" - } -} \ No newline at end of file + "ext\\account\\findByEmailAddress": "Espo\\Modules\\Crm\\Classes\\FormulaFunctions\\ExtGroup\\AccountGroup\\FindByEmailAddressType", + "ext\\calendar\\userIsBusy": "Espo\\Modules\\Crm\\Classes\\FormulaFunctions\\ExtGroup\\CalendarGroup\\UserIsBusyType" + }, + "functionList": [ + "__APPEND__", + { + "name": "ext\\account\\findByEmailAddress", + "insertText": "ext\\account\\findByEmailAddress(EMAIL_ADDRESS)", + "returnType": "string" + }, + { + "name": "ext\\calendar\\userIsBusy", + "insertText": "ext\\calendar\\userIsBusy(USER_ID, FROM, TO)", + "returnType": "bool" + } + ] +} diff --git a/application/Espo/Modules/Crm/Tools/Calendar/Items/Event.php b/application/Espo/Modules/Crm/Tools/Calendar/Items/Event.php index 0ba092113a..1a459112eb 100644 --- a/application/Espo/Modules/Crm/Tools/Calendar/Items/Event.php +++ b/application/Espo/Modules/Crm/Tools/Calendar/Items/Event.php @@ -89,6 +89,14 @@ class Event implements Item return $obj; } + public function withId(string $id): self + { + $obj = clone $this; + $obj->attributes['id'] = $id; + + return $obj; + } + public function withUserIdAdded(string $userId): self { $obj = clone $this; diff --git a/application/Espo/Modules/Crm/Tools/Calendar/Service.php b/application/Espo/Modules/Crm/Tools/Calendar/Service.php index 45cbee5f11..ccbdad08dc 100644 --- a/application/Espo/Modules/Crm/Tools/Calendar/Service.php +++ b/application/Espo/Modules/Crm/Tools/Calendar/Service.php @@ -752,7 +752,7 @@ class Service $ignoreHash = (object) []; foreach ($ignoreEventList as $event) { - $id = $event->getAttribute('id'); + $id = $event->getId(); if ($id) { $ignoreHash->$id = true; @@ -767,7 +767,7 @@ class Service $start = $event->getStart(); $end = $event->getEnd(); $status = $event->getAttribute('status'); - $id = $event->getAttribute('id'); + $id = $event->getId(); if (!$start || !$end) { continue; @@ -782,7 +782,7 @@ class Service } try { - foreach ($rangeList as &$range) { + foreach ($rangeList as $range) { if ( $start->getTimestamp() < $range->start->getTimestamp() && $end->getTimestamp() > $range->end->getTimestamp() diff --git a/tests/integration/Espo/Core/Formula/FormulaTest.php b/tests/integration/Espo/Core/Formula/FormulaTest.php index b9aed2a65c..e4787224ee 100644 --- a/tests/integration/Espo/Core/Formula/FormulaTest.php +++ b/tests/integration/Espo/Core/Formula/FormulaTest.php @@ -29,7 +29,10 @@ namespace tests\integration\Espo\Core\Formula; +use Espo\Core\Field\DateTime; +use Espo\Core\Field\DateTimeOptional; use Espo\Core\Formula\Manager; +use Espo\Entities\User; use Espo\Modules\Crm\Entities\Meeting; use Espo\ORM\EntityManager; @@ -771,4 +774,58 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase $id = $fm->run($script); $this->assertEquals($id, $user->getId()); } + + public function testCalendarUserBusy(): void + { + $fm = $this->getContainer()->getByClass(Manager::class); + $user = $this->getContainer()->getByClass(User::class); + $em = $this->getContainer()->getByClass(EntityManager::class); + + $dateStart = DateTimeOptional::createNow(); + $dateEnd = $dateStart->addHours(1); + + /** @var Meeting $meeting */ + $meeting = $em->getRDBRepositoryByClass(Meeting::class)->getNew(); + + $meeting + ->setDateStart($dateStart) + ->setDateEnd($dateEnd) + ->setAssignedUserId($user->getId()); + + $em->saveEntity($meeting); + + $script = sprintf( + "ext\\calendar\\userIsBusy('%s', '%s', '%s')", + $user->getId(), + $dateStart->getString(), + $dateEnd->getString() + ); + $this->assertTrue($fm->run($script)); + + $script = sprintf( + "ext\\calendar\\userIsBusy('%s', '%s', '%s')", + $user->getId(), + $dateStart->addHours(-1)->getString(), + $dateEnd->addHours(1)->getString() + ); + $this->assertTrue($fm->run($script)); + + $script = sprintf( + "ext\\calendar\\userIsBusy('%s', '%s', '%s')", + $user->getId(), + $dateStart->addDays(-1)->getString(), + $dateEnd->addDays(-1)->getString() + ); + $this->assertFalse($fm->run($script)); + + $script = sprintf( + "ext\\calendar\\userIsBusy('%s', '%s', '%s', '%s', '%s')", + $user->getId(), + $dateStart->getString(), + $dateEnd->getString(), + $meeting->getEntityType(), + $meeting->getId() + ); + $this->assertFalse($fm->run($script)); + } }