diff --git a/src/modules/Servicehosting/Api/Admin.php b/src/modules/Servicehosting/Api/Admin.php index 5f9cbf4da..e259d750e 100644 --- a/src/modules/Servicehosting/Api/Admin.php +++ b/src/modules/Servicehosting/Api/Admin.php @@ -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; } diff --git a/src/modules/Servicehosting/Service.php b/src/modules/Servicehosting/Service.php index 66ecac964..762e8f323 100644 --- a/src/modules/Servicehosting/Service.php +++ b/src/modules/Servicehosting/Service.php @@ -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'); diff --git a/tests-legacy/modules/Servicehosting/Api/AdminTest.php b/tests-legacy/modules/Servicehosting/Api/AdminTest.php index b6d939bcc..3c1daf0c9 100644 --- a/tests-legacy/modules/Servicehosting/Api/AdminTest.php +++ b/tests-legacy/modules/Servicehosting/Api/AdminTest.php @@ -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;