Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions app/Events/ProsedurChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* File ini bagian dari:
*
* OpenDK
*
* Aplikasi dan source code ini dirilis berdasarkan lisensi GPL V3
*
* Hak Cipta 2017 - 2025 Perkumpulan Desa Digital Terbuka (https://opendesa.id)
*
* Dengan ini diberikan izin, secara gratis, kepada siapa pun yang mendapatkan salinan
* dari perangkat lunak ini dan file dokumentasi terkait ("Aplikasi Ini"), untuk diperlakukan
* tanpa batasan, termasuk hak untuk menggunakan, menyalin, mengubah dan/atau mendistribusikan,
* asal tunduk pada syarat berikut:
*
* Pemberitahuan hak cipta di atas dan pemberitahuan izin ini harus disertakan dalam
* setiap salinan atau bagian penting Aplikasi Ini. Barang siapa yang menghapus atau menghilangkan
* pemberitahuan ini melanggar ketentuan lisensi Aplikasi Ini.
*
* PERANGKAT LUNAK INI DISEDIAKAN "SEBAGAIMANA ADANYA", TANPA JAMINAN APA PUN, BAIK TERSURAT MAUPUN
* TERSIRAT. PENULIS ATAU PEMEGANG HAK CIPTA SAMA SEKALI TIDAK BERTANGGUNG JAWAB ATAS KLAIM, KERUSAKAN ATAU
* KEWAJIBAN APAPUN ATAS PENGGUNAAN ATAU LAINNYA TERKAIT APLIKASI INI.
*
* @package OpenDK
* @author Tim Pengembang OpenDesa
* @copyright Hak Cipta 2017 - 2025 Perkumpulan Desa Digital Terbuka (https://opendesa.id)
* @license http://www.gnu.org/licenses/gpl.html GPL V3
* @link https://github.com/OpenSID/opendk
*/

namespace App\Events;

use App\Models\Prosedur;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ProsedurChanged
{
use Dispatchable;
use SerializesModels;

public Prosedur $prosedur;

/**
* Create a new event instance.
*
* @param Prosedur $prosedur
* @return void
*/
public function __construct(Prosedur $prosedur)
{
$this->prosedur = $prosedur;
}
}
57 changes: 57 additions & 0 deletions app/Listeners/ClearProsedurCacheListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* File ini bagian dari:
*
* OpenDK
*
* Aplikasi dan source code ini dirilis berdasarkan lisensi GPL V3
*
* Hak Cipta 2017 - 2025 Perkumpulan Desa Digital Terbuka (https://opendesa.id)
*
* Dengan ini diberikan izin, secara gratis, kepada siapa pun yang mendapatkan salinan
* dari perangkat lunak ini dan file dokumentasi terkait ("Aplikasi Ini"), untuk diperlakukan
* tanpa batasan, termasuk hak untuk menggunakan, menyalin, mengubah dan/atau mendistribusikan,
* asal tunduk pada syarat berikut:
*
* Pemberitahuan hak cipta di atas dan pemberitahuan izin ini harus disertakan dalam
* setiap salinan atau bagian penting Aplikasi Ini. Barang siapa yang menghapus atau menghilangkan
* pemberitahuan ini melanggar ketentuan lisensi Aplikasi Ini.
*
* PERANGKAT LUNAK INI DISEDIAKAN "SEBAGAIMANA ADANYA", TANPA JAMINAN APA PUN, BAIK TERSURAT MAUPUN
* TERSIRAT. PENULIS ATAU PEMEGANG HAK CIPTA SAMA SEKALI TIDAK BERTANGGUNG JAWAB ATAS KLAIM, KERUSAKAN ATAU
* KEWAJIBAN APAPUN ATAS PENGGUNAAN ATAU LAINNYA TERKAIT APLIKASI INI.
*
* @package OpenDK
* @author Tim Pengembang OpenDesa
* @copyright Hak Cipta 2017 - 2025 Perkumpulan Desa Digital Terbuka (https://opendesa.id)
* @license http://www.gnu.org/licenses/gpl.html GPL V3
* @link https://github.com/OpenSID/opendk
*/

namespace App\Listeners;

use App\Events\ProsedurChanged;
use App\Services\CacheService;
use Illuminate\Support\Facades\Log;

class ClearProsedurCacheListener
{
/**
* Handle the event.
*
* @return void
*/
public function handle(ProsedurChanged $event)
{
try {
$cacheService = app(CacheService::class);
$prefix = config('theme-api.prosedur.cache_prefix', 'prosedur:api');
$cacheService->removeCachePrefix($prefix);
} catch (\Exception $e) {
Log::error('Exception occurred while clearing prosedur cache', [
'message' => $e->getMessage(),
]);
}
}
}
11 changes: 11 additions & 0 deletions app/Models/Prosedur.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

namespace App\Models;

use App\Events\ProsedurChanged;
use App\Traits\HandlesResourceDeletion;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
Expand All @@ -44,6 +45,16 @@ class Prosedur extends Model

protected $table = 'das_prosedur';

/**
* Register model lifecycle hooks.
*/
protected static function booted(): void
{
static::created(fn (Prosedur $prosedur) => ProsedurChanged::dispatch($prosedur));
static::updated(fn (Prosedur $prosedur) => ProsedurChanged::dispatch($prosedur));
static::deleted(fn (Prosedur $prosedur) => ProsedurChanged::dispatch($prosedur));
}

protected $fillable = [
'judul_prosedur',
'file_prosedur',
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/Listeners/ClearProsedurCacheListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

use App\Events\ProsedurChanged;
use App\Listeners\ClearProsedurCacheListener;
use App\Models\Prosedur;
use App\Services\CacheService;

it('clears prosedur cache when event is dispatched', function () {
$cacheService = Mockery::mock(CacheService::class);
$cacheService->shouldReceive('removeCachePrefix')
->once()
->with('prosedur:api')
->andReturnTrue();

$this->app->instance(CacheService::class, $cacheService);

$listener = new ClearProsedurCacheListener();
$event = new ProsedurChanged(new Prosedur());

$listener->handle($event);
});

it('reads correct cache prefix from config', function () {
config(['theme-api.prosedur.cache_prefix' => 'custom:prosedur']);

$cacheService = Mockery::mock(CacheService::class);
$cacheService->shouldReceive('removeCachePrefix')
->once()
->with('custom:prosedur')
->andReturnTrue();

$this->app->instance(CacheService::class, $cacheService);

$listener = new ClearProsedurCacheListener();
$event = new ProsedurChanged(new Prosedur());

$listener->handle($event);
});

it('handles exception gracefully', function () {
$cacheService = Mockery::mock(CacheService::class);
$cacheService->shouldReceive('removeCachePrefix')
->once()
->andThrow(new \Exception('Cache error'));

$this->app->instance(CacheService::class, $cacheService);

\Illuminate\Support\Facades\Log::shouldReceive('error')->once();

$listener = new ClearProsedurCacheListener();
$event = new ProsedurChanged(new Prosedur());

// Should not throw exception
$listener->handle($event);
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
$(document).ready(function() {
var table = $('#dokumen-table').DataTable({
processing: true,
serverSide: false,
serverSide: true,
ajax: {
url: '{!! $urlApi !!}/form-dokumen',
cache: false,
dataSrc: 'data',
dataSrc: function(json) {
json.recordsTotal = json.meta?.pagination?.total ?? 0;
json.recordsFiltered = json.meta?.pagination?.total ?? 0;
return json.data;
},
data: function(d) {
// Convert DataTables parameters to API format (use safe defaults to avoid NaN)
var start = (typeof d.start !== 'undefined' && d.start !== null) ? d.start : 0;
var length = (typeof d.length !== 'undefined' && d.length) ? d.length : (typeof d.pageLength !== 'undefined' ? d.pageLength : 10);
var length = (typeof d.length !== 'undefined' && d.length) ? d.length : 10;
var pageNumber = 1;
if (length && !isNaN(length)) {
pageNumber = Math.floor(start / length) + 1;
Expand All @@ -52,6 +55,8 @@
};
}
},
pageLength: 10,
lengthMenu: [5, 10, 25, 50, 100],
columns: [{
data: null,
name: 'aksi',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
$(document).ready(function() {
var table = $('#prosedur-table').DataTable({
processing: true,
serverSide: false,
serverSide: true,
ajax: {
url: '{!! $urlApi !!}/prosedur',
cache: false,
dataSrc: 'data',
dataSrc: function(json) {
json.recordsTotal = json.meta?.pagination?.total ?? 0;
json.recordsFiltered = json.meta?.pagination?.total ?? 0;
return json.data;
},
data: function(d) {
// Convert DataTables parameters to API format (use safe defaults to avoid NaN)
var start = (typeof d.start !== 'undefined' && d.start !== null) ? d.start : 0;
var length = (typeof d.length !== 'undefined' && d.length) ? d.length : (typeof d.pageLength !== 'undefined' ? d.pageLength : 10);
var length = (typeof d.length !== 'undefined' && d.length) ? d.length : 10;
var pageNumber = 1;
if (length && !isNaN(length)) {
pageNumber = Math.floor(start / length) + 1;
Expand All @@ -50,6 +53,8 @@
};
}
},
pageLength: 10,
lengthMenu: [5, 10, 25, 50, 100],
columns: [{
data: 'attributes.judul_prosedur',
name: 'judul_prosedur',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
$(document).ready(function() {
var table = $('#regulasi-table').DataTable({
processing: true,
serverSide: false,
serverSide: true,
ajax: {
url: '{!! $urlApi !!}/regulasi',
cache: false,
dataSrc: 'data',
dataSrc: function(json) {
json.recordsTotal = json.meta?.pagination?.total ?? 0;
json.recordsFiltered = json.meta?.pagination?.total ?? 0;
return json.data;
},
data: function(d) {
// Convert DataTables parameters to API format (use safe defaults to avoid NaN)
var start = (typeof d.start !== 'undefined' && d.start !== null) ? d.start : 0;
var length = (typeof d.length !== 'undefined' && d.length) ? d.length : (typeof d.pageLength !== 'undefined' ? d.pageLength : 10);
var length = (typeof d.length !== 'undefined' && d.length) ? d.length : 10;
var pageNumber = 1;
if (length && !isNaN(length)) {
pageNumber = Math.floor(start / length) + 1;
Expand All @@ -50,6 +53,8 @@
};
}
},
pageLength: 10,
lengthMenu: [5, 10, 25, 50, 100],
columns: [{
data: 'attributes.judul',
name: 'judul',
Expand Down
Loading