New endpoint to list the accounts of a hosting server & Fixes (#2728)

* +toHostingAccountApiArray

* +getAccountsSearchQuery

* +accounts_get_list & pagination fix for server_get_list

* Update AdminTest.php

* +tests for the account_get_list

* Update Admin.php

* Also return linked order and client data

* Update AdminTest.php

* Removed duplicated field

* Adam's suggestions
This commit is contained in:
Yağızhan authored and GitHub committed 2025-05-22 00:33:11 +03:00
1 parent 59489d502d
commit 3fda0795e1
3 files changed
+106 -13

No files matched your search

+43 -8
View File
@@ -138,23 +138,58 @@ class Admin extends \Api_Abstract
}
/**
* Get paginated list of servers.
* Get a paginated list of servers.
*
* @return array
*/
public function server_get_list($data)
{
$servers = $this->di['db']->find('ServiceHostingServer', 'ORDER BY id ASC');
$serversArr = [];
foreach ($servers as $server) {
$serversArr[] = $this->getService()->toHostingServerApiArray($server, false, $this->getIdentity());
}
[$sql, $params] = $this->getService()->getServersSearchQuery($data);
$per_page = $data['per_page'] ?? $this->di['pager']->getPer_page();
$result = $this->di['pager']->getSimpleResultSet($sql, $params, $per_page);
$result['list'] = $serversArr;
foreach ($result['list'] as $key => $server) {
$bean = $this->di['db']->dispense('ServiceHostingServer')->unbox();
$bean->import($server);
$model = $bean->box();
$result['list'][$key] = $this->getService()->toHostingServerApiArray($model, false, $this->getIdentity());
}
return $result;
}
/**
* Get a paginated list of hosting accounts, along with the "order" and "client" information.
*
* @param $data array Accepts the optional "server_id" property
* @return array
*/
public function account_get_list($data)
{
[$sql, $params] = $this->getService()->getAccountsSearchQuery($data);
$per_page = $data['per_page'] ?? $this->di['pager']->getPer_page();
$result = $this->di['pager']->getSimpleResultSet($sql, $params, $per_page);
$orderService = $this->di['mod_service']('order');
foreach ($result['list'] as $key => $account) {
$bean = $this->di['db']->dispense('ServiceHosting')->unbox();
$bean->import($account);
$model = $bean->box();
$order = $this->di['db']->findOne('ClientOrder', 'service_id = :service_id', [':service_id' => $model->id]);
$result['list'][$key] = $this->getService()->toHostingAccountApiArray($model, true, $this->getIdentity());
if ($order) {
$result['list'][$key]['order'] = $orderService->toApiArray($order);
$result['list'][$key]['client'] = $result['list'][$key]['order']['client'];
unset($result['list'][$key]['order']['client']);
} else {
$result['list'][$key]['order'] = null;
}
}
return $result;
}
+39
View File
@@ -552,6 +552,28 @@ class Service implements InjectionAwareInterface
return $result;
}
public function toHostingAccountApiArray(\Model_ServiceHosting $model, $deep = false, $identity = null): array
{
$result = [
'id' => $model->id,
'sld' => $model->sld,
'tld' => $model->tld,
'client_id' => $model->client_id,
'server_id' => $model->service_hosting_server_id,
'plan_id' => $model->service_hosting_hp_id,
'reseller' => $model->reseller,
];
if ($identity instanceof \Model_Admin) {
$result['ip'] = $model->ip;
$result['username'] = $model->username;
$result['created_at'] = $model->created_at;
$result['updated_at'] = $model->updated_at;
}
return $result;
}
private function _getDomainTuple($data): array
{
$required = [
@@ -681,6 +703,23 @@ class Service implements InjectionAwareInterface
return [$sql, []];
}
public function getAccountsSearchQuery($data): array
{
$sql = 'SELECT * FROM service_hosting';
$params = [];
$serverID = $data['server_id'] ?? null;
if (!empty($serverID)) {
$sql = $sql . " WHERE service_hosting_server_id = :server_id";
$params['server_id'] = $serverID;
}
$sql = $sql . " ORDER BY id ASC";
return [$sql, $params];
}
public function createServer($name, $ip, $manager, $data)
{
$model = $this->di['db']->dispense('ServiceHostingServer');
@@ -231,6 +231,30 @@ class AdminTest extends \BBTestCase
$this->assertIsArray($result);
}
public function testaccountGetList(): void
{
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Servicehosting\Service::class)->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getAccountsSearchQuery')
->willReturn(['SQLstring', []]);
$pagerMock = $this->getMockBuilder('\Box_Pagination')->getMock();
$pagerMock->expects($this->atLeastOnce())
->method('getSimpleResultSet')
->willReturn(['list' => []]);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn ($name) => $systemServiceMock);
$di['pager'] = $pagerMock;
$di['db'] = $dbMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->account_get_list([]);
$this->assertIsArray($result);
}
public function testserverGetList(): void
{
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Servicehosting\Service::class)->getMock();
@@ -243,11 +267,6 @@ class AdminTest extends \BBTestCase
->method('getSimpleResultSet')
->willReturn(['list' => []]);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('find')
->willReturn([]);
$di = new \Pimple\Container();
$di['pager'] = $pagerMock;
$di['db'] = $dbMock;