Skip to content

Commit

Permalink
Merge branch '1607-transaction-list---change-date-display-and-add-col…
Browse files Browse the repository at this point in the history
…umn-sorting' into staging
  • Loading branch information
PG-Momik committed Nov 25, 2024
2 parents c784b96 + 23fca76 commit 10b4ed1
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 30 deletions.
20 changes: 20 additions & 0 deletions app/Helpers/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,26 @@ function getTableConfig($module): array
],
'user' => ['orderBy' => ['username', 'publisher_name', 'created_at', 'organisation', 'admin', 'general', 'active', 'deactivated', 'total', 'last_logged_in'], 'direction' => ['asc', 'desc']],
'audit' => ['orderBy' => ['user_id', 'user_type', 'event', 'auditable_type', 'created_at'], 'direction' => ['asc', 'desc']],
'transaction' => [
'orderBy' => ['type', 'value', 'date'],
'direction' => ['asc', 'desc'],
'filterBy' => [
'all',
'incoming_funds',
'outgoing_commitment',
'disbursement',
'expenditure',
'interest_payment',
'loan_repayment',
'reimbursement',
'purchase_of_equity',
'sale_of_equity',
'credit_guarantee',
'incoming_commitment',
'outgoing_pledge',
'incoming_pledge',
],
],
];

return $tableConfig[$module];
Expand Down
50 changes: 48 additions & 2 deletions app/Http/Controllers/Admin/Activity/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,66 @@ public function index($activityId): Factory|View|RedirectResponse|Application
public function getPaginatedTransactions(int $activityId, int $page = 1): JsonResponse
{
try {
$transaction = $this->transactionService->getPaginatedTransaction($activityId, $page);
$transaction = $this->transactionService->getPaginatedTransaction($activityId, $page, $this->sanitizeRequest(request()));

return response()->json([
'success' => true,
'message' => 'Transactions fetched successfully',
'data' => $transaction,
]);
} catch (Exception $e) {
logger()->error($e->getMessage());
logger()->error($e);

return response()->json(['success' => false, 'message' => 'Error occurred while fetching the data']);
}
}

public function sanitizeRequest(\Illuminate\Http\Request $request): array
{
$tableConfig = getTableConfig('transaction');
$transactionTypeMap = [
'all' => 'all',
'incoming_funds' => 1,
'outgoing_commitment' => 2,
'disbursement' => 3,
'expenditure' => 4,
'interest_payment' => 5,
'loan_repayment' => 6,
'reimbursement' => 7,
'purchase_of_equity' => 8,
'sale_of_equity' => 9,
'credit_guarantee' => 10,
'incoming_commitment' => 11,
'outgoing_pledge' => 12,
'incoming_pledge' => 13,
];

$queryParams = [];

if (!empty($request->get('q')) || $request->get('q') === '0') {
$queryParams['query'] = $request->get('q');
}

if (!empty($request->get('limit'))) {
$queryParams['limit'] = $request->get('limit') ?? 10;
}

if (in_array($request->get('orderBy'), $tableConfig['orderBy'], true)) {
$queryParams['orderBy'] = $request->get('orderBy') ?? '';

if (in_array($request->get('direction'), $tableConfig['direction'], true)) {
$queryParams['direction'] = $request->get('direction') ?? 'asc';
}
}

if (in_array($request->get('filterBy'), $tableConfig['filterBy'], true)) {
$queryParams['filterBy'] = $request->get('filterBy');
$queryParams['filterBy'] = Arr::get($transactionTypeMap, $queryParams['filterBy'], 'all');
}

return $queryParams;
}

/**
* Show the form for creating a new resource.
*
Expand Down
30 changes: 26 additions & 4 deletions app/IATI/Repositories/Activity/TransactionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\IATI\Repositories\Repository;
use App\IATI\Traits\FillDefaultValuesTrait;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;

/**
* Class TransactionRepository.
Expand All @@ -27,14 +28,35 @@ public function getModel(): string
/**
* Returns paginated transactions.
*
* @param int $activityId
* @param int $page
* @param int $activityId
* @param array $queryParams
* @param int $page
*
* @return LengthAwarePaginator
*/
public function getPaginatedTransaction(int $activityId, int $page = 1): LengthAwarePaginator
public function getPaginatedTransaction(int $activityId, array $queryParams, int $page = 1): LengthAwarePaginator
{
return $this->model->where('activity_id', $activityId)->orderBy('created_at', 'DESC')->paginate(10, ['*'], 'transaction', $page);
$query = $this->model->where('activity_id', $activityId);

$filterApplied = Arr::get($queryParams, 'filterBy', 'all');
$orderBy = Arr::get($queryParams, 'orderBy', 'created_at');
$direction = strtoupper(Arr::get($queryParams, 'direction', 'ASC'));
$limit = Arr::get($queryParams, 'limit', 10);

$query = $query->when($filterApplied != 'all', function ($query) use ($filterApplied) {
return $query->whereRaw("(transaction->'transaction_type'->0->>'transaction_type_code') = '$filterApplied'");
});

$query = $query->when(true, function ($query) use ($orderBy, $direction) {
return match ($orderBy) {
'type' => $query->orderByRaw("(transaction->'transaction_type'->0->>'transaction_type_code')::INTEGER $direction, created_at DESC"),
'value' => $query->orderByRaw("(transaction->'value'->0->>'amount')::NUMERIC $direction, created_at DESC"),
'date' => $query->orderByRaw("(transaction->'transaction_date'->0->>'date')::DATE $direction, created_at DESC"),
default => $query->orderByRaw('created_at DESC'),
};
});

return $query->paginate($limit, ['*'], 'transaction', $page);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions app/IATI/Services/Activity/TransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ public function __construct(
}

/**
* @param int $activityId
* @param int $page
* @param int $activityId
* @param int $page
* @param array $queryParams
*
* @return LengthAwarePaginator|Collection
*/
public function getPaginatedTransaction(int $activityId, int $page): LengthAwarePaginator|Collection
public function getPaginatedTransaction(int $activityId, int $page, array $queryParams): LengthAwarePaginator|Collection
{
return $this->transactionRepository->getPaginatedTransaction($activityId, $page);
return $this->transactionRepository->getPaginatedTransaction($activityId, $queryParams, $page);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions public/css/app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions public/css/webportal-app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions resources/assets/js/components/FilteringPills.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<template>
<div class="my-4 flex flex-wrap items-center gap-2">
<div
v-for="(item, index) in pills"
:key="index"
class="tooltip-btn"
:class="active === item.title ? 'active' : ''"
>
<button @click="filterBy(item.title, item.searchTerm)">
<span>{{ item.title }}</span>
</button>
</div>
</div>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits } from 'vue';
const props = defineProps({
pills: {
type: Object,
required: true,
},
});
const emit = defineEmits(['filterBy']);
const active = ref(props.pills[0].title || '');
const filterBy = (title: string, code: string) => {
active.value = title;
emit('filterBy', code);
};
</script>
5 changes: 5 additions & 0 deletions resources/assets/js/composable/dateFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ function dateFormat(date: Date, format = 'MMMM DD, YYYY' as string) {
case 'fromNow':
format_date = date ? moment(date).fromNow() : '';
break;

case 'calendar':
format_date = date ? moment(date).calendar() : '';
break;

default:
format_date = date ? moment(date).format(format) : '';
}
Expand Down
Loading

0 comments on commit 10b4ed1

Please sign in to comment.