Skip to content

Commit

Permalink
Merge pull request #2 from realtimeregister/contact_mapping
Browse files Browse the repository at this point in the history
Contact mapping
  • Loading branch information
zbrag authored Aug 14, 2024
2 parents b1b37a3 + 9935a45 commit 49473a3
Show file tree
Hide file tree
Showing 10 changed files with 759 additions and 207 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"php": "^8.1"
},
"require-dev": {
"illuminate/database": "^10.29",
"illuminate/database": "v9.52.16",
"phpunit/phpunit": "^10.5",
"squizlabs/php_codesniffer": "^3.10"
}
Expand Down
806 changes: 650 additions & 156 deletions composer.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions modules/registrars/realtimeregister/realtimeregister.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ function realtimeregister_AdminCustomButtonArray(array $params): array
];
}

function realtimeregister_RegisterDomain(array $params): array
{
return App::dispatch(RegisterDomain::class, $params);
}

function realtimeregister_GetTldPricing(array $params)
{
return App::dispatch(\RealtimeRegister\Actions\Tlds\PricingSync::class, $params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RealtimeRegister\Actions\Contacts;

use RealtimeRegister\Actions\Arr;
use Illuminate\Support\Arr;
use SandwaveIo\RealtimeRegister\Domain\Contact;
use SandwaveIo\RealtimeRegister\Domain\DomainContact;
use SandwaveIo\RealtimeRegister\Domain\DomainDetails;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class CheckAvailability extends Action
public function __invoke(Request $request)
{
$isProxy = App::isProxy();

$isProxy->enable('premium');

$isProxyDomains = $isProxy->checkMany(
Expand All @@ -25,6 +24,7 @@ public function __invoke(Request $request)
$results = new ResultsList();

foreach ($isProxyDomains as $result) {

$tld = trim(strstr($result->getDomain(), '.'), '.');

$searchResult = new SearchResult($request->get('searchTerm'), $tld);
Expand All @@ -48,7 +48,7 @@ public function __invoke(Request $request)

$results->append($searchResult);

return $results;
}
return $results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use RealtimeRegister\App;
use RealtimeRegister\Models\ContactMapping;
use RealtimeRegister\Services\ContactService;

trait DomainTrait
{
Expand Down Expand Up @@ -32,34 +33,34 @@ protected function getOrCreateContact(int $clientId, int $contactId, string $rol
return $handle;
}

// Should we use the request domain?
// Fetch the whmcs contact
$whmcsContact = App::localApi()->contact($clientId, $contactId);

if (!$whmcsContact) {
return null;
}

$fields = [
'name' => $whmcsContact->get('')
];

// Try and match the whmcs contact to a rtr contact
// Should we even match the contact?
$contacts = App::client()->contacts->list(
App::registrarConfig()->get('customer_handle'),
parameters: [
'order' => '-createdDate',
'export' => true,
'fields' => 'handle'
]
);
// Should we even match the contact? yes / on exception only?
// $contact = ContactService::findRemote();

// If we do not find a match we create a new contact
$rtrContact = ContactService::convertToRtrContact($whmcsContact, $organizationAllowed);
$handle = uniqid(App::registrarConfig()->contactHandlePrefix() ?: '');

// Map the contact in the mapper
App::client()->contacts->create(
customer: App::registrarConfig()->customerHandle(),
handle: $handle,
name: $rtrContact->get('name'),
addressLine: $rtrContact->get('addressLine'),
postalCode: $rtrContact->get('postalCode'),
city: $rtrContact->get('city'),
country: $rtrContact->get('country'),
email: $rtrContact->get('email'),
voice: $rtrContact->get('voice'),
organization: $rtrContact->get('organization'),
state: $rtrContact->get('state')
);

//
ContactService::addContactMapping($clientId, $contactId, $handle, $organizationAllowed);
return $handle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
use RealtimeRegister\Actions\Action;
use RealtimeRegister\App;
use RealtimeRegister\Request;
use SandwaveIo\RealtimeRegister\Domain\DomainContactCollection;

class RegisterDomain extends Action
{
use DomainTrait;

private static array $CONTACT_ROLES = [
"TECH" => "techContacts",
"ADMIN" => "adminContacts",
"BILLING" => "billingContacts"
];


/**
* @throws \Exception
*/
public function __invoke(Request $request)
{

$metadata = $this->metadata($request);
$domain = $request->domain;

Expand All @@ -30,36 +39,39 @@ public function __invoke(Request $request)
$orderId = App::localApi()->domain(
clientId: $request->get('clientid'),
domainId: $request->get('domainid')
)->get('orderid');
)->get('orderId');
$contactId = App::localApi()->order(id: $orderId)->get('contactid');

$contacts = [];

foreach (['REGISTRANT', 'ADMIN', 'BILLING'] as $role) {
$organizationAllowed = $metadata->{strtolower($role) . 'Contacts'}->organizationAllowed;
$registrant = $this->getOrCreateContact(
clientId: $request->get('client_id'),
contactId: $contactId,
role: 'REGISTRANT',
organizationAllowed: $metadata->registrant->organizationAllowed
);

foreach (self::$CONTACT_ROLES as $role => $name) {
$organizationAllowed = $metadata->{$name}->organizationAllowed;
$contacts[] = [
'role' => $role,
'handle' => $this->getOrCreateContact(
clientId: $request->get('clientid'),
clientId: $request->get('client_id'),
contactId: $contactId,
role: $role,
organizationAllowed: $organizationAllowed
)
];
}

// Fetch order id
// Fetch contact id

// Create contacts for domain with all roles

$registration = App::client()->domains->register(
App::client()->domains->register(
domainName: $domain->domainName(),
customer: null,
registrant: null,
customer: App::registrarConfig()->customerHandle(),
registrant: $registrant,
period: $request->registrationPeriod,
autoRenew: false,
ns: $domain->nameservers
ns: $domain->nameservers,
contacts: DomainContactCollection::fromArray($contacts)
);
}
}
4 changes: 2 additions & 2 deletions modules/registrars/realtimeregister/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class App
protected const TABLE_REGISTRANT_HANDLE = 'mod_realtimeregister_registrant_handle';

protected const API_URL = "https://api.yoursrs.com/";
protected const API_URL_TEST = "https://api.yoursrs-ote.com/";
protected const API_URL_TEST = "host.docker.internal:8003"; //FIXME
protected const IS_PROXY_HOST = "is.yoursrs.com";
protected const IS_PROXY_HOST_TEST = "is.yoursrs-ote.com";

Expand Down Expand Up @@ -120,7 +120,7 @@ public static function isProxy(): IsProxy
}

return static::$isProxy = new IsProxy(
apiKey: App::registrarConfig()->apiKey(),
apiKey: "cGlldGVyamFuL2FkbWluOmtlcvS8FZgoxd2yygmFBvvP0iJGmkyd2MrEh+PejQy/",
host: App::registrarConfig()->isTest() ? self::IS_PROXY_HOST_TEST : self::IS_PROXY_HOST
);
}
Expand Down
9 changes: 6 additions & 3 deletions modules/registrars/realtimeregister/src/LocalApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function domains(array $filters = []): Collection
{
$data = localApi('GetClientsDomains', array_filter($filters));

return collect($data['domains'] ?? [])->map(fn($domain) => new DataObject($domain));

return collect($data['domains'] ?? [])->map(fn($domain) => new DataObject($domain[0]));
}

public function domain(int $clientId = null, int $domainId = null, string $domain = null): ?DataObject
Expand All @@ -35,7 +36,7 @@ public function orders(array $filters = []): Collection
{
$data = localApi('GetOrders', array_filter($filters));

return collect($data['orders'] ?? [])->map(fn($order) => new DataObject($order));
return collect($data['orders'] ?? [])->map(fn($order) => new DataObject($order[0]));
}

public function order(
Expand Down Expand Up @@ -73,7 +74,9 @@ public function contact(int $clientId, int $contactId): ?DataObject
$start += (int)$data['numreturned'];
} while ($start < (int)$data['totalresults']);

return null;


return new DataObject(localAPI('GetClientsDetails', ['clientid' => $clientId])['client']);
}

public function createContact()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;
use RealtimeRegister\App;
use RealtimeRegister\Entities\Contact;
use RealtimeRegister\Entities\DataObject;
use RealtimeRegister\Models\ContactMapping;
use SandwaveIo\RealtimeRegister\Domain\Contact as RTRContact;

class ContactService
{
public function findRemote(Contact $contact, bool $organizationAllowed)
public static function findRemote(DataObject $contact, bool $organizationAllowed)
{
$params = Arr::only($contact->toArray(), ['organization', 'name', 'email', 'country']);
$params = Arr::only($contact->getArrayCopy(), ['organization', 'name', 'email', 'country']);

if (!$organizationAllowed) {
unset($params['organization']);
Expand All @@ -25,14 +25,10 @@ public function findRemote(Contact $contact, bool $organizationAllowed)
'export' => true
]);

dd($params);

$result = App::client()->contacts->list(
return App::client()->contacts->list(
customer: App::registrarConfig()->customerHandle(),
parameters: $params
);

dd($result);
)[0];
}

public function findByContactId($contactId)
Expand All @@ -47,6 +43,26 @@ public function findLocal(RTRContact $contact)
// Search with data
}

public static function convertToRtrContact(DataObject $whmcsContact, bool $organizationAllowed) : DataObject
{
$rtr_contact = [
'name' => trim($whmcsContact['firstname'] . " " . $whmcsContact['lastname']),
'addressLine' => array_values(array_filter([$whmcsContact['address1'], $whmcsContact['address2']])),
'postalCode' => $whmcsContact['postcode'],
'city' => $whmcsContact['city'],
'state' => $whmcsContact['state'],
'country' => $whmcsContact['country'],
'email' => $whmcsContact['email'],
'voice' => $whmcsContact['phonenumberformatted'],
];

if ($organizationAllowed) {
$rtr_contact['organization'] = $whmcsContact['organization'];
}

return new DataObject($rtr_contact);
}

/**
* @param string|int $userId
* @param string|int $contactId
Expand All @@ -67,4 +83,25 @@ public function handleHasMapping(string $handle): bool
{
return ContactMapping::query()->where('handle', $handle)->exists();
}

public static function addContactMapping(int $clientId, int $contactId, string $handle, bool $orgAllowed): void {
ContactMapping::query()->insert([
"userid" => $clientId,
"contactid" => $contactId,
"handle" => $handle,
"org_allowed" => $orgAllowed
]);
}

public static function getMatchingRtrContact() {
//TODO if necessary
// App::client()->contacts->list(
// App::registrarConfig()->get('customer_handle'),
// parameters: [
// 'order' => '-createdDate',
// 'export' => true,
// 'fields' => 'handle'
// ]
// )->first();
}
}

0 comments on commit 49473a3

Please sign in to comment.