diff --git a/web/app/Models/PhyreServer.php b/web/app/Models/PhyreServer.php index 9e6fea0..b68af9c 100644 --- a/web/app/Models/PhyreServer.php +++ b/web/app/Models/PhyreServer.php @@ -33,22 +33,77 @@ class PhyreServer extends Model public function syncResources() { // Sync customers - $customerExternalIds = []; - $getCustomers = Customer::where('phyre_server_id', $this->id)->get(); - if ($getCustomers->count() > 0) { - foreach ($getCustomers as $customer) { - $customerExternalIds[] = $customer->external_id; + $centralServerCustomerExternalIds = []; + $getCentralServerCustomers = Customer::where('phyre_server_id', $this->id)->get(); + if ($getCentralServerCustomers->count() > 0) { + foreach ($getCentralServerCustomers as $customer) { + $centralServerCustomerExternalIds[] = $customer->external_id; } } + $phyreApiSDK = new PhyreApiSDK($this->ip, 8443, $this->username, $this->password); - $getExternalCustomers = $phyreApiSDK->getCustomers(); - if (isset($getExternalCustomers['data']['customers'])) { - $externalCustomerIds = []; - foreach ($getExternalCustomers['data']['customers'] as $externalCustomer) { - $externalCustomerIds[] = $externalCustomer['id']; + $getPhyreServerCustomers = $phyreApiSDK->getCustomers(); + if (isset($getPhyreServerCustomers['data']['customers'])) { + $phyreServerCustomerIds = []; + foreach ($getPhyreServerCustomers['data']['customers'] as $customer) { + $phyreServerCustomerIds[] = $customer['id']; } // Delete customers to main server that are not in external server + foreach ($centralServerCustomerExternalIds as $centralServerCustomerExternalId) { + if (!in_array($centralServerCustomerExternalId, $phyreServerCustomerIds)) { + $getCustomer = Customer::where('external_id', $centralServerCustomerExternalId) + ->where('phyre_server_id', $this->id) + ->first(); + if ($getCustomer) { + $getCustomer->delete(); + } + } + } + + // Add customers to main server from external server + foreach ($getPhyreServerCustomers['data']['customers'] as $phyreServerCustomer) { + $findCustomer = Customer::where('external_id', $phyreServerCustomer['id']) + ->where('phyre_server_id', $this->id) + ->first(); + if (!$findCustomer) { + $findCustomer = new Customer(); + $findCustomer->phyre_server_id = $this->id; + $findCustomer->external_id = $phyreServerCustomer['id']; + } + $findCustomer->name = $phyreServerCustomer['name']; + $findCustomer->username = $phyreServerCustomer['username']; + $findCustomer->email = $phyreServerCustomer['email']; + $findCustomer->phone = $phyreServerCustomer['phone']; + $findCustomer->address = $phyreServerCustomer['address']; + $findCustomer->city = $phyreServerCustomer['city']; + $findCustomer->state = $phyreServerCustomer['state']; + $findCustomer->zip = $phyreServerCustomer['zip']; + $findCustomer->country = $phyreServerCustomer['country']; + $findCustomer->company = $phyreServerCustomer['company']; + $findCustomer->saveQuietly(); + } + } + + // Sync Hosting Subscriptions + $centralServerHostingSubscriptionsExternalIds = []; + $getCentralHostingSubscriptions = HostingSubscription::where('phyre_server_id', $this->id)->get(); + if ($getCentralHostingSubscriptions->count() > 0) { + foreach ($getCentralHostingSubscriptions as $customer) { + $centralServerHostingSubscriptionsExternalIds[] = $customer->external_id; + } + } + // $getPhyreServerHostingSubscriptions = $phyreApiSDK->getHostingSubscriptions(); + + dd($centralServerHostingSubscriptionsExternalIds); + + if (isset($getExternalHostingSubscriptions['data']['HostingSubscriptions'])) { + $externalHostingSubscriptionsIds = []; + foreach ($getExternalHostingSubscriptions['data']['HostingSubscriptions'] as $externalHostingSubscriptions) { + $externalHostingSubscriptionsIds[] = $externalHostingSubscriptions['id']; + } + + // Delete hosting subscriptions to main server that are not in external server foreach ($customerExternalIds as $customerExternalId) { if (!in_array($customerExternalId, $externalCustomerIds)) { $getCustomer = Customer::where('external_id', $customerExternalId)->first(); @@ -57,37 +112,9 @@ class PhyreServer extends Model } } } - // Add customers to main server from external server - foreach ($getExternalCustomers['data']['customers'] as $externalCustomer) { - $findCustomer = Customer::where('external_id', $externalCustomer['id']) - ->where('phyre_server_id', $this->id) - ->first(); - if (!$findCustomer) { - $findCustomer = new Customer(); - $findCustomer->phyre_server_id = $this->id; - $findCustomer->external_id = $externalCustomer['id']; - } - $findCustomer->name = $externalCustomer['name']; - $findCustomer->username = $externalCustomer['username']; - $findCustomer->email = $externalCustomer['email']; - $findCustomer->phone = $externalCustomer['phone']; - $findCustomer->address = $externalCustomer['address']; - $findCustomer->city = $externalCustomer['city']; - $findCustomer->state = $externalCustomer['state']; - $findCustomer->zip = $externalCustomer['zip']; - $findCustomer->country = $externalCustomer['country']; - $findCustomer->company = $externalCustomer['company']; - $findCustomer->saveQuietly(); - } - } - - // Sync Hosting Subscriptions - $getExternalHostingSubscriptions = $phyreApiSDK->getHostingSubscriptions(); - if (isset($getExternalHostingSubscriptions['data']['hostingSubscriptions'])) { - dd($getExternalHostingSubscriptions['data']['hostingSubscriptions']); } - dd($getExternalHostingSubscriptions); + dd($externalHostingSubscriptionsIds); // // Sync Hosting Plans