From 86ed8044c3ebaa3b89d8e40b5c95fde21f342c92 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 14 Nov 2024 11:02:43 +1100 Subject: [PATCH] Refactor for livewire components --- .../EInvoice/SelfhostController.php | 26 ---- app/Livewire/EInvoice/Portal.php | 142 ------------------ .../PaymentMethods/UpdateDefaultMethod.php | 36 +++-- app/Livewire/Profile/Settings/General.php | 24 ++- .../Profile/Settings/NameWebsiteLogo.php | 16 +- .../Profile/Settings/PersonalAddress.php | 7 +- .../Profile/Settings/ShippingAddress.php | 7 +- resources/views/einvoice/index.blade.php | 5 - .../update-default-payment-method.blade.php | 4 +- .../ninja2020/payment_methods/show.blade.php | 2 +- routes/web.php | 2 - 11 files changed, 55 insertions(+), 216 deletions(-) delete mode 100644 app/Http/Controllers/EInvoice/SelfhostController.php delete mode 100644 app/Livewire/EInvoice/Portal.php delete mode 100644 resources/views/einvoice/index.blade.php diff --git a/app/Http/Controllers/EInvoice/SelfhostController.php b/app/Http/Controllers/EInvoice/SelfhostController.php deleted file mode 100644 index 3d6c07e57d..0000000000 --- a/app/Http/Controllers/EInvoice/SelfhostController.php +++ /dev/null @@ -1,26 +0,0 @@ -api_url = config('ninja.hosted_ninja_url'); - - $this->getCompanies(); - - } - - private function getCompanies(): self - { - - $this->companies = auth()->guard('user')->check() ? auth()->guard('user')->user()->account->companies->map(function ($company) { - return [ - 'key' => $company->company_key, - 'city' => $company->settings->city, - 'country' => $company->country()->iso_3166_2, - 'county' => $company->settings->state, - 'line1' => $company->settings->address1, - 'line2' => $company->settings->address2, - 'party_name' => $company->settings->name, - 'vat_number' => $company->settings->vat_number, - 'zip' => $company->settings->postal_code, - 'legal_entity_id' => $company->legal_entity_id, - 'tax_registered' => (bool) strlen($company->settings->vat_number ?? '') > 2, - 'tenant_id' => $company->company_key, - 'classification' => strlen($company->settings->classification ?? '') > 2 ? $company->settings->classification : 'business', - ]; - })->toArray() : []; - - return $this; - - } - - public function login() - { - $credentials = ['email' => $this->email, 'password' => $this->password]; - - if (Auth::attempt($credentials)) { - session()->flash('message', 'Logged in successfully.'); - - App::setLocale(auth()->guard('user')->user()->account->companies->first()->getLocale()); - - $this->getCOmpanies(); - - - } else { - session()->flash('error', 'Invalid credentials.'); - } - } - - public function logout() - { - Auth::logout(); - - session()->flash('message', 'Logged out!'); - - } - - public function register(string $company_key) - { - - $register_company = [ - 'acts_as_receiver' => true, - 'acts_as_sender' => true, - 'advertisements' => ['invoice'] - ]; - - foreach($this->companies as $company) - { - if($company['key'] == $company_key) - $register_company = array_merge($company, $register_company); - } - - $r = Http::withHeaders($this->getHeaders()) - ->post("{$this->api_url}/api/einvoice/createLegalEntity", $register_company); - - if($r->successful()) - { - - nlog($r->body()); - $response = $r->json(); - - $_company = auth()->guard('user')->user()->account->companies()->where('company_key', $company_key)->first(); - $_company->legal_entity_id = $response['id']; - $_company->save(); - - $this->getCompanies(); - - return; - } - - if($r->failed()) - nlog($r->getBody()->getContents()); - - $error = json_decode($r->getBody()->getContents(),true); - - session()->flash('error', $error['message']); - - } - - private function getHeaders() - { - return [ - 'X-API-SELF-HOST-TOKEN' => config('ninja.license_key'), - "X-Requested-With" => "XMLHttpRequest", - "Content-Type" => "application/json", - ]; - } - - public function render() - { - return view('livewire.e-invoice.portal'); - } -} diff --git a/app/Livewire/PaymentMethods/UpdateDefaultMethod.php b/app/Livewire/PaymentMethods/UpdateDefaultMethod.php index f2f93c919f..77b20c3a59 100644 --- a/app/Livewire/PaymentMethods/UpdateDefaultMethod.php +++ b/app/Livewire/PaymentMethods/UpdateDefaultMethod.php @@ -12,39 +12,43 @@ namespace App\Livewire\PaymentMethods; -use App\Libraries\MultiDB; use Livewire\Component; +use App\Libraries\MultiDB; +use Livewire\Attributes\Computed; +use App\Models\ClientGatewayToken; class UpdateDefaultMethod extends Component { - /** @var \App\Models\Company */ - public $company; + public $db; - /** @var \App\Models\ClientGatewayToken */ - public $token; - - /** @var \App\Models\Client */ - public $client; + public $token_id; public function mount() { - $this->company = $this->client->company; + MultiDB::setDb($this->db); + } - MultiDB::setDb($this->company->db); - - // $this->is_disabled = $this->token->is_default; + #[Computed] + public function token() + { + return ClientGatewayToken::withTrashed()->find($this->token_id); } public function makeDefault(): void { - if ($this->token->is_default) { + + MultiDB::setDb($this->db); + + + if ($this->token()->is_default) { return; } - $this->client->gateway_tokens()->update(['is_default' => 0]); + $this->token()->client->gateway_tokens()->update(['is_default' => 0]); - $this->token->is_default = 1; - $this->token->save(); + $token = $this->token(); + $token->is_default = 1; + $token->save(); $this->dispatch('UpdateDefaultMethod::method-updated'); } diff --git a/app/Livewire/Profile/Settings/General.php b/app/Livewire/Profile/Settings/General.php index 3e0c14238f..e8d5157f8b 100644 --- a/app/Livewire/Profile/Settings/General.php +++ b/app/Livewire/Profile/Settings/General.php @@ -12,13 +12,12 @@ namespace App\Livewire\Profile\Settings; -use Illuminate\Support\Facades\Hash; use Livewire\Component; +use Livewire\Attributes\Computed; +use Illuminate\Support\Facades\Hash; class General extends Component { - public $profile; - public $first_name; public $last_name; @@ -40,12 +39,18 @@ class General extends Component 'phone' => ['sometimes'], ]; + #[Computed] + public function profile() + { + return auth()->guard('contact')->user(); + } + public function mount() { - $profile = auth()->guard('contact')->user(); + $profile = $this->profile(); $this->fill([ - 'profile' => $profile, + // 'profile' => $profile, 'first_name' => $profile->first_name, 'last_name' => $profile->last_name, 'email' => $profile->email, @@ -61,7 +66,10 @@ class General extends Component public function submit() { - if ($this->profile->email != $this->email) { + + $profile = $this->profile(); + + if ($profile->email != $this->email) { $this->rules['email'][] = 'unique:client_contacts,email'; } @@ -72,10 +80,10 @@ class General extends Component $data = $this->validate($this->rules); if (! empty($this->password)) { - $this->profile->password = Hash::make($this->password); + $profile->password = Hash::make($this->password); } - $this->profile + $profile ->fill($data) ->save(); diff --git a/app/Livewire/Profile/Settings/NameWebsiteLogo.php b/app/Livewire/Profile/Settings/NameWebsiteLogo.php index 71ab2ebdde..8e6d527c98 100644 --- a/app/Livewire/Profile/Settings/NameWebsiteLogo.php +++ b/app/Livewire/Profile/Settings/NameWebsiteLogo.php @@ -6,8 +6,6 @@ use Livewire\Component; class NameWebsiteLogo extends Component { - public $profile; - public $name; public $vat_number; @@ -28,7 +26,7 @@ class NameWebsiteLogo extends Component public function mount() { $this->fill([ - 'profile' => auth()->guard('contact')->user()->client, + // 'profile' => auth()->guard('contact')->user()->client, 'name' => auth()->guard('contact')->user()->client->present()->name(), 'vat_number' => auth()->guard('contact')->user()->client->vat_number ?: '', 'website' => auth()->guard('contact')->user()->client->website, @@ -46,12 +44,14 @@ class NameWebsiteLogo extends Component { $data = $this->validate($this->rules); - $this->profile->name = $data['name']; - $this->profile->vat_number = $data['vat_number']; - $this->profile->website = $data['website']; - $this->profile->phone = $data['phone']; + $profile = auth()->guard('contact')->user()->client; - $this->profile->save(); + $profile->name = $data['name']; + $profile->vat_number = $data['vat_number']; + $profile->website = $data['website']; + $profile->phone = $data['phone']; + + $profile->save(); $this->saved = ctrans('texts.saved_at', ['time' => now()->toTimeString()]); } diff --git a/app/Livewire/Profile/Settings/PersonalAddress.php b/app/Livewire/Profile/Settings/PersonalAddress.php index f0ab4bcc00..ef3a190251 100644 --- a/app/Livewire/Profile/Settings/PersonalAddress.php +++ b/app/Livewire/Profile/Settings/PersonalAddress.php @@ -6,7 +6,6 @@ use Livewire\Component; class PersonalAddress extends Component { - public $profile; public $address1; @@ -34,7 +33,7 @@ class PersonalAddress extends Component public function mount() { $this->fill([ - 'profile' => auth()->guard('contact')->user()->client, + // 'profile' => auth()->guard('contact')->user()->client, 'address1' => auth()->guard('contact')->user()->client->address1, 'address2' => auth()->guard('contact')->user()->client->address2, 'city' => auth()->guard('contact')->user()->client->city, @@ -58,7 +57,9 @@ class PersonalAddress extends Component $data['country_id'] = null; } - $this->profile + $profile = auth()->guard('contact')->user()->client; + + $profile ->fill($data) ->save(); diff --git a/app/Livewire/Profile/Settings/ShippingAddress.php b/app/Livewire/Profile/Settings/ShippingAddress.php index 41393bc40b..475f66d2bb 100644 --- a/app/Livewire/Profile/Settings/ShippingAddress.php +++ b/app/Livewire/Profile/Settings/ShippingAddress.php @@ -6,7 +6,6 @@ use Livewire\Component; class ShippingAddress extends Component { - public $profile; public $shipping_address1; @@ -34,7 +33,7 @@ class ShippingAddress extends Component public function mount() { $this->fill([ - 'profile' => auth()->guard('contact')->user()->client, + // 'profile' => auth()->guard('contact')->user()->client, 'shipping_address1' => auth()->guard('contact')->user()->client->shipping_address1, 'shipping_address2' => auth()->guard('contact')->user()->client->shipping_address2, 'shipping_city' => auth()->guard('contact')->user()->client->shipping_city, @@ -58,7 +57,9 @@ class ShippingAddress extends Component $data['shipping_country_id'] = null; } - $this->profile + $profile = auth()->guard('contact')->user()->client; + + $profile ->fill($data) ->save(); diff --git a/resources/views/einvoice/index.blade.php b/resources/views/einvoice/index.blade.php deleted file mode 100644 index faff93397e..0000000000 --- a/resources/views/einvoice/index.blade.php +++ /dev/null @@ -1,5 +0,0 @@ -@extends('layouts.ninja') - -@section('body') - @livewire('e-invoice.portal') -@stop \ No newline at end of file diff --git a/resources/views/portal/ninja2020/components/livewire/update-default-payment-method.blade.php b/resources/views/portal/ninja2020/components/livewire/update-default-payment-method.blade.php index f6f4ce0623..9d8edadee6 100644 --- a/resources/views/portal/ninja2020/components/livewire/update-default-payment-method.blade.php +++ b/resources/views/portal/ninja2020/components/livewire/update-default-payment-method.blade.php @@ -9,13 +9,13 @@

- {{ $token->is_default ? ctrans('texts.already_default_payment_method') : ctrans('texts.default_payment_method') }} + {{ $this->token->is_default ? ctrans('texts.already_default_payment_method') : ctrans('texts.default_payment_method') }}

-
diff --git a/resources/views/portal/ninja2020/payment_methods/show.blade.php b/resources/views/portal/ninja2020/payment_methods/show.blade.php index 73d3dbf1b0..ff7e6a4b1e 100644 --- a/resources/views/portal/ninja2020/payment_methods/show.blade.php +++ b/resources/views/portal/ninja2020/payment_methods/show.blade.php @@ -116,7 +116,7 @@
@endif - @livewire('payment-methods.update-default-method', ['token' => $payment_method, 'client' => $client]) + @livewire('payment-methods.update-default-method', ['token_id' => $payment_method->id, 'db' => $client->company->db])
diff --git a/routes/web.php b/routes/web.php index 107bcba858..518c002b49 100644 --- a/routes/web.php +++ b/routes/web.php @@ -57,6 +57,4 @@ Route::get('gocardless/oauth/connect/confirm', [GoCardlessOAuthController::class Route::post('gocardless/oauth/connect/webhook', GoCardlessOAuthWebhookController::class)->name('gocardless.oauth.webhook'); Route::get('gocardless/oauth/connect/{token}', [GoCardlessOAuthController::class, 'connect']); -Route::get('einvoice/beta', [SelfhostController::class, 'index'])->name('einvoice.beta'); - \Illuminate\Support\Facades\Broadcast::routes(['middleware' => ['token_auth']]);