Update perbaikan ContactSeeder dan AreaGeoSeeder

This commit is contained in:
mikael-zakaria 2026-01-08 10:49:19 +07:00
parent c8e18ed283
commit d4029dce38
2 changed files with 88 additions and 32 deletions

View File

@ -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 = [
$client = Services::curlrequest([
'baseURI' => $this->apiUrl,
'timeout' => 30,
];
$client = new CURLRequest($options);
]);
$response = $client->get('');
if ($response->getStatusCode() !== 200) {
echo "Failed to fetch data from API. Status: " . $response->getStatusCode() . "\n";
echo "HTTP Error: {$response->getStatusCode()}" . PHP_EOL;
return;
}
$externalData = $response->getJSON(true);
$payload = json_decode($response->getBody(), true);
if (empty($externalData)) {
echo "No data found from API.\n";
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;
}
// Prepare data for insertion (exclude AreaGeoID to allow auto-increment)
$data = [];
foreach ($externalData as $row) {
foreach ($zones as $row) {
$data[] = [
'AreaCode' => $row['zonecode'] ?? null,
'Class' => $row['zoneclass'] ?? null,
'AreaName' => str_replace('_', ' ', $row['zonename'] ?? ''),
'Parent' => $row['parentzoneid'] ?? null,
'Parent' => $row['parentzoneid'],
];
}
// Insert into local database
$this->db->table('areageo')->insertBatch($data);
echo "Successfully seeded " . count($data) . " area geo records.\n";
echo "Seeded " . count($data) . " records." . PHP_EOL;
}
}

View File

@ -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);