fix filter cabang by role
This commit is contained in:
@@ -23,10 +23,44 @@ class CabangController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCabangByRegion($regionCode)
|
public function getCabangByRegion($regionCode = null)
|
||||||
{
|
{
|
||||||
$region_id = Region::where('code', $regionCode)->first()->id;
|
// Get the authenticated user's role
|
||||||
$cabang = Cabang::where('region_id', $region_id)->get(['code', 'name']);
|
$role = auth()->user()->getRoleNames()->first(); // Assuming you're using Spatie's Laravel-permission package
|
||||||
|
|
||||||
|
$cabangQuery = Cabang::query(); // Start a new query builder instance
|
||||||
|
|
||||||
|
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
||||||
|
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
||||||
|
if ($region_id) {
|
||||||
|
$cabangQuery->where('region_id', $region_id);
|
||||||
|
} else {
|
||||||
|
// If no region is found for the user, return an empty array
|
||||||
|
return response()->json([]);
|
||||||
|
}
|
||||||
|
} elseif ($role == 'Area Manager Cabang') {
|
||||||
|
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
||||||
|
if ($cabang_id) {
|
||||||
|
$cabangQuery->where('id', $cabang_id);
|
||||||
|
} else {
|
||||||
|
// If no cabang is found for the user, return an empty array
|
||||||
|
return response()->json([]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If $regionCode is provided, filter by it for other roles or if no specific role-based filtering applies
|
||||||
|
if ($regionCode) {
|
||||||
|
$region = Region::where('code', $regionCode)->first();
|
||||||
|
if ($region) {
|
||||||
|
$cabangQuery->where('region_id', $region->id);
|
||||||
|
} else {
|
||||||
|
// If region code is provided but not found, return empty
|
||||||
|
return response()->json([]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If no regionCode and no specific role, all cabangs will be returned (as no `where` clause is added)
|
||||||
|
}
|
||||||
|
|
||||||
|
$cabang = $cabangQuery->get(['code', 'name']);
|
||||||
return response()->json($cabang);
|
return response()->json($cabang);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user