Skip to content

Commit

Permalink
add transactions search
Browse files Browse the repository at this point in the history
  • Loading branch information
killua-eu committed Feb 12, 2024
1 parent b84f247 commit b8184f2
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions glued/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,66 @@ private function accounts($account = null): mixed
return $d ?? false;
}

private function transactions($trx = null): mixed
private function transactions($trx = false, $args = []): mixed
{
$wq = "";
$wd = [];
$trx = $args['uuid'] ?? false;
if ($trx) { $wq = " AND uuid = uuid_to_bin(?, true)"; $wd[] = $trx; }
$args = array_merge(...array_values($args));
//print_r($trx);
//print_r($args);
// Get a single transaction
if ($trx) {
$wq .= " AND uuid = uuid_to_bin(?, true)";
$wd[] = $trx;
}
// Query for transactions
else {
// Separate numeric and date arguments
$dateArgs = array_filter($args, function($arg) { return preg_match('/^\d{4}-\d{2}-\d{2}$/', $arg); });
$numericArgs = array_filter($args, 'is_numeric');

// Handle non-numeric and non-date arguments for the reference field using LIKE
foreach ($args as $arg) {
if (!is_numeric($arg) && !preg_match('/^\d{4}-\d{2}-\d{2}$/', $arg)) {
$wq .= " AND JSON_SEARCH(trx.data, 'all', ?);";
$wd[] = "%" . $arg . "%";
}
}

// Handle numeric arguments for volume and reference
if (!empty($numericArgs)) {
$numericConditions = [];
foreach ($numericArgs as $numericArg) {
$numericConditions[] = "(volume = ? OR ROUND(volume) = ? OR reference = ?)";
array_push($wd, $numericArg, $numericArg, $numericArg);
}
$wq .= " AND (" . implode(' OR ', $numericConditions) . ")";
}

// Handle date arguments for at with a +/- 5 days range
if (!empty($dateArgs)) {
$dateConditions = [];
foreach ($dateArgs as $dateArg) {
$dateStart = date('Y-m-d', strtotime($dateArg . ' -5 days'));
$dateEnd = date('Y-m-d', strtotime($dateArg . ' +5 days'));
$dateConditions[] = "at BETWEEN ? AND ?";
array_push($wd, $dateStart, $dateEnd);
}
$wq .= " AND (" . implode(' OR ', $dateConditions) . ")";
}

}

$q = "SELECT
acc.ext_fid as account,
bin_to_uuid(trx.uuid, true) as uuid,
trx.data
FROM t_settlement_transactions trx
LEFT JOIN t_settlement_accounts acc ON trx.account = acc.uuid
WHERE 1=1";
WHERE 1=1 {$wq}";
// echo $q; die();

$res = $this->mysqli->execute_query($q, $wd);
// return $res->fetch_all(MYSQLI_ASSOC);
foreach ($res as $k=>$i) {
$d[$k] = $i;
$d[$k]['data'] = json_decode($i['data'], true);
Expand All @@ -94,7 +139,7 @@ public function accounts_r1(Request $request, Response $response, array $args =

public function transactions_r1(Request $request, Response $response, array $args = []): Response
{
$data = $this->transactions($args['uuid'] ?? false);
$data = $this->transactions($args['uuid'] ?? false, $request->getQueryParams() ?? []);
return $response->withJson(['data' => $data]);
}

Expand Down

0 comments on commit b8184f2

Please sign in to comment.