clqms-be/app/Database/Seeds/AreaGeoSeeder.php
mahdahar e36e390f71 refactor: consolidate ValueSet API and simplify seeders
- Consolidate ValueSet routes from multiple nested groups to flat structure
- Delete deprecated ValueSet\ namespaced controllers (ValueSetController, ValueSetDefController)
- Remove ValueSetSeeder and ValueSetCountrySeeder from DBSeeder
- Simplify seeders (LocationSeeder, OrganizationSeeder, PatientSeeder, TestSeeder)
  to use literal string values instead of ValueSet lookups
- Add new ValueSetController and ValueSetDefController in root namespace
- Update test files for new controller structure
The key changes are:
1. Routes: Consolidated from nested ValueSet\ namespace routes to flat ValueSetController routes with /items sub-endpoints
2. Controllers: Deleted old app/Controllers/ValueSet/ directory, created new root-level controllers
3. Seeders: Removed ValueSet dependencies, using literal values like 'ROOM', '1', 'TEST' instead of [12]['ROOM'] etc.
4. Tests: Updated tests to match new controller structure
2026-01-13 16:48:43 +07:00

112 lines
3.1 KiB
PHP

<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
use CodeIgniter\HTTP\CURLRequest;
use Config\Services;
class AreaGeoSeeder extends Seeder
{
/**
* API configuration for fetching zones data
*/
protected string $apiUrl = 'https://services-summit.my.id/api/zones'; //DEV
//protected string $apiUrl = 'http://services-summit.my.id/api/zones'; //PROD
// public function run()
// {
// // Fetch data from external API
// $options = [
// 'baseURI' => $this->apiUrl,
// 'timeout' => 30,
// ];
// // $client = new CURLRequest($options);
// $client = Services::curlrequest([
// 'baseURI' => $this->apiUrl,
// 'timeout' => 30,
// ]);
// $response = $client->get('');
// if ($response->getStatusCode() !== 200) {
// echo "Failed to fetch data from API. Status: " . $response->getStatusCode() . "\n";
// return;
// }
// $externalData = $response->getJSON(true);
// if (empty($externalData)) {
// echo "No data found from API.\n";
// return;
// }
// // Prepare data for insertion (exclude AreaGeoID to allow auto-increment)
// $data = [];
// foreach ($externalData as $row) {
// $data[] = [
// 'AreaCode' => $row['zonecode'] ?? null,
// 'Class' => $row['zoneclass'] ?? null,
// 'AreaName' => str_replace('_', ' ', $row['zonename'] ?? ''),
// 'Parent' => $row['parentzoneid'] ?? null,
// ];
// }
// // Insert into local database
// $this->db->table('areageo')->insertBatch($data);
// echo "Successfully seeded " . count($data) . " area geo records.\n";
// }
public function run()
{
$client = Services::curlrequest([
'baseURI' => $this->apiUrl,
'timeout' => 30,
]);
$response = $client->get('');
if ($response->getStatusCode() !== 200) {
echo "HTTP Error: {$response->getStatusCode()}" . PHP_EOL;
return;
}
$payload = json_decode($response->getBody(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "Invalid JSON response: " . json_last_error_msg() . PHP_EOL;
return;
}
if (
!isset($payload['status'], $payload['data']) ||
$payload['status'] !== 'success'
) {
echo "Unexpected API structure." . PHP_EOL;
return;
}
$zones = $payload['data'];
if (empty($zones)) {
echo "No zone data found." . PHP_EOL;
return;
}
$data = [];
foreach ($zones as $row) {
$data[] = [
'AreaCode' => $row['zonecode'] ?? null,
'Class' => $row['zoneclass'] ?? null,
'AreaName' => str_replace('_', ' ', $row['zonename'] ?? ''),
'Parent' => $row['parentzoneid'],
];
}
$this->db->table('areageo')->insertBatch($data);
echo "Seeded " . count($data) . " records." . PHP_EOL;
}
}