175 lines
5.7 KiB
PHP
175 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\Api\ApiService;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CardMemberController extends Controller
|
|
{
|
|
protected ApiService $apiService;
|
|
|
|
public function __construct(ApiService $apiService)
|
|
{
|
|
$this->apiService = $apiService;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
try {
|
|
$response = $this->apiService->get('/card-members');
|
|
|
|
if ($response->successful()) {
|
|
return view('pages.member management.card-member', [
|
|
'members' => $response->json()['data']
|
|
]);
|
|
}
|
|
|
|
return back()->with('error', 'Unable to fetch card members.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'card_number' => 'required|string|unique:card_members,card_number',
|
|
'member_type' => 'required|string',
|
|
'first_name' => 'required|string|max:255',
|
|
'last_name' => 'required|string|max:255',
|
|
'email' => 'required|email|unique:card_members,email',
|
|
'phone' => 'required|string',
|
|
'birth_date' => 'required|date',
|
|
'address' => 'required|string',
|
|
'city' => 'required|string',
|
|
'state' => 'required|string',
|
|
'postal_code' => 'required|string',
|
|
'status' => 'required|string|in:active,inactive,locked'
|
|
]);
|
|
|
|
try {
|
|
$response = $this->apiService->post('/card-members', $validated);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('card-member')
|
|
->with('success', 'Card member created successfully.');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors']);
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Unable to create card member.');
|
|
}
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$validated = $request->validate([
|
|
'member_type' => 'required|string',
|
|
'first_name' => 'required|string|max:255',
|
|
'last_name' => 'required|string|max:255',
|
|
'email' => 'required|email|unique:card_members,email,'.$id,
|
|
'phone' => 'required|string',
|
|
'birth_date' => 'required|date',
|
|
'address' => 'required|string',
|
|
'city' => 'required|string',
|
|
'state' => 'required|string',
|
|
'postal_code' => 'required|string',
|
|
'status' => 'required|string|in:active,inactive,locked'
|
|
]);
|
|
|
|
try {
|
|
$response = $this->apiService->put("/card-members/{$id}", $validated);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('card-member')
|
|
->with('success', 'Card member updated successfully.');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors']);
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Unable to update card member.');
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$response = $this->apiService->delete("/card-members/{$id}");
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('card-member')
|
|
->with('success', 'Card member deleted successfully.');
|
|
}
|
|
|
|
return back()->with('error', 'Unable to delete card member.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function lockAccount($id)
|
|
{
|
|
try {
|
|
$response = $this->apiService->post("/card-members/{$id}/lock");
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('card-member')
|
|
->with('success', 'Account locked successfully.');
|
|
}
|
|
|
|
return back()->with('error', 'Unable to lock account.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function unlockAccount($id)
|
|
{
|
|
try {
|
|
$response = $this->apiService->post("/card-members/{$id}/unlock");
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('card-member')
|
|
->with('success', 'Account unlocked successfully.');
|
|
}
|
|
|
|
return back()->with('error', 'Unable to unlock account.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function getLockedAccounts()
|
|
{
|
|
try {
|
|
$response = $this->apiService->get('/card-members/locked');
|
|
|
|
if ($response->successful()) {
|
|
return view('pages.member management.locked-accounts', [
|
|
'lockedAccounts' => $response->json()['data']
|
|
]);
|
|
}
|
|
|
|
return back()->with('error', 'Unable to fetch locked accounts.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function getTransactionHistory($id)
|
|
{
|
|
try {
|
|
$response = $this->apiService->get("/card-members/{$id}/transactions");
|
|
|
|
if ($response->successful()) {
|
|
return response()->json($response->json());
|
|
}
|
|
|
|
return response()->json(['error' => 'Unable to fetch transaction history'], 400);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => 'Service unavailable'], 503);
|
|
}
|
|
}
|
|
}
|