This commit is contained in:
Yuri Kuznetsov
2023-02-19 12:58:37 +02:00
parent b635018949
commit b59ff42ac2
8 changed files with 230 additions and 108 deletions

View File

@@ -27,29 +27,24 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Controllers;
namespace Espo\Core\Action\Api;
use Espo\Core\{
Exceptions\BadRequest,
Action\Service,
Api\Request,
};
use stdClass;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Action\Service;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
/**
* Action framework.
* Processes actions.
*/
class Action
class PostProcess implements Action
{
private $service;
public function __construct(private Service $service)
{}
public function __construct(Service $service)
{
$this->service = $service;
}
public function postActionProcess(Request $request): stdClass
public function process(Request $request): Response
{
$body = $request->getParsedBody();
@@ -64,6 +59,6 @@ class Action
$entity = $this->service->process($entityType, $action, $id, $data);
return $entity->getValueMap();
return ResponseComposer::json($entity->getValueMap());
}
}

View File

@@ -0,0 +1,56 @@
<?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\Core\MassAction\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\MassAction\Service;
class GetStatus implements Action
{
public function __construct(private Service $service)
{}
public function process(Request $request): Response
{
$id = $request->getRouteParam('id');
if (!$id) {
throw new BadRequest();
}
$result = $this->service->getStatusData($id);
return ResponseComposer::json($result);
}
}

View File

@@ -27,37 +27,31 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Controllers;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Error;
use Espo\Core\MassAction\Service;
use Espo\Core\MassAction\ServiceResult;
use Espo\Core\MassAction\Params;
use Espo\Core\MassAction\ServiceParams;
namespace Espo\Core\MassAction\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Error;
use Espo\Core\MassAction\Params;
use Espo\Core\MassAction\Service;
use Espo\Core\MassAction\ServiceParams;
use Espo\Core\MassAction\ServiceResult;
use Espo\Core\Utils\Json;
use stdClass;
use RuntimeException;
use stdClass;
/**
* Mass-Action framework.
* Processes mass actions.
*/
class MassAction
class PostProcess implements Action
{
private $service;
public function __construct(private Service $service)
{}
public function __construct(Service $service)
{
$this->service = $service;
}
public function postActionProcess(Request $request): stdClass
public function process(Request $request): Response
{
$body = $request->getParsedBody();
@@ -90,35 +84,13 @@ class MassAction
$data
);
return $this->convertResult($result);
}
$result = $this->convertResult($result);
public function getActionStatus(Request $request): stdClass
{
$id = $request->getQueryParam('id');
if (!$id) {
throw new BadRequest();
}
return $this->service->getStatusData($id);
}
public function postActionSubscribeToNotificationOnSuccess(Request $request, Response $response): void
{
$id = $request->getParsedBody()->id ?? null;
if (!$id || !is_string($id)) {
throw new BadRequest();
}
$this->service->subscribeToNotificationOnSuccess($id);
$response->writeBody('true');
return ResponseComposer::json($result);
}
/**
* @return array<string,mixed>
* @return array<string, mixed>
* @throws BadRequest
*/
private function prepareMassActionParams(stdClass $data): array
@@ -141,13 +113,11 @@ class MassAction
return $params;
}
if (!is_null($ids)) {
return [
'ids' => $ids,
];
if (is_null($ids)) {
throw new BadRequest("Bad search params for mass action.");
}
throw new BadRequest("Bad search params for mass action.");
return ['ids' => $ids];
}
/**

View File

@@ -0,0 +1,56 @@
<?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\Core\MassAction\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\MassAction\Service;
class PostSubscribe implements Action
{
public function __construct(private Service $service)
{}
public function process(Request $request): Response
{
$id = $request->getRouteParam('id');
if (!$id) {
throw new BadRequest();
}
$this->service->subscribeToNotificationOnSuccess($id);
return ResponseComposer::json(true);
}
}

View File

@@ -228,29 +228,35 @@
"name": ":name"
}
},
{
"route": "/CurrencyRate",
"method": "get",
"actionClassName": "Espo\\Tools\\Currency\\Api\\Get"
},
{
"route": "/CurrencyRate",
"method": "put",
"params": {
"controller": "CurrencyRate",
"action": "update"
}
"actionClassName": "Espo\\Tools\\Currency\\Api\\PutUpdate"
},
{
"route": "/Action",
"method": "post",
"params": {
"controller": "Action",
"action": "process"
}
"actionClassName": "Espo\\Core\\Action\\Api\\PostProcess"
},
{
"route": "/MassAction",
"method": "post",
"params": {
"controller": "MassAction",
"action": "process"
}
"actionClassName": "Espo\\Core\\MassAction\\Api\\PostProcess"
},
{
"route": "/MassAction/:id/status",
"method": "get",
"actionClassName": "Espo\\Core\\MassAction\\Api\\GetStatus"
},
{
"route": "/MassAction/:id/subscribe",
"method": "post",
"actionClassName": "Espo\\Core\\MassAction\\Api\\PostSubscribe"
},
{
"route": "/Export",

View File

@@ -0,0 +1,52 @@
<?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\Tools\Currency\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Tools\Currency\RateService as Service;
/**
* Gets rates.
*/
class Get implements Action
{
public function __construct(private Service $service)
{}
public function process(Request $request): Response
{
$result = $this->service->get()->toAssoc();
return ResponseComposer::json($result);
}
}

View File

@@ -27,33 +27,24 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Controllers;
namespace Espo\Tools\Currency\Api;
use Espo\Core\Currency\Rates;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Tools\Currency\RateService as Service;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use stdClass;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Currency\Rates;
use Espo\Tools\Currency\RateService as Service;
class CurrencyRate
/**
* Updates rates.
*/
class PutUpdate implements Action
{
public function __construct(private Service $service)
{}
/**
* @throws Forbidden
*/
public function getActionIndex(): stdClass
{
return (object) $this->service->get()->toAssoc();
}
/**
* @throws BadRequest
* @throws Forbidden
*/
public function putActionUpdate(Request $request): bool
public function process(Request $request): Response
{
$data = $request->getParsedBody();
@@ -61,6 +52,6 @@ class CurrencyRate
$this->service->set($rates);
return true;
return ResponseComposer::json(true);
}
}

View File

@@ -114,9 +114,7 @@ define('views/modals/mass-action', ['views/modal', 'model'], function (Dep, Mode
return;
}
Espo.Ajax.postRequest('MassAction/action/subscribeToNotificationOnSuccess', {
id: this.id,
});
Espo.Ajax.postRequest(`MassAction/${this.id}/subscribe`);
});
this.checkStatus();
@@ -124,9 +122,7 @@ define('views/modals/mass-action', ['views/modal', 'model'], function (Dep, Mode
checkStatus: function () {
Espo.Ajax
.getRequest('MassAction/action/status', {
id: this.id,
})
.getRequest(`MassAction/${this.id}/status`)
.then(response => {
let status = response.status;