Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/WalletsController.php
Show First 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | public function transactions($id) | ||||
// Only owner (or admin) has access to the wallet | // Only owner (or admin) has access to the wallet | ||||
if (!Auth::guard()->user()->canRead($wallet)) { | if (!Auth::guard()->user()->canRead($wallet)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
$pageSize = 10; | $pageSize = 10; | ||||
$page = intval(request()->input('page')) ?: 1; | $page = intval(request()->input('page')) ?: 1; | ||||
$hasMore = false; | $hasMore = false; | ||||
$isAdmin = $this instanceof Admin\WalletsController; | |||||
if ($transaction = request()->input('transaction')) { | if ($transaction = request()->input('transaction')) { | ||||
// Get sub-transactions for the specified transaction ID, first | // Get sub-transactions for the specified transaction ID, first | ||||
// check access rights to the transaction's wallet | // check access rights to the transaction's wallet | ||||
$transaction = $wallet->transactions()->where('id', $transaction)->first(); | $transaction = $wallet->transactions()->where('id', $transaction)->first(); | ||||
if (!$transaction) { | if (!$transaction) { | ||||
Show All 15 Lines | public function transactions($id) | ||||
->get(); | ->get(); | ||||
if (count($result) > $pageSize) { | if (count($result) > $pageSize) { | ||||
$result->pop(); | $result->pop(); | ||||
$hasMore = true; | $hasMore = true; | ||||
} | } | ||||
} | } | ||||
$result = $result->map(function ($item) { | $result = $result->map(function ($item) use ($isAdmin) { | ||||
$amount = $item->amount; | $amount = $item->amount; | ||||
if (in_array($item->type, [Transaction::WALLET_PENALTY, Transaction::WALLET_DEBIT])) { | if (in_array($item->type, [Transaction::WALLET_PENALTY, Transaction::WALLET_DEBIT])) { | ||||
$amount *= -1; | $amount *= -1; | ||||
} | } | ||||
return [ | $entry = [ | ||||
'id' => $item->id, | 'id' => $item->id, | ||||
'createdAt' => $item->created_at->format('Y-m-d H:i'), | 'createdAt' => $item->created_at->format('Y-m-d H:i'), | ||||
'type' => $item->type, | 'type' => $item->type, | ||||
'description' => $item->shortDescription(), | 'description' => $item->shortDescription(), | ||||
'amount' => $amount, | 'amount' => $amount, | ||||
'hasDetails' => !empty($item->cnt), | 'hasDetails' => !empty($item->cnt), | ||||
]; | ]; | ||||
if ($isAdmin && $item->user_email) { | |||||
$entry['user'] = $item->user_email; | |||||
} | |||||
return $entry; | |||||
}); | }); | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'list' => $result, | 'list' => $result, | ||||
'count' => count($result), | 'count' => count($result), | ||||
'hasMore' => $hasMore, | 'hasMore' => $hasMore, | ||||
'page' => $page, | 'page' => $page, | ||||
]); | ]); | ||||
} | } | ||||
} | } |