284 lines
11 KiB
PHP
284 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
class LockedAccountController extends Controller
|
|
{
|
|
protected $apiBaseUrl = 'http://192.168.100.6:8081/api';
|
|
|
|
/**
|
|
* Display the main page with locked accounts.
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
try {
|
|
// Force a log to confirm the method is reached
|
|
Log::debug('Entering LockedAccountController index method', ['request' => $request->all()]);
|
|
|
|
$user = Session::get('user');
|
|
$accessToken = $user['access_token'] ?? null;
|
|
|
|
if (!$accessToken) {
|
|
Log::warning('No access token found, redirecting to login from locked-accounts');
|
|
return redirect()->route('login')->with('error', 'Please log in to view locked accounts.');
|
|
}
|
|
|
|
Log::debug('Access token found', ['access_token' => $accessToken]);
|
|
|
|
// Prepare query parameters
|
|
$params = [
|
|
'page' => $request->input('page', 1),
|
|
'page_size' => $request->input('page_size', 5), // Match CardMemberController's default
|
|
'_search' => $request->input('_search', null),
|
|
'status' => $request->input('status', null),
|
|
'_locked' => 1,
|
|
];
|
|
|
|
Log::debug('Making API call to fetch locked accounts', [
|
|
'url' => "{$this->apiBaseUrl}/cms/member",
|
|
'params' => $params,
|
|
]);
|
|
|
|
$response = Http::withHeaders([
|
|
'Accept' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $accessToken,
|
|
])->get("{$this->apiBaseUrl}/cms/member", $params);
|
|
|
|
Log::debug('API response received', [
|
|
'status' => $response->status(),
|
|
'body' => $response->body(),
|
|
]);
|
|
|
|
if ($response->status() === 401 || $response->status() === 403) {
|
|
Log::warning('Unauthorized or Forbidden API response', ['response' => $response->json()]);
|
|
return redirect()->route('login')->with('error', 'Your session has expired. Please log in again.');
|
|
}
|
|
|
|
$json = $response->json();
|
|
Log::info('Locked Accounts API Raw Response', ['response' => $json]);
|
|
|
|
if ($response->successful() && isset($json['data']) && is_array($json['data'])) {
|
|
$accounts = array_map(function ($account) {
|
|
Log::info('Processing locked account record', ['account' => $account]);
|
|
return [
|
|
'id' => $account['lcard_uuid'] ?? null,
|
|
'cardNumber' => $account['card_number'] ?? '',
|
|
'firstName' => $account['firstname'] ?? '',
|
|
'lastName' => $account['lastname'] ?? '',
|
|
'birthday' => $account['birthdate'] ?? '',
|
|
'cardType' => $account['card_type'] ?? '',
|
|
'status' => $account['status'] ? 'Active' : 'Inactive',
|
|
'is_locked' => $account['is_locked'] ?? 1, // Ensure locked status
|
|
];
|
|
}, $json['data']);
|
|
|
|
$total = $json['meta']['total'] ?? count($accounts);
|
|
$lastPage = $json['meta']['last_page'] ?? ceil($total / $params['page_size']);
|
|
} else {
|
|
Log::warning('No locked account data found or invalid API response', ['response' => $json]);
|
|
$accounts = [];
|
|
$total = 0;
|
|
$lastPage = 1;
|
|
}
|
|
|
|
Log::debug('Rendering view with data', [
|
|
'accounts' => $accounts,
|
|
'currentPage' => $params['page'],
|
|
'lastPage' => $lastPage,
|
|
'total' => $total,
|
|
'params' => $params,
|
|
]);
|
|
|
|
return view('pages.member management.locked-accounts', [
|
|
'members' => $accounts, // Match naming convention with CardMemberController
|
|
'currentPage' => $params['page'],
|
|
'lastPage' => $lastPage,
|
|
'total' => $total,
|
|
'search' => $params['_search'],
|
|
'params' => $params, // Include params for view compatibility
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error in LockedAccountController index method', [
|
|
'message' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
return view('pages.member management.locked-accounts', [
|
|
'members' => [],
|
|
'currentPage' => 1,
|
|
'lastPage' => 1,
|
|
'total' => 0,
|
|
'search' => $params['_search'] ?? null,
|
|
'params' => $params ?? [
|
|
'page' => 1,
|
|
'page_size' => 5,
|
|
'_search' => null,
|
|
'status' => null,
|
|
'_locked' => 1,
|
|
],
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Activate a locked account.
|
|
*
|
|
* @param string $uuid
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function activate($uuid)
|
|
{
|
|
try {
|
|
$user = Session::get('user');
|
|
$accessToken = $user['access_token'] ?? null;
|
|
|
|
if (!$accessToken) {
|
|
Log::warning('No access token found, redirecting to login from activate account');
|
|
return redirect()->route('login')->with('error', 'Please log in to activate an account.');
|
|
}
|
|
|
|
Log::debug('Making API call to activate account', [
|
|
'url' => "{$this->apiBaseUrl}/cms/memberActivate/{$uuid}",
|
|
'uuid' => $uuid,
|
|
]);
|
|
|
|
$response = Http::withHeaders([
|
|
'Accept' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $accessToken,
|
|
])->post("{$this->apiBaseUrl}/cms/memberActivate/{$uuid}");
|
|
|
|
Log::debug('Activate API response received', [
|
|
'status' => $response->status(),
|
|
'body' => $response->body(),
|
|
]);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('locked-accounts')->with('success', 'Account activated successfully.');
|
|
} else {
|
|
Log::warning('Failed to activate account', ['uuid' => $uuid, 'response' => $response->json()]);
|
|
return redirect()->route('locked-accounts')->with('error', $response->json()['message'] ?? 'Failed to activate account. Please try again.');
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('Error activating account', [
|
|
'uuid' => $uuid,
|
|
'message' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
return redirect()->route('locked-accounts')->with('error', 'An error occurred while activating the account.');
|
|
}
|
|
}
|
|
|
|
public function show($uuid)
|
|
{
|
|
try {
|
|
// Verify session data
|
|
$user = Session::get('user');
|
|
Log::debug('Session user data', ['user' => $user]);
|
|
|
|
if (!$user || !isset($user['access_token'])) {
|
|
Log::warning('No user or access token found in session', ['uuid' => $uuid]);
|
|
return redirect()->route('login')->with('error', 'Please log in to view the account.');
|
|
}
|
|
|
|
$accessToken = $user['access_token'];
|
|
Log::debug('Access token in show method', ['access_token' => $accessToken]);
|
|
|
|
// Log the API request details
|
|
$apiUrl = "{$this->apiBaseUrl}/cms/member/{$uuid}";
|
|
Log::debug('Making API call to fetch locked account details', [
|
|
'url' => $apiUrl,
|
|
'uuid' => $uuid,
|
|
]);
|
|
|
|
// Make the API call
|
|
$response = Http::withHeaders([
|
|
'Accept' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $accessToken,
|
|
])->timeout(10)->get($apiUrl);
|
|
|
|
// Log the raw response
|
|
Log::debug('Show API response received', [
|
|
'status' => $response->status(),
|
|
'headers' => $response->headers(),
|
|
'body' => $response->body(),
|
|
]);
|
|
|
|
// Handle specific HTTP status codes
|
|
if ($response->status() === 401 || $response->status() === 403) {
|
|
Log::warning('Unauthorized or Forbidden API response', [
|
|
'uuid' => $uuid,
|
|
'response' => $response->json(),
|
|
]);
|
|
return redirect()->route('login')->with('error', 'Your session has expired. Please log in again.');
|
|
}
|
|
|
|
if ($response->status() === 404) {
|
|
Log::warning('Account not found in API', [
|
|
'uuid' => $uuid,
|
|
'response' => $response->json(),
|
|
]);
|
|
return redirect()->route('locked-accounts')->with('error', 'The requested account does not exist.');
|
|
}
|
|
|
|
if (!$response->successful()) {
|
|
Log::error('API request failed', [
|
|
'uuid' => $uuid,
|
|
'status' => $response->status(),
|
|
'response' => $response->json(),
|
|
]);
|
|
return redirect()->route('locked-accounts')->with('error', 'Failed to fetch account data from the server. Status: ' . $response->status());
|
|
}
|
|
|
|
// Parse the response
|
|
$json = $response->json();
|
|
Log::debug('Parsed API response', ['json' => $json]);
|
|
|
|
if (!isset($json['data']) || !is_array($json['data'])) {
|
|
Log::warning('Invalid or missing data in API response', [
|
|
'uuid' => $uuid,
|
|
'response' => $json,
|
|
]);
|
|
return redirect()->route('locked-accounts')->with('error', 'Invalid account data received from the server.');
|
|
}
|
|
|
|
// Map the data
|
|
$account = [
|
|
'id' => $json['data']['lcard_uuid'] ?? null,
|
|
'cardNumber' => $json['data']['card_number'] ?? '',
|
|
'firstName' => $json['data']['firstname'] ?? '',
|
|
'lastName' => $json['data']['lastname'] ?? '',
|
|
'birthday' => $json['data']['birthdate'] ?? '',
|
|
'cardType' => $json['data']['card_type'] ?? '',
|
|
'status' => $json['data']['status'] ? 'Active' : 'Inactive',
|
|
'is_locked' => $json['data']['is_locked'] ?? 1,
|
|
];
|
|
|
|
Log::debug('Mapped account data', ['account' => $account]);
|
|
|
|
return view('pages.locked-account-view', [
|
|
'member' => $account,
|
|
]);
|
|
} catch (\Illuminate\Http\Client\ConnectionException $e) {
|
|
Log::error('Connection error in LockedAccountController show method', [
|
|
'uuid' => $uuid,
|
|
'message' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
return redirect()->route('locked-accounts')->with('error', 'Unable to connect to the server. Please try again later.');
|
|
} catch (\Exception $e) {
|
|
Log::error('Unexpected error in LockedAccountController show method', [
|
|
'uuid' => $uuid,
|
|
'message' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
return redirect()->route('locked-accounts')->with('error', 'An unexpected error occurred while fetching the account: ' . $e->getMessage());
|
|
}
|
|
}
|
|
} |