user is busy function

This commit is contained in:
Yuri Kuznetsov
2023-05-02 12:12:51 +03:00
parent d57d5d1d4a
commit 9bc932d54f
5 changed files with 182 additions and 6 deletions

View File

@@ -0,0 +1,97 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Classes\FormulaFunctions\ExtGroup\CalendarGroup;
use Espo\Core\Field\DateTime;
use Espo\Core\Formula\EvaluatedArgumentList;
use Espo\Core\Formula\Exceptions\BadArgumentType;
use Espo\Core\Formula\Exceptions\TooFewArguments;
use Espo\Core\Formula\Func;
use Espo\Modules\Crm\Tools\Calendar\FetchParams;
use Espo\Modules\Crm\Tools\Calendar\Items\Event;
use Espo\Modules\Crm\Tools\Calendar\Service;
use Exception;
use RuntimeException;
class UserIsBusyType implements Func
{
public function __construct(private Service $service) {}
public function process(EvaluatedArgumentList $arguments): bool
{
if (count($arguments) < 3) {
throw TooFewArguments::create(3);
}
$userId = $arguments[0];
$from = $arguments[1];
$to = $arguments[2];
$entityType = $arguments[3] ?? null;
$id = $arguments[4] ?? null;
if (!is_string($userId)) {
throw BadArgumentType::create(1, 'string');
}
if (!is_string($from)) {
throw BadArgumentType::create(2, 'string');
}
if (!is_string($to)) {
throw BadArgumentType::create(3, 'string');
}
if ($entityType !== null && !is_string($entityType)) {
throw BadArgumentType::create(4, 'string');
}
if ($id !== null && !is_string($id)) {
throw BadArgumentType::create(5, 'string');
}
$params = FetchParams::create(DateTime::fromString($from), DateTime::fromString($to))
->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 !== [];
}
}

View File

@@ -1,5 +1,19 @@
{
"functionClassNameMap": {
"ext\\account\\findByEmailAddress": "Espo\\Modules\\Crm\\Classes\\FormulaFunctions\\ExtGroup\\AccountGroup\\FindByEmailAddressType"
}
}
"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"
}
]
}

View File

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

View File

@@ -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()

View File

@@ -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));
}
}