Skip to content

Commit

Permalink
#519 Fetch Aurora Customer History (Notes) working on it
Browse files Browse the repository at this point in the history
  • Loading branch information
inikoo committed Oct 1, 2024
1 parent 7f15661 commit 2ced2f5
Show file tree
Hide file tree
Showing 17 changed files with 418 additions and 104 deletions.
29 changes: 22 additions & 7 deletions app/Actions/CRM/CustomerNote/StoreCustomerNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Models\CRM\Customer;
use App\Models\CRM\CustomerNote;
use App\Models\SysAdmin\User;
use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;
use Lorisleiva\Actions\ActionRequest;
use OwenIt\Auditing\Resolvers\IpAddressResolver;
Expand All @@ -22,6 +23,7 @@
class StoreCustomerNote extends OrgAction
{
use WithModelAddressActions;
use WithNotesDetails;


public function handle(Customer $customer, array $modelData): CustomerNote
Expand All @@ -34,20 +36,25 @@ public function handle(Customer $customer, array $modelData): CustomerNote
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);

if ($this->strict) {
data_set($modelData, 'user_type', class_basename($user));
data_set($modelData, 'user_id', $user->id);
}

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']]);

$modelData = $this->processNotes($modelData);

data_set($modelData, 'url', UrlResolver::resolve($customer));
data_set($modelData, 'ip_address', IpAddressResolver::resolve($customer));
data_set($modelData, 'user_agent', UserAgentResolver::resolve($customer));

data_set($modelData, 'event', Arr::get($modelData, 'event', 'customer_note'), overwrite: false);
data_set($modelData, 'tags', Arr::get($modelData, 'tags', ['customer_notes']), overwrite: false);


/** @var CustomerNote $CustomerNote */
$CustomerNote = $customer->customerNotes()->create($modelData);
Expand All @@ -72,8 +79,16 @@ public function rules(): array
];

if (!$this->strict) {
$rules['user_type'] = ['sometimes', Rule::in(['User', 'WebUser'])];
$rules['user_id'] = ['required', 'integer'];
$rules['user_type'] = ['sometimes', Rule::in(['User', 'WebUser'])];
$rules['user_id'] = ['sometimes', 'required', 'integer'];
$rules['source_id'] = ['sometimes', 'string'];
$rules['note'] = ['sometimes', 'string', 'max:4096'];
$rules['note_details_html'] = ['sometimes', 'string', 'max:4096'];
$rules['note_details'] = ['sometimes', 'string', 'max:4096'];
$rules['created_at'] = ['sometimes', 'date'];
$rules['new_values'] = ['sometimes', 'array'];
$rules['event'] = ['sometimes', 'string'];
$rules['tags'] = ['sometimes', 'array'];
}

return $rules;
Expand Down
6 changes: 6 additions & 0 deletions app/Actions/CRM/CustomerNote/UpdateCustomerNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
class UpdateCustomerNote extends OrgAction
{
use WithActionUpdate;
use WithNotesDetails;

public function handle(CustomerNote $customerNote, array $modelData): CustomerNote
{
$modelData = $this->processNotes($modelData);


return $this->update($customerNote, $modelData, ['data']);
}

Expand All @@ -40,6 +44,8 @@ public function rules(): array

if (!$this->strict) {
$rules['note'] = ['sometimes', 'string', 'max:4096'];
$rules['note_details_html'] = ['sometimes', 'string', 'max:4096'];
$rules['note_details'] = ['sometimes', 'string', 'max:4096'];
}

return $rules;
Expand Down
34 changes: 34 additions & 0 deletions app/Actions/CRM/CustomerNote/WithNotesDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/*
* Author: Raul Perusquia <[email protected]>
* Created: Tue, 01 Oct 2024 17:25:13 Malaysia Time, Kuala Lumpur, Malaysia
* Copyright (c) 2024, Raul A Perusquia Flores
*/

namespace App\Actions\CRM\CustomerNote;

use Illuminate\Support\Arr;

trait WithNotesDetails
{
private function processNotes($modelData)
{
if (!Arr::exists($modelData, 'new_values')) {
$newValues = ['note' => $modelData['note']];
if (Arr::exists($modelData, 'note_details_html')) {
$newValues['details']['html'] = $modelData['note_details_html'];
}
if (Arr::exists($modelData, 'note_details')) {
$newValues['details']['text'] = $modelData['note_details'];
}

data_set($modelData, 'new_values', $newValues);
}
data_forget($modelData, 'note');
data_forget($modelData, 'note_details_html');
data_forget($modelData, 'note_details');


return $modelData;
}
}
1 change: 1 addition & 0 deletions app/Actions/Transfers/Aurora/FetchAuroraAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected function preProcessCommand(Command $command): void
'fetch:portfolios',
'fetch:stock_movements',
'fetch:deleted-locations',
'fetch:customer-notes'
])) {
$this->onlyNew = (bool)$command->option('only_new');
}
Expand Down
74 changes: 41 additions & 33 deletions app/Actions/Transfers/Aurora/FetchAuroraCustomerNotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,53 @@ class FetchAuroraCustomerNotes extends FetchAuroraAction

public function handle(SourceOrganisationService $organisationSource, int $organisationSourceId): ?CustomerNote
{
if ($CustomerNoteData = $organisationSource->fetchCustomerNote($organisationSourceId)) {
if ($CustomerNote = CustomerNote::where('source_id', $CustomerNoteData['customer_note']['source_id'])
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;
}


// 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_notes_id' => $customerNote->id]);

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;
}


// } catch (Exception|Throwable $e) {
// $this->recordError($organisationSource, $e, $customerNoteData['customer_note'], 'CustomerNote', 'store');
// return null;
// }
}


return $CustomerNote;

return $customerNote;
}

return null;
Expand Down Expand Up @@ -90,12 +98,12 @@ public function count(): ?int
->where('Indirect Object', 'Customer');

if ($this->onlyNew) {

$query->whereNull('aiku_notes_id');
}

return $query->count();
}



}
64 changes: 42 additions & 22 deletions app/Actions/Transfers/Aurora/FetchAuroraDeletedUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class FetchAuroraDeletedUsers extends FetchAuroraAction
public string $commandSignature = 'fetch:deleted-users {organisations?*} {--s|source_id=} {--d|db_suffix=}';


/**
* @throws \Throwable
*/
public function handle(SourceOrganisationService $organisationSource, int $organisationSourceId): ?User
{
if ($userData = $organisationSource->fetchDeletedUser($organisationSourceId)) {
if ($userData['user']) {
if ($user = User::withTrashed()->where('source_id', $userData['user']['source_id'])
->first()) {
if ($user = User::withTrashed()->where('source_id', $userData['user']['source_id'])->first()) {
if (Arr::get($user->data, 'deleted.source') == 'aurora') {
try {
$user = UpdateUser::make()->action(
Expand All @@ -43,27 +45,46 @@ public function handle(SourceOrganisationService $organisationSource, int $organ
return null;
}
}
} else {
try {
$user = StoreUser::make()->action(
parent: $userData['shop'],
modelData: $userData['user'],
hydratorsDelay: $this->hydrateDelay,
strict: false
);

$this->recordNew($organisationSource);
} catch (Exception $e) {
$this->recordError($organisationSource, $e, $userData['user'], 'DeletedUser', 'store');

return null;
} elseif (!$userData['parent']) {
$group_id = $organisationSource->getOrganisation()->group_id;

$user = User::withTrashed()->where('group_id', $group_id)->where('username', $userData['related_username'])->first();

if ($user) {
$sourceData = explode(':', $userData['user']['source_id']);
DB::connection('aurora')->table('User Deleted Dimension')
->where('User Deleted Key', $sourceData[1])
->update(['aiku_related_id' => $user->id]);


DB::transaction(function () use ($user, $userData) {
$sources = $user->sources;
$sources[] = $userData['user']['source_id'];
$sources = array_unique($sources);
$user->updateQuietly(['sources' => $sources]);
});
}
} else {
// try {
$user = StoreUser::make()->action(
parent: $userData['parent'],
modelData: $userData['user'],
hydratorsDelay: $this->hydrateDelay,
strict: false
);

$sourceData = explode(':', $user->source_id);
DB::connection('aurora')->table('User Deleted Dimension')
->where('User Deleted Key', $sourceData[1])
->update(['aiku_id' => $user->id]);

$this->recordNew($organisationSource);
// } catch (Exception $e) {
// $this->recordError($organisationSource, $e, $userData['user'], 'DeletedUser', 'store');
// return null;
// }
}

$sourceData = explode(':', $user->source_id);
DB::connection('aurora')->table('User Deleted Dimension')
->where('User Key', $sourceData[1])
->update(['aiku_id' => $user->id]);

return $user;
}
Expand All @@ -83,8 +104,7 @@ public function getModelsQuery(): Builder
public function count(): ?int
{
$query = DB::connection('aurora')->table('User Deleted Dimension');

return $query->count();
}


}
Loading

0 comments on commit 2ced2f5

Please sign in to comment.