...
 
Commits (2)
......@@ -2,10 +2,9 @@
namespace Minds\Controllers\api\v2\admin\rewards;
use Exception;
use Minds\Api\Exportable;
use Minds\Common\Repository\Response;
use Minds\Core\Di\Di;
use Minds\Core\Rewards\Withdraw\Manager;
use Minds\Core\Rewards\Withdraw\Repository;
use Minds\Core\Rewards\Withdraw\Request;
use Minds\Entities\User;
use Minds\Interfaces;
......@@ -21,7 +20,8 @@ class withdrawals implements Interfaces\Api, Interfaces\ApiAdminPam
*/
public function get($pages)
{
$repository = new Repository();
/** @var Manager $manager */
$manager = Di::_()->get('Rewards\Withdraw\Manager');
$userGuid = null;
......@@ -36,13 +36,15 @@ class withdrawals implements Interfaces\Api, Interfaces\ApiAdminPam
'user_guid' => $userGuid,
'limit' => isset($_GET['limit']) ? (int) $_GET['limit'] : 12,
'offset' => isset($_GET['offset']) ? $_GET['offset'] : '',
'hydrate' => true,
];
$withdrawals = $repository->getList($opts);
/** @var Response $withdrawals */
$withdrawals = $manager->getList($opts);
return Factory::response([
'withdrawals' => Exportable::_($withdrawals['withdrawals']),
'load-next' => (string) base64_encode($withdrawals['token']),
'withdrawals' => $withdrawals,
'load-next' => $withdrawals->getPagingToken(),
]);
}
......
......@@ -1531,5 +1531,5 @@ CREATE MATERIALIZED VIEW minds.withdrawals_by_status AS
SELECT *
FROM minds.withdrawals
WHERE status IS NOT NULL AND user_guid IS NOT NULL AND timestamp IS NOT NULL AND tx IS NOT NULL
PRIMARY KEY (status, user_guid, timestamp, tx)
WITH CLUSTERING ORDER BY (user_guid ASC, timestamp ASC, tx ASC);
PRIMARY KEY (status, timestamp, user_guid, tx)
WITH CLUSTERING ORDER BY (timestamp ASC, user_guid ASC, tx ASC);
......@@ -82,8 +82,8 @@ class NotificationsDelegate
public function onApprove(Request $request): void
{
$message = sprintf(
"Your withdrawal request has been approved and %s OnChain token(s) were issued.",
BigNumber::fromPlain($request->getAmount(), 18)
"Your withdrawal request has been approved and %g OnChain token(s) were issued.",
BigNumber::fromPlain($request->getAmount(), 18)->toDouble()
);
$this->dispatcher->trigger('notification', 'all', [
......@@ -102,8 +102,8 @@ class NotificationsDelegate
public function onReject(Request $request): void
{
$message = sprintf(
"Your withdrawal request has been rejected. Your %s OffChain token(s) were refunded.",
BigNumber::fromPlain($request->getAmount(), 18)
"Your withdrawal request has been rejected. Your %g OffChain token(s) were refunded.",
BigNumber::fromPlain($request->getAmount(), 18)->toDouble()
);
$this->dispatcher->trigger('notification', 'all', [
......
<?php
/**
* RequestHydrationDelegate
* @author edgebal
*/
namespace Minds\Core\Rewards\Withdraw\Delegates;
use Exception;
use Minds\Core\Rewards\Withdraw\Request;
use Minds\Entities\User;
class RequestHydrationDelegate
{
/**
* @param Request $request
* @return Request
* @throws Exception
*/
public function hydrate(Request $request)
{
$userGuid = $request->getUserGuid();
if (!$userGuid) {
return $request;
}
$user = new User($userGuid);
return $request
->setUser($user);
}
}
......@@ -5,6 +5,7 @@
namespace Minds\Core\Rewards\Withdraw;
use Exception;
use Minds\Common\Repository\Response;
use Minds\Core\Blockchain\Services\Ethereum;
use Minds\Core\Blockchain\Transactions\Manager as TransactionsManager;
use Minds\Core\Blockchain\Transactions\Transaction;
......@@ -39,6 +40,9 @@ class Manager
/** @var Delegates\NotificationsDelegate */
protected $notificationsDelegate;
/** @var Delegates\RequestHydrationDelegate */
protected $requestHydrationDelegate;
public function __construct(
$txManager = null,
$offChainTransactions = null,
......@@ -46,7 +50,8 @@ class Manager
$eth = null,
$repository = null,
$offChainBalance = null,
$notificationsDelegate = null
$notificationsDelegate = null,
$requestHydrationDelegate = null
) {
$this->txManager = $txManager ?: Di::_()->get('Blockchain\Transactions\Manager');
$this->offChainTransactions = $offChainTransactions ?: Di::_()->get('Blockchain\Wallets\OffChain\Transactions');
......@@ -55,6 +60,7 @@ class Manager
$this->repository = $repository ?: new Repository();
$this->offChainBalance = $offChainBalance ?: Di::_()->get('Blockchain\Wallets\OffChain\Balance');
$this->notificationsDelegate = $notificationsDelegate ?: new Delegates\NotificationsDelegate();
$this->requestHydrationDelegate = $requestHydrationDelegate ?: new Delegates\RequestHydrationDelegate();
}
/**
......@@ -81,12 +87,42 @@ class Manager
|| count($previousRequests['withdrawals']) === 0;
}
/**
* @param array $opts
* @return Response
* @throws Exception
*/
public function getList(array $opts = []): Response
{
$opts = array_merge([
'hydrate' => false
], $opts);
$requests = $this->repository->getList($opts);
$response = new Response();
foreach ($requests['withdrawals'] ?? [] as $request) {
if ($opts['hydrate']) {
$request = $this->requestHydrationDelegate->hydrate($request);
}
$response[] = $request;
}
$response
->setPagingToken(base64_encode($requests['load-next'] ?? ''));
return $response;
}
/**
* @param Request $request
* @param bool $hydrate
* @return Request|null
* @throws Exception
*/
public function get(Request $request): ?Request
public function get(Request $request, $hydrate = false): ?Request
{
if (
!$request->getUserGuid() ||
......@@ -103,7 +139,14 @@ class Manager
'limit' => 1,
]);
return $requests['withdrawals'][0] ?? null;
/** @var Request|null $request */
$request = $requests['withdrawals'][0] ?? null;
if ($request && $hydrate) {
$request = $this->requestHydrationDelegate->hydrate($request);
}
return $request;
}
/**
......
<?php
namespace Minds\Core\Rewards\Withdraw;
use JsonSerializable;
use Minds\Entities\User;
use Minds\Traits\MagicAttributes;
/**
......@@ -24,8 +26,10 @@ use Minds\Traits\MagicAttributes;
* @method Request setCompleted(bool $completed)
* @method int getTimestamp()
* @method Request setTimestamp(int $timestamp)
* @method User|null getUser()
* @method Request setUser(User|null $user)
*/
class Request
class Request implements JsonSerializable
{
use MagicAttributes;
......@@ -56,6 +60,9 @@ class Request
/** @var int **/
protected $timestamp;
/** @var User */
protected $user;
/**
* @return array
*/
......@@ -68,7 +75,20 @@ class Request
'tx' => $this->tx,
'status' => $this->status,
'completed' => $this->completed,
'completed_tx' => $this->completedTx
'completed_tx' => $this->completedTx,
'user' => $this->user ? $this->user->export() : null,
];
}
/**
* Specify data which should be serialized to JSON
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return $this->export();
}
}