diff --git a/app/Database/Seeds/AreaGeoSeeder.php b/app/Database/Seeds/AreaGeoSeeder.php index c7cdf38..7f65ffd 100644 --- a/app/Database/Seeds/AreaGeoSeeder.php +++ b/app/Database/Seeds/AreaGeoSeeder.php @@ -4,52 +4,108 @@ 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://your-api-domain.com/api/zones'; + // 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() { - // Fetch data from external API - $options = [ - 'baseURI' => $this->apiUrl, - 'timeout' => 30, - ]; + $client = Services::curlrequest([ + 'baseURI' => $this->apiUrl, + 'timeout' => 30, + ]); - $client = new CURLRequest($options); + $response = $client->get(''); - $response = $client->get(''); + if ($response->getStatusCode() !== 200) { + echo "HTTP Error: {$response->getStatusCode()}" . PHP_EOL; + return; + } - if ($response->getStatusCode() !== 200) { - echo "Failed to fetch data from API. Status: " . $response->getStatusCode() . "\n"; - return; - } + $payload = json_decode($response->getBody(), true); - $externalData = $response->getJSON(true); + if (json_last_error() !== JSON_ERROR_NONE) { + echo "Invalid JSON response: " . json_last_error_msg() . PHP_EOL; + return; + } - if (empty($externalData)) { - echo "No data found from API.\n"; - return; - } + if ( + !isset($payload['status'], $payload['data']) || + $payload['status'] !== 'success' + ) { + echo "Unexpected API structure." . PHP_EOL; + 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, - ]; - } + $zones = $payload['data']; - // Insert into local database - $this->db->table('areageo')->insertBatch($data); + if (empty($zones)) { + echo "No zone data found." . PHP_EOL; + return; + } - echo "Successfully seeded " . count($data) . " area geo records.\n"; + $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; } } diff --git a/app/Database/Seeds/ContactSeeder.php b/app/Database/Seeds/ContactSeeder.php index ab069ab..b048103 100644 --- a/app/Database/Seeds/ContactSeeder.php +++ b/app/Database/Seeds/ContactSeeder.php @@ -13,11 +13,11 @@ class ContactSeeder extends Seeder { $data = [ [ 'ContactID' => 1, 'NameFirst' => 'Default', 'NameLast' => 'Doctor', 'Title' => '', 'Initial' => 'DEFDOC', - 'Birthdate' => '', 'EmailAddress1' => '', 'EmailAddress2' => '', 'Phone' => '', 'MobilePhone1' => '', 'MobilePhone2' => '', 'Specialty' => '', 'SubSpecialty' => '', 'CreateDate' => "$now" + 'Birthdate' => '', 'EmailAddress1' => 'd1@as.com', 'EmailAddress2' => '', 'Phone' => '', 'MobilePhone1' => '', 'MobilePhone2' => '', 'Specialty' => '', 'SubSpecialty' => '', 'CreateDate' => "$now" ], [ 'ContactID' => 2, 'NameFirst' => 'Dummy', 'NameLast' => 'Doctor', 'Title' => '', 'Initial' => 'QDOC', - 'Birthdate' => '', 'EmailAddress1' => '', 'EmailAddress2' => '', 'Phone' => '', 'MobilePhone1' => '', 'MobilePhone2' => '', 'Specialty' => '', 'SubSpecialty' => '', 'CreateDate' => "$now" + 'Birthdate' => '', 'EmailAddress1' => 'd2a@gmail.com', 'EmailAddress2' => '', 'Phone' => '', 'MobilePhone1' => '', 'MobilePhone2' => '', 'Specialty' => '', 'SubSpecialty' => '', 'CreateDate' => "$now" ] ]; $this->db->table('contact')->insertBatch($data);