Skip to content

Commit

Permalink
#519 Fetch Aurora Customer History (Notes) foundations
Browse files Browse the repository at this point in the history
  • Loading branch information
inikoo committed Oct 1, 2024
1 parent 4aedb7b commit b04268a
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 17 deletions.
102 changes: 102 additions & 0 deletions app/Actions/CRM/CustomerNote/StoreCustomerNote.php
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);
}


}
68 changes: 68 additions & 0 deletions app/Actions/CRM/CustomerNote/UpdateCustomerNote.php
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 app/Actions/Transfers/Aurora/FetchAuroraCustomerNotes.php
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();
}



}
35 changes: 22 additions & 13 deletions app/Actions/Transfers/Aurora/FetchAuroraCustomers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
use Exception;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Throwable;

class FetchAuroraCustomers extends FetchAuroraAction
{
use WithAuroraAttachments;
use WithAuroraParsers;

public string $commandSignature = 'fetch:customers {organisations?*} {--s|source_id=} {--S|shop= : Shop slug} {--w|with=* : Accepted values: clients orders web-users attachments portfolio} {--N|only_new : Fetch only new} {--d|db_suffix=} {--r|reset}';
public string $commandSignature = 'fetch:customers {organisations?*} {--s|source_id=} {--S|shop= : Shop slug} {--w|with=* : Accepted values: clients orders web-users attachments portfolio full} {--N|only_new : Fetch only new} {--d|db_suffix=} {--r|reset}';


public function handle(SourceOrganisationService $organisationSource, int $organisationSourceId): ?Customer
Expand Down Expand Up @@ -61,7 +62,11 @@ public function handle(SourceOrganisationService $organisationSource, int $organ
);

$this->recordNew($organisationSource);
} catch (Exception $e) {
$sourceData = explode(':', $customer->source_id);
DB::connection('aurora')->table('Customer Dimension')
->where('Customer Key', $sourceData[1])
->update(['aiku_id' => $customer->id]);
} catch (Exception|Throwable $e) {
$this->recordError($organisationSource, $e, $customerData['customer'], 'Customer', 'store');

return null;
Expand All @@ -71,7 +76,7 @@ public function handle(SourceOrganisationService $organisationSource, int $organ

$sourceData = explode(':', $customer->source_id);

if (in_array('products', $with)) {
if (in_array('products', $with) || in_array('full', $with)) {
foreach (
DB::connection('aurora')
->table('Product Dimension')
Expand All @@ -84,7 +89,12 @@ public function handle(SourceOrganisationService $organisationSource, int $organ
}


if ($customer->shop->type == ShopTypeEnum::DROPSHIPPING and in_array('clients', $with)) {
if ($customer->shop->type == ShopTypeEnum::DROPSHIPPING and
(
in_array('clients', $with) || in_array('full', $with)
)

) {
foreach (
DB::connection('aurora')
->table('Customer Client Dimension')
Expand All @@ -96,7 +106,11 @@ public function handle(SourceOrganisationService $organisationSource, int $organ
}
}

if ($customer->shop->type == ShopTypeEnum::DROPSHIPPING and in_array('portfolio', $with)) {
if ($customer->shop->type == ShopTypeEnum::DROPSHIPPING and
(
in_array('portfolio', $with) || in_array('full', $with)
)
) {
foreach (
DB::connection('aurora')
->table('Customer Portfolio Fact')
Expand All @@ -108,7 +122,7 @@ public function handle(SourceOrganisationService $organisationSource, int $organ
}
}

if (in_array('orders', $with)) {
if (in_array('orders', $with) || in_array('full', $with)) {
foreach (
DB::connection('aurora')
->table('Order Dimension')
Expand All @@ -121,7 +135,7 @@ public function handle(SourceOrganisationService $organisationSource, int $organ
}


if (in_array('web-users', $with)) {
if (in_array('web-users', $with) || in_array('full', $with)) {
foreach (
DB::connection('aurora')
->table('Website User Dimension')
Expand All @@ -134,12 +148,7 @@ public function handle(SourceOrganisationService $organisationSource, int $organ
}


DB::connection('aurora')->table('Customer Dimension')
->where('Customer Key', $sourceData[1])
->update(['aiku_id' => $customer->id]);


if (in_array('attachments', $this->with)) {
if (in_array('attachments', $this->with) || in_array('full', $with)) {
$sourceData = explode(':', $customer->source_id);
foreach ($this->parseAttachments($sourceData[1]) ?? [] as $attachmentData) {
SaveModelAttachment::run(
Expand Down
Loading

0 comments on commit b04268a

Please sign in to comment.