add function to fill state_user_transactions, work on client-api

This commit is contained in:
Dario Rekowski on RockPI 2020-11-18 18:37:00 +00:00 committed by Ulf Gebhardt
parent 8687f9b9c5
commit 9e871d6d4b
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
5 changed files with 179 additions and 44 deletions

View File

@ -1,8 +1,9 @@
CREATE TABLE `transaction_group_addaddress` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`transaction_id` int(10) unsigned NOT NULL,
`address_type_id` int(10) unsigned NOT NULL,
`id` int unsigned NOT NULL AUTO_INCREMENT,
`transaction_id` int unsigned NOT NULL,
`address_type_id` int unsigned NOT NULL,
`remove_from_group` BOOLEAN DEFAULT FALSE,
`public_key` binary(32) NOT NULL,
`state_user_id` int unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@ -386,8 +386,100 @@ class StateBalancesController extends AppController
public function ajaxListTransactions($page = 0, $count = 20)
{
// TODO: add efficient paging with additional table: state_user_transactions
return $this->returnJson(['state' => 'success', 'transactions' => [], 'transactionExecutingCount' => 0, 'count' => 0]);
$session = $this->getRequest()->getSession();
$user = $session->read('StateUser');
if(!$user) {
return $this->returnJson(['state' => 'error', 'msg' => 'user not found', 'details' => 'exist a valid session cookie?']);
}
$stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions');
$paged_state_user_transactions = $stateUserTransactionsTable
->find('all')
->where(['state_user_id' => $user['id'], 'transaction_type_id IN' => [1,2]])
->limit($count)
->page($page)
->order(['transaction_id'])
;
$creationTransaction_ids = [];
$transferTransaction_ids = [];
$allTransaction_ids = [];
foreach($paged_state_user_transactions as $state_user_transaction) {
$allTransaction_ids[] = $state_user_transaction->transaction_id;
switch($state_user_transaction->transaction_type_id) {
case 1: $creationTransaction_ids[] = $state_user_transaction->transaction_id; break;
case 2: $transferTransaction_ids[] = $state_user_transaction->transaction_id; break;
}
}
$transactionsTable = TableRegistry::getTableLocator()->get('Transactions');
$transactionCreationsTable = TableRegistry::getTableLocator()->get('TransactionCreations');
$transactionSendCoinsTable = TableRegistry::getTableLocator()->get('TransactionSendCoins');
$transactionEntries = $transactionsTable->find('all')->where(['id IN' => $allTransaction_ids])->order(['id'])->toArray();
$transactionCreations = $transactionCreationsTable->find('all')->where(['transaction_id IN' => $creationTransaction_ids]);
$transactionTransfers = $transactionSendCoinsTable->find('all')->where(['transaction_id IN' => $transferTransaction_ids]);
//var_dump($transactions->all());
$transactions = [];
foreach ($transactionCreations as $creation) {
//var_dump($creation);
$transaction_entries_index = array_search($creation->transaction_id, $allTransaction_ids);
if(FALSE == $transaction_entries_index) {
return $this->returnJson(['state' => 'error', 'msg' => 'code error', 'details' => 'transaction_entries_index is FALSE, shouldn\'t occure']);
}
$transaction = $transactionEntries[$transaction_entries_index];
array_push($transactions, [
'name' => 'Gradido Akademie',
'type' => 'creation',
'transaction_id' => $creation->transaction_id,
'date' => $transaction->received,
'balance' => $creation->amount,
'memo' => $transaction->memo
]);
}
foreach($transactionTransfers as $transfer)
{
$transaction_entries_index = array_search($transfer->transaction_id, $allTransaction_ids);
if(FALSE == $transaction_entries_index) {
return $this->returnJson(['state' => 'error', 'msg' => 'code error', 'details' => 'transaction_entries_index is FALSE, shouldn\'t occure']);
}
$transaction = $transactionEntries[$transaction_entries_index];
$type = '';
$otherUser = null;
$other_user_public = '';
/*
if ($sendCoins->state_user_id == $user['id']) {
$type = 'send';
if(isset($involvedUserIndices[$sendCoins->receiver_user_id])) {
$otherUser = $involvedUserIndices[$sendCoins->receiver_user_id];
}
$other_user_public = bin2hex(stream_get_contents($sendCoins->receiver_public_key));
} else if ($sendCoins->receiver_user_id == $user['id']) {
$type = 'receive';
if(isset($involvedUserIndices[$sendCoins->state_user_id])) {
$otherUser = $involvedUserIndices[$sendCoins->state_user_id];
}
if($sendCoins->sender_public_key) {
$other_user_public = bin2hex(stream_get_contents($sendCoins->sender_public_key));
}
}
if(null == $otherUser) {
$otherUser = $this->StateBalances->StateUsers->newEntity();
}
array_push($transactions, [
'name' => $otherUser->first_name . ' ' . $otherUser->last_name,
'email' => $otherUser->email,
'type' => $type,
'transaction_id' => $sendCoins->transaction_id,
'date' => $sendCoins->transaction->received,
'balance' => $sendCoins->amount,
'memo' => $sendCoins->transaction->memo,
'pubkey' => $other_user_public
]);
* */
}
return $this->returnJson(['state' => 'success', 'transactions' => $transactions, 'transactionExecutingCount' => 0, 'count' => 0]);
}
public function ajaxGdtOverview()

View File

@ -31,6 +31,7 @@ class TransactionGroupAddaddres extends Entity
'remove_from_group' => true,
'public_key' => true,
'transaction' => true,
'address_type' => true
'address_type' => true,
'state_user_id' => true
];
}

View File

@ -45,6 +45,10 @@ class TransactionGroupAddaddressTable extends Table
'foreignKey' => 'address_type_id',
'joinType' => 'INNER'
]);
$this->belongsTo('StateUsers', [
'foreignKey' => 'state_user_id',
'joinType' => 'INNER'
]);
}
/**
@ -77,6 +81,7 @@ class TransactionGroupAddaddressTable extends Table
{
$rules->add($rules->existsIn(['transaction_id'], 'Transactions'));
$rules->add($rules->existsIn(['address_type_id'], 'AddressTypes'));
$rules->add($rules->existsIn(['state_user_id'], 'StateUsers'));
return $rules;
}

View File

@ -54,7 +54,55 @@ class Signature
}
class ManageNodeGroupAdd
class GradidoModifieUserBalance
{
private $state_users = [];
public function getUserId($userPublicKey)
{
$stateUsersTable = TableRegistry::getTableLocator()->get('StateUsers');
$stateUser = $stateUsersTable->find('all')->where(['public_key' => hex2bin($userPublicKey)]);
if($stateUser->isEmpty()) {
return ['state' => 'error', 'msg' => 'couldn\'t find user via public key'];
}
$id = $stateUser->first()->id;
if($id && is_int($id) && (int)$id > 0 && !in_array((int)$id, $this->state_users)) {
array_push($this->state_users, (int)$id);
}
return $stateUser->first()->id;
}
public function updateBalance($newBalance, $recordDate, $userId)
{
$stateBalancesTable = TableRegistry::getTableLocator()->get('StateBalances');
$stateBalanceQuery = $stateBalancesTable->find('all')->where(['state_user_id' => $userId]);
$entity = null;
if(!$stateBalanceQuery->isEmpty()) {
$entity = $stateBalanceQuery->first();
if($entity->record_date != NULL && $entity->record_date > $recordDate) {
return false;
}
} else {
$entity = $stateBalancesTable->newEntity();
$entity->state_user_id = $userId;
}
$entity->record_date = $recordDate;
$entity->amount = $newBalance;
if(!$stateBalancesTable->save($entity)) {
return ['state' => 'error', 'msg' => 'error saving state balance', 'details' => $entity->getErrors()];
}
return true;
}
public function getAllStateUsers()
{
return $this->state_users;
}
}
class ManageNodeGroupAdd extends GradidoModifieUserBalance
{
/*
"add_user": {
@ -100,11 +148,13 @@ class ManageNodeGroupAdd
}
$transactionGroupEntity->public_key = hex2bin($this->user_pubkey);
$transactionGroupEntity->state_user_id = $this->getUserId($this->user_pubkey);
$transactionGroupEntity->remove_from_group = $this->remove_from_group;
if(!$transactionGroupAddadressTable->save($transactionGroupEntity)) {
return ['state' => 'error', 'msg' => 'error saving TransactionGroupAddaddress Entity', 'details' => $transactionGroupEntity->getErrors()];
}
$userPubkeyBin = hex2bin($this->user_pubkey);
if($this->remove_from_group) {
$stateGroup_query = $stateGroupAddresses->find('all')->where(['public_key' => hex2bin($this->user_pubkey)]);
if(!$stateGroup_query->isEmpty()) {
@ -128,42 +178,6 @@ class ManageNodeGroupAdd
}
}
class GradidoModifieUserBalance
{
public function getUserId($userPublicKey)
{
$stateUsersTable = TableRegistry::getTableLocator()->get('StateUsers');
$stateUser = $stateUsersTable->find('all')->where(['public_key' => hex2bin($userPublicKey)]);
if($stateUser->isEmpty()) {
return ['state' => 'error', 'msg' => 'couldn\'t find user via public key'];
}
return $stateUser->first()->id;
}
public function updateBalance($newBalance, $recordDate, $userId)
{
$stateBalancesTable = TableRegistry::getTableLocator()->get('StateBalances');
$stateBalanceQuery = $stateBalancesTable->find('all')->where(['state_user_id' => $userId]);
$entity = null;
if(!$stateBalanceQuery->isEmpty()) {
$entity = $stateBalanceQuery->first();
if($entity->record_date != NULL && $entity->record_date > $recordDate) {
return false;
}
} else {
$entity = $stateBalancesTable->newEntity();
$entity->state_user_id = $userId;
}
$entity->record_date = $recordDate;
$entity->amount = $newBalance;
if(!$stateBalancesTable->save($entity)) {
return ['state' => 'error', 'msg' => 'error saving state balance', 'details' => $entity->getErrors()];
}
return true;
}
}
class GradidoCreation extends GradidoModifieUserBalance
{
@ -180,6 +194,7 @@ class GradidoCreation extends GradidoModifieUserBalance
private $targetDate; // seems currently not in node server implementet, use hedera date until it is implemented
private $new_balance;
public function __construct($data)
{
$this->userPubkey = $data['user'];
@ -219,6 +234,8 @@ class GradidoCreation extends GradidoModifieUserBalance
return true;
}
}
class GradidoTransfer extends GradidoModifieUserBalance
@ -245,6 +262,7 @@ class GradidoTransfer extends GradidoModifieUserBalance
private $receiver_pubkey;
private $receiver_new_balance;
public function __construct($data)
{
$this->amount = $data['amount'];
@ -295,7 +313,7 @@ class GradidoTransfer extends GradidoModifieUserBalance
}
}
if(is_int($receiver_id) && $receiver_id > 0) {
$transferEntity->receiver_user_id = $receiver_id;
$this->state_users[] = $receiver_id;
$balance_result = $this->updateBalance($this->receiver_new_balance, $received, $receiver_id);
if(is_array($balance_result)) {
return $balance_result;
@ -308,6 +326,7 @@ class GradidoTransfer extends GradidoModifieUserBalance
return true;
}
}
@ -355,6 +374,7 @@ class Record
{
$transactionTypesTable = TableRegistry::getTableLocator()->get('TransactionTypes');
$transactionsTable = TableRegistry::getTableLocator()->get('Transactions');
$stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions');
$transactionTypeName = $this->nodeTransactionTypeToDBTransactionType($this->transactionType);
$transactionTypeResults = $transactionTypesTable->find('all')->where(['name' => $transactionTypeName]);
@ -404,6 +424,22 @@ class Record
if($transaction_obj_result !== true) {
return ['state' => 'error', 'msg' => 'error finalizing transaction object', 'details' => $transaction_obj_result];
}
$state_users = $this->transactionObj->getAllStateUsers();
$sut_entities = [];
foreach($state_users as $state_user_id) {
$entity = $stateUserTransactionsTable->newEntity();
$entity->state_user_id = $state_user_id;
$entity->transaction_id = $newTransaction->id;
$entity->transaction_type_id = $newTransaction->transaction_type_id;
$sut_entities[] = $entity;
}
$sut_results = $stateUserTransactionsTable->saveMany($sut_entities);
foreach($sut_results as $i => $result) {
if(false == $result) {
return ['state' => 'error', 'msg' => 'error saving state_user_transaction', 'details' => $sut_entities[$i]->getErrors()];
}
}
return true;
}