From 510377971346fb6645fc7b1cdc3161b83e8a44c8 Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 15 Jan 2020 15:36:00 +0000 Subject: [PATCH] adding js for showing running transactions --- config/routes.php | 2 +- src/Controller/AppController.php | 6 +- .../Component/JsonRequestClientComponent.php | 28 ++++++ src/Controller/DashboardController.php | 8 ++ src/Controller/StateBalancesController.php | 1 + .../TransactionCreationsController.php | 1 + ...ransactionJsonRequestHandlerController.php | 97 ------------------- src/Template/Dashboard/index.ctp | 31 ++++++ src/Template/Layout/frontend.ctp | 5 +- src/Template/StateBalances/overview.ctp | 65 ++++++++++++- .../TransactionCreations/create_multi.ctp | 94 ++++++++++++------ ...actionJsonRequestHandlerControllerTest.php | 2 +- webroot/js/basic.js | 38 ++++++-- 13 files changed, 237 insertions(+), 141 deletions(-) delete mode 100644 src/Controller/TransactionJsonRequestHandlerController.php diff --git a/config/routes.php b/config/routes.php index 1290ad06e..0aec385cc 100644 --- a/config/routes.php +++ b/config/routes.php @@ -55,7 +55,7 @@ Router::scope('/', function (RouteBuilder $routes) { $csrf->whitelistCallback(function ($request) { // Skip token check for API URLs. //die($request->getParam('controller')); - if($request->getParam('controller') === 'TransactionJsonRequestHandler') { + if($request->getParam('controller') === 'JsonRequestHandler') { return true; } }); diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 66a1b6cd5..660be10ae 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -146,9 +146,11 @@ class AppController extends Controller if($session_id != 0) { $userStored = $session->read('StateUser'); $transactionPendings = $session->read('Transactions.pending'); + $transactionExecutings = $session->read('Transaction.executing'); if($session->read('session_id') != $session_id || ( $userStored && !isset($userStored['id'])) || - intval($transactionPendings) > 0) { + intval($transactionPendings) > 0 || + intval($transactionExecutings) > 0) { $http = new Client(); try { $loginServer = Configure::read('LoginServer'); @@ -170,8 +172,10 @@ class AppController extends Controller } //var_dump($json); $transactionPendings = $json['Transaction.pending']; + $transactionExecuting = $json['Transaction.executing']; //echo "read transaction pending: $transactionPendings
"; $session->write('Transactions.pending', $transactionPendings); + $session->write('Transaction.executing', $transactionExecuting); $session->write('session_id', $session_id); $stateUserTable = TableRegistry::getTableLocator()->get('StateUsers'); if($json['user']['public_hex'] != '') { diff --git a/src/Controller/Component/JsonRequestClientComponent.php b/src/Controller/Component/JsonRequestClientComponent.php index bf112bd4e..64e645e0a 100644 --- a/src/Controller/Component/JsonRequestClientComponent.php +++ b/src/Controller/Component/JsonRequestClientComponent.php @@ -7,6 +7,8 @@ */ namespace App\Controller\Component; +use App\Model\Validation\GenericValidation; + use Cake\Controller\Component; use Cake\Http\Client; use Cake\Core\Configure; @@ -53,6 +55,32 @@ class JsonRequestClientComponent extends Component return ['state' => 'success', 'data' => $json]; } + public function getRunningUserTasks($email) + { + if($email == "") { + return ['state' => 'error', 'type' => 'parameter error', 'msg' => 'email is empty']; + } + if(!GenericValidation::email($email, [])) { + return ['state' => 'error', 'type' => 'parameter error', 'msg' => 'email is invalid']; + } + $http = new Client(); + + $transactionbody = json_encode([ + 'email' => $email + ]); + $response = $http->post($this->getLoginServerUrl() . '/getRunningUserTasks', $transactionbody, ['type' => 'json']); + $responseStatus = $response->getStatusCode(); + if($responseStatus != 200) { + return ['state' => 'error', 'type' => 'request error', 'msg' => 'server response status code isn\'t 200', 'details' => $responseStatus]; + } + + $json = $response->getJson(); + if($json == null) { + return ['state' => 'error', 'type' => 'request error', 'msg' => 'server response isn\'t valid json', 'details' => $responseType]; + } + return ['state' => 'success', 'data' => $json]; + } + static public function getLoginServerUrl() { $loginServer = Configure::read('LoginServer'); diff --git a/src/Controller/DashboardController.php b/src/Controller/DashboardController.php index 8e5b44152..cf242b5a0 100644 --- a/src/Controller/DashboardController.php +++ b/src/Controller/DashboardController.php @@ -40,7 +40,15 @@ class DashboardController extends AppController } $user = $session->read('StateUser'); + $serverUser = $this->Auth->user('id'); + if($serverUser) { + $adminErrorsTable = TableRegistry::getTableLocator()->get('AdminErrors'); + $adminErrorCount = $adminErrorsTable->find('all')->count(); + $this->set('adminErrorCount', $adminErrorCount); + } + $this->set('user', $user); + $this->set('serverUser', $serverUser); $this->set('timeUsed', microtime(true) - $startTime); } diff --git a/src/Controller/StateBalancesController.php b/src/Controller/StateBalancesController.php index 0f81cf4c5..d823c9257 100644 --- a/src/Controller/StateBalancesController.php +++ b/src/Controller/StateBalancesController.php @@ -132,6 +132,7 @@ class StateBalancesController extends AppController } uasort($transactions, array($this, 'sortTransactions')); $this->set('transactions', $transactions); + $this->set('transactionExecutingCount', $session->read('Transaction.executing')); $this->set('balance', $session->read('StateUser.balance')); $this->set('timeUsed', microtime(true) - $startTime); } diff --git a/src/Controller/TransactionCreationsController.php b/src/Controller/TransactionCreationsController.php index c9665d707..25b751a10 100644 --- a/src/Controller/TransactionCreationsController.php +++ b/src/Controller/TransactionCreationsController.php @@ -244,6 +244,7 @@ class TransactionCreationsController extends AppController $this->set('activeUser', $user); $this->set('creationForm', $creationForm); + $this->set('transactionExecutingCount', $session->read('Transaction.executing')); $this->set('timeUsed', microtime(true) - $startTime); if ($this->request->is('post')) { diff --git a/src/Controller/TransactionJsonRequestHandlerController.php b/src/Controller/TransactionJsonRequestHandlerController.php deleted file mode 100644 index 651ccc97a..000000000 --- a/src/Controller/TransactionJsonRequestHandlerController.php +++ /dev/null @@ -1,97 +0,0 @@ -Auth->allow(['add', 'edit']); - $this->Auth->allow('index'); - } - - - public function index() - { - if($this->request->is('post')) { - $jsonData = $this->request->input('json_decode'); - //var_dump($jsonData); - if($jsonData == NULL || !isset($jsonData->method) || !isset($jsonData->transaction)) { - return $this->returnJson(['state' => 'error', 'msg' => 'parameter error']); - } - $method = $jsonData->method; - switch($method) { - case 'putTransaction': return $this->putTransaction($jsonData->transaction); - case 'userDelete': return $this->userDelete($jsonData->user); - } - return $this->returnJson(['state' => 'error', 'msg' => 'unknown method', 'details' => $method]); - } - return $this->returnJson(['state' => 'error', 'msg' => 'no post']); - } - - private function putTransaction($transactionBase64) { - $transaction = new Transaction($transactionBase64); - if($transaction->hasErrors()) { - return $this->returnJson(['state' => 'error', 'msg' => 'error parsing transaction', 'details' => $transaction->getErrors()]); - } - if(!$transaction->validate()) { - return $this->returnJsonSaveError($transaction, ['state' => 'error', 'msg' => 'error validate transaction', 'details' => $transaction->getErrors()]); - } - - if ($transaction->save()) { - // success - return $this->returnJson(['state' => 'success']); - } else { - return $this->returnJsonSaveError($transaction, [ - 'state' => 'error', - 'msg' => 'error saving transaction in db', - 'details' => json_encode($transaction->getErrors()) - ]); - } - - return $this->returnJson(['state' => 'success']); - } - - private function userDelete($userPubkeyHex) { - $stateUserTable = TableRegistry::getTableLocator()->get('StateUsers'); - $user = $stateUserTable->find('all')->where(['public_key' => hex2bin($userPubkeyHex)]); - if(!$user || $user->count == 0) { - return $this->returnJson(['state' => 'error', 'msg' => 'user not found']); - } - - } - - - private function returnJsonSaveError($transaction, $errorArray) { - $json = json_encode($errorArray); - $stateUserTable = TableRegistry::getTableLocator()->get('StateUsers'); - $pub = $transaction->getFirstPublic(); - $stateUserQuery = $stateUserTable - ->find('all') - ->where(['public_key' => $pub]) - ->contain(false); - if($stateUserQuery->count() == 1) { - $stateErrorsTable = TableRegistry::getTableLocator()->get('StateErrors'); - $stateErrorEntity = $stateErrorsTable->newEntity(); - $stateErrorEntity->state_user_id = $stateUserQuery->first()->id; - $stateErrorEntity->transaction_type_id = $transaction->getTransactionBody()->getTransactionTypeId(); - $stateErrorEntity->message_json = $json; - $stateErrorsTable->save($stateErrorEntity); - } - return $this->returnJsonEncoded($json); - } - -} \ No newline at end of file diff --git a/src/Template/Dashboard/index.ctp b/src/Template/Dashboard/index.ctp index d975d69a4..4aa08a475 100644 --- a/src/Template/Dashboard/index.ctp +++ b/src/Template/Dashboard/index.ctp @@ -15,6 +15,7 @@ $this->assign('title', __('Willkommen') . ' ' . $user['first_name'] . '&nbs ms
+

Gradido ...

Html->link( @@ -32,4 +33,34 @@ $this->assign('title', __('Willkommen') . ' ' . $user['first_name'] . '&nbs

Account ...

--> + +
+

Server Admin Gradido ...

+ Html->link( + __('einzeln schöpfen'), + ['controller' => 'TransactionCreations', 'action' => 'create'], + ['class' => 'grd-nav-bn grd-nav-bn-large'] + );?> + Html->link( + __('viele schöpfen'), + ['controller' => 'TransactionCreations', 'action' => 'createMulti'], + ['class' => 'grd-nav-bn grd-nav-bn-large'] + );?> +
+ Html->link( + __('Fehler') . ' (' . $adminErrorCount . ')', + ['controller' => 'AdminErrors'], ['class' => 'grd-nav-bn']); + ?> + + +
+

Adminbereich

+ Html->link( + __('Benutzer suchen'), + ['controller' => 'StateUsers', 'action' => 'search'], + ['class' => 'grd-nav-bn grd-nav-bn-large'] + ); ?> +
+ +
diff --git a/src/Template/Layout/frontend.ctp b/src/Template/Layout/frontend.ctp index 609f059d9..d363ed8d1 100644 --- a/src/Template/Layout/frontend.ctp +++ b/src/Template/Layout/frontend.ctp @@ -66,10 +66,7 @@ if(!isset($balance)) { ?> - -
  • Html->link(__('Server'), ['controller' => 'Dashboard', 'action' => 'serverIndex'], ['class' => 'grd-nav-bn'])?>
  • - -
  • Html->link(__('Startseite'), ['controller' => 'Dashboard'], ['class' => 'grd-nav-bn'])?>
  • +
  • Html->link(__('Startseite'), ['controller' => 'Dashboard', 'action' => 'index'], ['class' => 'grd-nav-bn'])?>
  • 0) : ?>
  • diff --git a/src/Template/StateBalances/overview.ctp b/src/Template/StateBalances/overview.ctp index 0c399edc6..b053f868c 100644 --- a/src/Template/StateBalances/overview.ctp +++ b/src/Template/StateBalances/overview.ctp @@ -18,6 +18,17 @@ $this->assign('title', __('Kontoübersicht')); + 0) : ?> +
    +
    +
    +
    + +
    +
    +
    +
    +
    @@ -94,4 +105,56 @@ $this->assign('title', __('Kontoübersicht'));
    -
    \ No newline at end of file + + 0) : ?> + + \ No newline at end of file diff --git a/src/Template/TransactionCreations/create_multi.ctp b/src/Template/TransactionCreations/create_multi.ctp index cf82ee374..021d7e7b1 100644 --- a/src/Template/TransactionCreations/create_multi.ctp +++ b/src/Template/TransactionCreations/create_multi.ctp @@ -57,7 +57,9 @@ $this->assign('title', __('Schöpfungstransaktion'));
    Form->create($creationForm) ?> - + 0) : ?> +
    +
    Form->control('memo'); ?> Form->control('amount', ['required' => false]); ?> @@ -107,6 +109,7 @@ $this->assign('title', __('Schöpfungstransaktion')); Form->end() ?> +Html->script('basic'); ?> \ No newline at end of file + + 0) : ?> + + \ No newline at end of file diff --git a/tests/TestCase/Controller/TransactionJsonRequestHandlerControllerTest.php b/tests/TestCase/Controller/TransactionJsonRequestHandlerControllerTest.php index b9b0cc513..08c43c533 100644 --- a/tests/TestCase/Controller/TransactionJsonRequestHandlerControllerTest.php +++ b/tests/TestCase/Controller/TransactionJsonRequestHandlerControllerTest.php @@ -1,7 +1,7 @@