Update perbaikan ContactSeeder dan AreaGeoSeeder
This commit is contained in:
parent
c8e18ed283
commit
d4029dce38
@ -4,52 +4,108 @@ namespace App\Database\Seeds;
|
|||||||
|
|
||||||
use CodeIgniter\Database\Seeder;
|
use CodeIgniter\Database\Seeder;
|
||||||
use CodeIgniter\HTTP\CURLRequest;
|
use CodeIgniter\HTTP\CURLRequest;
|
||||||
|
use Config\Services;
|
||||||
|
|
||||||
class AreaGeoSeeder extends Seeder
|
class AreaGeoSeeder extends Seeder
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* API configuration for fetching zones data
|
* 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()
|
public function run()
|
||||||
{
|
{
|
||||||
// Fetch data from external API
|
$client = Services::curlrequest([
|
||||||
$options = [
|
'baseURI' => $this->apiUrl,
|
||||||
'baseURI' => $this->apiUrl,
|
'timeout' => 30,
|
||||||
'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) {
|
$payload = json_decode($response->getBody(), true);
|
||||||
echo "Failed to fetch data from API. Status: " . $response->getStatusCode() . "\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$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)) {
|
if (
|
||||||
echo "No data found from API.\n";
|
!isset($payload['status'], $payload['data']) ||
|
||||||
return;
|
$payload['status'] !== 'success'
|
||||||
}
|
) {
|
||||||
|
echo "Unexpected API structure." . PHP_EOL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare data for insertion (exclude AreaGeoID to allow auto-increment)
|
$zones = $payload['data'];
|
||||||
$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
|
if (empty($zones)) {
|
||||||
$this->db->table('areageo')->insertBatch($data);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,11 +13,11 @@ class ContactSeeder extends Seeder {
|
|||||||
$data = [
|
$data = [
|
||||||
[
|
[
|
||||||
'ContactID' => 1, 'NameFirst' => 'Default', 'NameLast' => 'Doctor', 'Title' => '', 'Initial' => 'DEFDOC',
|
'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',
|
'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);
|
$this->db->table('contact')->insertBatch($data);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user