-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#519 Fetch Aurora Customer History (Notes) foundations
- Loading branch information
Showing
9 changed files
with
450 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/* | ||
* Author: Raul Perusquia <[email protected]> | ||
* Created: Tue, 01 Oct 2024 10:17:17 Malaysia Time, Kuala Lumpur, Malaysia | ||
* Copyright (c) 2024, Raul A Perusquia Flores | ||
*/ | ||
|
||
namespace App\Actions\CRM\CustomerNote; | ||
|
||
use App\Actions\OrgAction; | ||
use App\Actions\Traits\WithModelAddressActions; | ||
use App\Models\CRM\Customer; | ||
use App\Models\CRM\CustomerNote; | ||
use App\Models\SysAdmin\User; | ||
use Illuminate\Validation\Rule; | ||
use Lorisleiva\Actions\ActionRequest; | ||
use OwenIt\Auditing\Resolvers\IpAddressResolver; | ||
use OwenIt\Auditing\Resolvers\UrlResolver; | ||
use OwenIt\Auditing\Resolvers\UserAgentResolver; | ||
use OwenIt\Auditing\Resolvers\UserResolver; | ||
|
||
class StoreCustomerNote extends OrgAction | ||
{ | ||
use WithModelAddressActions; | ||
|
||
|
||
public function handle(Customer $customer, array $modelData): CustomerNote | ||
{ | ||
/** @var User $user */ | ||
$user = UserResolver::resolve(); | ||
|
||
data_set($modelData, 'group_id', $customer->group_id); | ||
data_set($modelData, 'organisation_id', $customer->organisation_id); | ||
data_set($modelData, 'shop_id', $customer->shop_id); | ||
data_set($modelData, 'customer_id', $customer->id); | ||
|
||
data_set($modelData, 'user_type', class_basename($user), overwrite: false); | ||
data_set($modelData, 'user_id', $user->id, overwrite: false); | ||
|
||
data_set($modelData, 'auditable_type', 'Customer'); | ||
data_set($modelData, 'auditable_id', $customer->id); | ||
|
||
data_set($modelData, 'tags', ['customer_notes']); | ||
data_set($modelData, 'event', 'note_created'); | ||
data_set($modelData, 'new_values', ['note' => $modelData['note']]); | ||
|
||
data_set($modelData, 'url', UrlResolver::resolve($customer)); | ||
data_set($modelData, 'ip_address', IpAddressResolver::resolve($customer)); | ||
data_set($modelData, 'user_agent', UserAgentResolver::resolve($customer)); | ||
|
||
|
||
/** @var CustomerNote $CustomerNote */ | ||
$CustomerNote = $customer->customerNotes()->create($modelData); | ||
|
||
|
||
return $CustomerNote; | ||
} | ||
|
||
public function authorize(ActionRequest $request): bool | ||
{ | ||
if ($this->asAction) { | ||
return true; | ||
} | ||
|
||
return $request->user()->hasPermissionTo("crm.{$this->shop->id}.edit"); | ||
} | ||
|
||
public function rules(): array | ||
{ | ||
$rules = [ | ||
'note' => ['required', 'string', 'max:1024'], | ||
]; | ||
|
||
if (!$this->strict) { | ||
$rules['user_type'] = ['sometimes', Rule::in(['User', 'WebUser'])]; | ||
$rules['user_id'] = ['required', 'integer']; | ||
} | ||
|
||
return $rules; | ||
} | ||
|
||
|
||
public function action(Customer $customer, array $modelData, int $hydratorsDelay = 0, bool $strict = true): CustomerNote | ||
{ | ||
$this->asAction = true; | ||
$this->strict = $strict; | ||
$this->hydratorsDelay = $hydratorsDelay; | ||
$this->initialisationFromShop($customer->shop, $modelData); | ||
|
||
return $this->handle($customer, $this->validatedData); | ||
} | ||
|
||
public function asController(Customer $customer, ActionRequest $request): CustomerNote | ||
{ | ||
$this->asAction = true; | ||
$this->initialisationFromShop($customer->shop, $request); | ||
|
||
return $this->handle($customer, $this->validatedData); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/* | ||
* Author: Raul Perusquia <[email protected]> | ||
* Created: Tue, 01 Oct 2024 10:17:17 Malaysia Time, Kuala Lumpur, Malaysia | ||
* Copyright (c) 2024, Raul A Perusquia Flores | ||
*/ | ||
|
||
namespace App\Actions\CRM\CustomerNote; | ||
|
||
use App\Actions\OrgAction; | ||
use App\Actions\Traits\WithActionUpdate; | ||
use App\Models\Catalogue\Shop; | ||
use App\Models\CRM\CustomerNote; | ||
use App\Models\SysAdmin\Organisation; | ||
use Lorisleiva\Actions\ActionRequest; | ||
|
||
class UpdateCustomerNote extends OrgAction | ||
{ | ||
use WithActionUpdate; | ||
|
||
public function handle(CustomerNote $customerNote, array $modelData): CustomerNote | ||
{ | ||
return $this->update($customerNote, $modelData, ['data']); | ||
} | ||
|
||
public function authorize(ActionRequest $request): bool | ||
{ | ||
if ($this->asAction) { | ||
return true; | ||
} | ||
|
||
return $request->user()->hasPermissionTo("crm.{$this->shop->id}.edit"); | ||
} | ||
|
||
public function rules(): array | ||
{ | ||
$rules = [ | ||
'note' => ['sometimes', 'string', 'max:1024'], | ||
]; | ||
|
||
if (!$this->strict) { | ||
$rules['note'] = ['sometimes', 'string', 'max:4096']; | ||
} | ||
|
||
return $rules; | ||
} | ||
|
||
|
||
public function asController(Organisation $organisation, Shop $shop, CustomerNote $customerNote, ActionRequest $request): CustomerNote | ||
{ | ||
$this->initialisationFromShop($shop, $request); | ||
|
||
return $this->handle($customerNote, $this->validatedData); | ||
} | ||
|
||
public function action(CustomerNote $customerNote, array $modelData, int $hydratorsDelay = 0, bool $strict = true): CustomerNote | ||
{ | ||
$this->asAction = true; | ||
$this->strict = $strict; | ||
$this->hydratorsDelay = $hydratorsDelay; | ||
$this->setRawAttributes($modelData); | ||
$this->initialisationFromShop($customerNote->shop, $modelData); | ||
|
||
return $this->handle($customerNote, $this->validatedData); | ||
} | ||
|
||
|
||
} |
101 changes: 101 additions & 0 deletions
101
app/Actions/Transfers/Aurora/FetchAuroraCustomerNotes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/* | ||
* Author: Raul Perusquia <[email protected]> | ||
* Created: Tue, 01 Oct 2024 10:17:17 Malaysia Time, Kuala Lumpur, Malaysia | ||
* Copyright (c) 2024, Raul A Perusquia Flores | ||
*/ | ||
|
||
namespace App\Actions\Transfers\Aurora; | ||
|
||
use App\Actions\CRM\CustomerNote\StoreCustomerNote; | ||
use App\Actions\CRM\CustomerNote\UpdateCustomerNote; | ||
use App\Models\CRM\CustomerNote; | ||
use App\Transfers\SourceOrganisationService; | ||
use Exception; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Support\Facades\DB; | ||
use Throwable; | ||
|
||
class FetchAuroraCustomerNotes extends FetchAuroraAction | ||
{ | ||
public string $commandSignature = 'fetch:customer-notes {organisations?*} {--s|source_id=} {--N|only_new : Fetch only new} {--d|db_suffix=} {--r|reset}'; | ||
|
||
|
||
public function handle(SourceOrganisationService $organisationSource, int $organisationSourceId): ?CustomerNote | ||
{ | ||
if ($CustomerNoteData = $organisationSource->fetchCustomerNote($organisationSourceId)) { | ||
if ($CustomerNote = CustomerNote::where('source_id', $CustomerNoteData['customer_note']['source_id']) | ||
->first()) { | ||
try { | ||
|
||
$CustomerNote = UpdateCustomerNote::make()->action( | ||
CustomerNote: $CustomerNote, | ||
modelData: $CustomerNoteData['customer_note'], | ||
hydratorsDelay: 60, | ||
strict: false, | ||
); | ||
$this->recordChange($organisationSource, $CustomerNote->wasChanged()); | ||
} catch (Exception $e) { | ||
$this->recordError($organisationSource, $e, $CustomerNoteData['customer_note'], 'CustomerNote', 'update'); | ||
|
||
return null; | ||
} | ||
} else { | ||
|
||
|
||
try { | ||
$CustomerNote = StoreCustomerNote::make()->action( | ||
customer: $CustomerNoteData['customer'], | ||
modelData: $CustomerNoteData['customer_note'], | ||
hydratorsDelay: 60, | ||
strict: false, | ||
); | ||
$sourceData = explode(':', $CustomerNote->source_id); | ||
DB::connection('aurora')->table('History Dimension') | ||
->where('History Key', $sourceData[1]) | ||
->update(['aiku_id' => $CustomerNote->id]); | ||
} catch (Exception|Throwable $e) { | ||
$this->recordError($organisationSource, $e, $CustomerNoteData['customer_note'], 'CustomerNote', 'store'); | ||
return null; | ||
} | ||
} | ||
|
||
|
||
return $CustomerNote; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getModelsQuery(): Builder | ||
{ | ||
$query = DB::connection('aurora') | ||
->table('History Dimension') | ||
->where('Direct Object', 'Note') | ||
->where('Indirect Object', 'Customer') | ||
->select('History Key as source_id') | ||
->orderBy('source_id'); | ||
|
||
if ($this->onlyNew) { | ||
$query->whereNull('aiku_notes_id'); | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
public function count(): ?int | ||
{ | ||
$query = DB::connection('aurora')->table('History Dimension') | ||
->where('Direct Object', 'Note') | ||
->where('Indirect Object', 'Customer'); | ||
|
||
if ($this->onlyNew) { | ||
$query->whereNull('aiku_notes_id'); | ||
} | ||
|
||
return $query->count(); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.