mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
writing TransactionTransfer Validation method, update tests
This commit is contained in:
parent
18848f65ae
commit
2bfee6b405
@ -41,7 +41,8 @@ class TransactionBase {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function updateStateBalance($stateUserId, $newAmountCent) {
|
protected function updateStateBalance($stateUserId, $addAmountCent) {
|
||||||
|
$finalBalance = 0;
|
||||||
$stateBalancesTable = TableRegistry::getTableLocator()->get('stateBalances');
|
$stateBalancesTable = TableRegistry::getTableLocator()->get('stateBalances');
|
||||||
$stateBalanceQuery = $stateBalancesTable
|
$stateBalanceQuery = $stateBalancesTable
|
||||||
->find('all')
|
->find('all')
|
||||||
@ -52,18 +53,19 @@ class TransactionBase {
|
|||||||
|
|
||||||
if($stateBalanceQuery->count() > 0) {
|
if($stateBalanceQuery->count() > 0) {
|
||||||
$stateBalanceEntry = $stateBalanceQuery->first();
|
$stateBalanceEntry = $stateBalanceQuery->first();
|
||||||
$stateBalanceEntry->amount += $newAmountCent;
|
$stateBalanceEntry->amount += $addAmountCent;
|
||||||
} else {
|
} else {
|
||||||
$stateBalanceEntry = $stateBalancesTable->newEntity();
|
$stateBalanceEntry = $stateBalancesTable->newEntity();
|
||||||
$stateBalanceEntry->state_user_id = $stateUserId;
|
$stateBalanceEntry->state_user_id = $stateUserId;
|
||||||
$stateBalanceEntry->amount = $newAmountCent;
|
$stateBalanceEntry->amount = $addAmountCent;
|
||||||
}
|
}
|
||||||
|
$finalBalance = $stateBalanceEntry->amount;
|
||||||
//echo "\ntry to save: "; var_dump($stateBalanceEntry); echo "\n";
|
//echo "\ntry to save: "; var_dump($stateBalanceEntry); echo "\n";
|
||||||
if(!$stateBalancesTable->save($stateBalanceEntry)) {
|
if(!$stateBalancesTable->save($stateBalanceEntry)) {
|
||||||
$errors = $stateBalanceEntry->getErrors();
|
$errors = $stateBalanceEntry->getErrors();
|
||||||
$this->addError('TransactionBase::updateStateBalance', 'error saving state balance with: ' . json_encode($errors));
|
$this->addError('TransactionBase::updateStateBalance', 'error saving state balance with: ' . json_encode($errors));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return $finalBalance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ class TransactionCreation extends TransactionBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update state balance
|
// update state balance
|
||||||
if(!$this->updateStateBalance($receiverUser, $this->getAmount())) {
|
if(false === $this->updateStateBalance($receiverUser, $this->getAmount())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -153,9 +153,53 @@ class TransactionTransfer extends TransactionBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function save($transaction_id, $firstPublic) {
|
public function save($transaction_id, $firstPublic) {
|
||||||
$this->addError('TransactionTransfer::save', 'not implemented yet');
|
|
||||||
|
static $functionName = 'TransactionCreation::save';
|
||||||
|
|
||||||
|
if(count($this->protoTransactionTransfer->getSenderAmounts()) !== 1) {
|
||||||
|
$this->addError($functionName, 'not more than one sender currently supported');
|
||||||
return false;
|
return false;
|
||||||
//return true;
|
}
|
||||||
|
$senderAmount = $this->protoTransactionTransfer->getSenderAmounts()[0];
|
||||||
|
|
||||||
|
if(count($this->protoTransactionTransfer->getReceiverAmounts()) !== 1) {
|
||||||
|
$this->addError($functionName, 'not more tahn one receiver currently supported');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$receiverAmount = $this->protoTransactionTransfer->getReceiverAmounts()[0];
|
||||||
|
$transactionTransferTable = TableRegistry::getTableLocator()->get('TransactionSendCoins');
|
||||||
|
|
||||||
|
$senderUserId = $this->getStateUserId($senderAmount->getEd25519SenderPubkey());
|
||||||
|
$receiverUserId = $this->getStateUserId($receiverAmount->getEd25519ReceiverPubkey());
|
||||||
|
|
||||||
|
if($senderUserId === NULL || $receiverUserId === NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$finalSenderBalance = $this->updateStateBalance($senderUserId, -$senderAmount->amount());
|
||||||
|
if(false === $finalSenderBalance) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(false === $this->updateStateBalance($receiverUserId, $receiverAmount->amount())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$transactionTransferEntity = $transactionTransferTable->newEntity();
|
||||||
|
$transactionTransferEntity->transaction_id = $transaction_id;
|
||||||
|
$transactionTransferEntity->state_user_id = $senderUserId;
|
||||||
|
$transactionTransferEntity->receiver_public_key = $receiverAmount->getEd25519ReceiverPubkey();
|
||||||
|
$transactionTransferEntity->receiver_user_id = $receiverUserId;
|
||||||
|
$transactionTransferEntity->amount = $senderAmount->amount();
|
||||||
|
$transactionTransferEntity->sender_final_balance = $finalSenderBalance;
|
||||||
|
|
||||||
|
if(!$transactionTransferTable->save($transactionTransferEntity)) {
|
||||||
|
$this->addError($functionName, 'error saving transactionSendCoins with errors: ' . json_encode($transactionTransferEntity->getErrors()));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//$this->addError('TransactionTransfer::save', 'not implemented yet');
|
||||||
|
//return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user