From 2ced2f5370ba4adc09a3400459b84df617e93a88 Mon Sep 17 00:00:00 2001 From: Raul Perusquia Date: Wed, 2 Oct 2024 03:42:21 +0800 Subject: [PATCH] #519 Fetch Aurora Customer History (Notes) working on it --- .../CRM/CustomerNote/StoreCustomerNote.php | 29 +++- .../CRM/CustomerNote/UpdateCustomerNote.php | 6 + .../CRM/CustomerNote/WithNotesDetails.php | 34 ++++ .../Transfers/Aurora/FetchAuroraAction.php | 1 + .../Aurora/FetchAuroraCustomerNotes.php | 74 +++++---- .../Aurora/FetchAuroraDeletedUsers.php | 64 +++++--- .../Transfers/Aurora/FetchResetBase.php | 107 ++++++++++-- app/Models/CRM/Customer.php | 4 +- app/Models/HumanResources/Employee.php | 3 +- app/Models/SysAdmin/User.php | 3 + .../Aurora/FetchAuroraCustomerNote.php | 153 +++++++++++++++++- .../Aurora/FetchAuroraDeletedUser.php | 22 ++- app/Transfers/Aurora/FetchAuroraUser.php | 8 +- app/Transfers/Aurora/WithAuroraParsers.php | 1 + app/Transfers/WowsbarOrganisationService.php | 10 -- .../2022_08_15_000000_create_users_table.php | 1 + routes/channels.php | 2 +- 17 files changed, 418 insertions(+), 104 deletions(-) create mode 100644 app/Actions/CRM/CustomerNote/WithNotesDetails.php diff --git a/app/Actions/CRM/CustomerNote/StoreCustomerNote.php b/app/Actions/CRM/CustomerNote/StoreCustomerNote.php index c12f9f43d0..57bf66191d 100644 --- a/app/Actions/CRM/CustomerNote/StoreCustomerNote.php +++ b/app/Actions/CRM/CustomerNote/StoreCustomerNote.php @@ -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; @@ -22,6 +23,7 @@ class StoreCustomerNote extends OrgAction { use WithModelAddressActions; + use WithNotesDetails; public function handle(Customer $customer, array $modelData): CustomerNote @@ -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); @@ -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; diff --git a/app/Actions/CRM/CustomerNote/UpdateCustomerNote.php b/app/Actions/CRM/CustomerNote/UpdateCustomerNote.php index c4bec50ead..2526666f25 100644 --- a/app/Actions/CRM/CustomerNote/UpdateCustomerNote.php +++ b/app/Actions/CRM/CustomerNote/UpdateCustomerNote.php @@ -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']); } @@ -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; diff --git a/app/Actions/CRM/CustomerNote/WithNotesDetails.php b/app/Actions/CRM/CustomerNote/WithNotesDetails.php new file mode 100644 index 0000000000..a0e15491f6 --- /dev/null +++ b/app/Actions/CRM/CustomerNote/WithNotesDetails.php @@ -0,0 +1,34 @@ + + * 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; + } +} diff --git a/app/Actions/Transfers/Aurora/FetchAuroraAction.php b/app/Actions/Transfers/Aurora/FetchAuroraAction.php index 2b4429e8f2..85be8c11e6 100644 --- a/app/Actions/Transfers/Aurora/FetchAuroraAction.php +++ b/app/Actions/Transfers/Aurora/FetchAuroraAction.php @@ -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'); } diff --git a/app/Actions/Transfers/Aurora/FetchAuroraCustomerNotes.php b/app/Actions/Transfers/Aurora/FetchAuroraCustomerNotes.php index 415bd056e1..1ddd648288 100644 --- a/app/Actions/Transfers/Aurora/FetchAuroraCustomerNotes.php +++ b/app/Actions/Transfers/Aurora/FetchAuroraCustomerNotes.php @@ -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; @@ -90,6 +98,7 @@ public function count(): ?int ->where('Indirect Object', 'Customer'); if ($this->onlyNew) { + $query->whereNull('aiku_notes_id'); } @@ -97,5 +106,4 @@ public function count(): ?int } - } diff --git a/app/Actions/Transfers/Aurora/FetchAuroraDeletedUsers.php b/app/Actions/Transfers/Aurora/FetchAuroraDeletedUsers.php index 2366727100..a79e1e596d 100644 --- a/app/Actions/Transfers/Aurora/FetchAuroraDeletedUsers.php +++ b/app/Actions/Transfers/Aurora/FetchAuroraDeletedUsers.php @@ -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( @@ -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; } @@ -83,8 +104,7 @@ public function getModelsQuery(): Builder public function count(): ?int { $query = DB::connection('aurora')->table('User Deleted Dimension'); + return $query->count(); } - - } diff --git a/app/Actions/Transfers/Aurora/FetchResetBase.php b/app/Actions/Transfers/Aurora/FetchResetBase.php index 7907ec846a..af10e2923a 100644 --- a/app/Actions/Transfers/Aurora/FetchResetBase.php +++ b/app/Actions/Transfers/Aurora/FetchResetBase.php @@ -46,6 +46,22 @@ public function asCommand(Command $command): int $this->timeLastStep = microtime(true); + DB::connection('aurora')->table('History Dimension') + ->whereNotNull('aiku_notes_id') + ->update( + [ + 'aiku_notes_id' => null + ] + ); + + DB::connection('aurora')->table('History Dimension') + ->whereNotNull($aikuIdField) + ->update( + [ + $aikuIdField => null, + ] + ); + DB::connection('aurora')->table('Staff Dimension') ->update( [ @@ -70,7 +86,6 @@ public function asCommand(Command $command): int ->update([$aikuIdField => null]); - $command->line('✅ sysadmins'); DB::connection('aurora')->table('Store Dimension') @@ -80,10 +95,13 @@ public function asCommand(Command $command): int ->update([$aikuIdField => null]); DB::connection('aurora')->table('Product Dimension') + ->whereNotNull($aikuIdField) ->update([$aikuIdField => null]); DB::connection('aurora')->table('Product History Dimension') - ->update([$aikuIdField => null]); + ->update([ + $aikuIdField => null, + ]); DB::connection('aurora')->table('Category Dimension') ->update( @@ -123,12 +141,14 @@ public function asCommand(Command $command): int ); DB::connection('aurora')->table('Supplier Dimension') + ->whereNotNull($aikuIdField) ->update( [ $aikuIdField => null, ] ); DB::connection('aurora')->table('Supplier Deleted Dimension') + ->whereNotNull($aikuIdField) ->update( [ $aikuIdField => null, @@ -139,15 +159,20 @@ public function asCommand(Command $command): int DB::connection('aurora')->table('Attachment Bridge') + ->whereNotNull($aikuIdField) ->update([$aikuIdField => null]); DB::connection('aurora')->table('Image Subject Bridge') + ->whereNotNull($aikuIdField) ->update([$aikuIdField => null]); DB::connection('aurora')->table('Customer Dimension') + ->whereNotNull($aikuIdField) ->update([$aikuIdField => null]); DB::connection('aurora')->table('Customer Deleted Dimension') + ->whereNotNull($aikuIdField) ->update([$aikuIdField => null]); DB::connection('aurora')->table('Customer Client Dimension') + ->whereNotNull($aikuIdField) ->update([$aikuIdField => null]); DB::connection('aurora')->table('Website User Dimension') ->update([$aikuIdField => null]); @@ -173,6 +198,7 @@ public function asCommand(Command $command): int ->update([$aikuIdField => null]); DB::connection('aurora')->table('Part Dimension') + ->whereNotNull($aikuIdField) ->update([ // 'aiku_unit_id' => null, $aikuIdField => null @@ -197,6 +223,7 @@ public function asCommand(Command $command): int DB::connection('aurora')->table('Website User Dimension') ->update([$aikuIdField => null]); DB::connection('aurora')->table('Prospect Dimension') + ->whereNotNull($aikuIdField) ->update([$aikuIdField => null]); $command->line("✅ customers \t\t".$this->stepTime()); @@ -215,38 +242,84 @@ public function asCommand(Command $command): int $command->line("✅ HR \t\t\t".$this->stepTime()); - DB::connection('aurora')->table('Order Dimension')->update([$aikuIdField => null]); - DB::connection('aurora')->table('Order Transaction Fact')->update( + DB::connection('aurora')->table('Order Dimension') + ->whereNotNull($aikuIdField) + ->update([$aikuIdField => null]); + DB::connection('aurora')->table('Order Transaction Fact') + ->whereNotNull($aikuIdField) + ->update( + [ + $aikuIdField => null, + ] + ); + DB::connection('aurora')->table('Order Transaction Fact') + ->whereNotNull('aiku_basket_id') + ->update( + [ + 'aiku_basket_id' => null, + ] + ); + + DB::connection('aurora')->table('Order Transaction Fact') + ->whereNotNull('aiku_invoice_id') + ->update( + [ + 'aiku_invoice_id' => null + ] + ); + + + DB::connection('aurora')->table('Order No Product Transaction Fact')->update( [ $aikuIdField => null, 'aiku_basket_id' => null, 'aiku_invoice_id' => null ] ); - DB::connection('aurora')->table('Order No Product Transaction Fact')->update( - [ - $aikuIdField => null, - 'aiku_basket_id' => null, - 'aiku_invoice_id' => null - ] - ); - DB::connection('aurora')->table('Order Dimension')->update([$aikuIdField => null]); + $command->line("✅ orders \t\t".$this->stepTime()); - DB::connection('aurora')->table('Delivery Note Dimension')->update([$aikuIdField => null]); - DB::connection('aurora')->table('Inventory Transaction Fact')->update( - [ + DB::connection('aurora')->table('Delivery Note Dimension') + ->whereNotNull($aikuIdField) + ->update([$aikuIdField => null]); + + + + DB::connection('aurora')->table('Inventory Transaction Fact') + ->whereNotNull($aikuIdField) + ->update( + [ $aikuIdField => null, + + ] + ); + + DB::connection('aurora')->table('Inventory Transaction Fact') + ->whereNotNull('aiku_dn_item_id') + ->update( + [ 'aiku_dn_item_id' => null, + ] + ); + + DB::connection('aurora')->table('Inventory Transaction Fact') + ->whereNotNull('aiku_picking_id') + ->update( + [ 'aiku_picking_id' => null ] - ); + ); + + $command->line("✅ delivery notes \t\t".$this->stepTime()); - DB::connection('aurora')->table('Invoice Dimension')->update([$aikuIdField => null]); + DB::connection('aurora') + ->table('Invoice Dimension') + ->whereNotNull($aikuIdField) + ->update([$aikuIdField => null]); //DB::connection('aurora')->table('Invoice Deleted Dimension')->update([$aikuIdField => null]); diff --git a/app/Models/CRM/Customer.php b/app/Models/CRM/Customer.php index c5e775e7da..1bb7c6924b 100644 --- a/app/Models/CRM/Customer.php +++ b/app/Models/CRM/Customer.php @@ -42,7 +42,6 @@ use App\Models\Traits\HasImage; use App\Models\Traits\HasUniversalSearch; use App\Models\Traits\InShop; -use Google\Service\Sasportal\Resource\CustomersNodes; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -111,6 +110,7 @@ * @property-read Collection $audits * @property-read Collection $clients * @property-read Collection $creditTransactions + * @property-read Collection $customerNotes * @property-read Address|null $deliveryAddress * @property-read Collection $deliveryNotes * @property-read \App\Models\CRM\CustomerDropshippingStat|null $dropshippingStats @@ -396,6 +396,6 @@ public function deliveryNotes(): HasMany public function customerNotes(): HasMany { - return $this->hasMany(CustomersNodes::class); + return $this->hasMany(CustomerNote::class); } } diff --git a/app/Models/HumanResources/Employee.php b/app/Models/HumanResources/Employee.php index 3ed90d3bd0..5118a0a625 100644 --- a/app/Models/HumanResources/Employee.php +++ b/app/Models/HumanResources/Employee.php @@ -123,7 +123,7 @@ class Employee extends Model implements HasMedia, Auditable 'errors' => 'array', 'salary' => 'array', 'working_hours' => 'array', - // 'migration_data' => 'array', + 'migration_data' => 'array', 'date_of_birth' => 'datetime:Y-m-d', 'gender' => GenderEnum::class, 'state' => EmployeeStateEnum::class, @@ -220,7 +220,6 @@ public function users(): MorphToMany return $this->morphToMany(User::class, 'model', 'user_has_models')->withTimestamps()->withPivot('status'); } - public function getRouteKeyName(): string { return 'slug'; diff --git a/app/Models/SysAdmin/User.php b/app/Models/SysAdmin/User.php index 196f26ae5a..2884ae862b 100644 --- a/app/Models/SysAdmin/User.php +++ b/app/Models/SysAdmin/User.php @@ -68,6 +68,7 @@ * @property \Illuminate\Support\Carbon|null $deleted_at * @property string|null $delete_comment * @property string|null $source_id + * @property array $sources * @property string|null $legacy_password source password * @property-read \Illuminate\Database\Eloquent\Collection $audits * @property-read \Illuminate\Database\Eloquent\Collection $authorisedAgentsOrganisations @@ -128,6 +129,7 @@ class User extends Authenticatable implements HasMedia, Auditable protected $casts = [ 'data' => 'array', 'settings' => 'array', + 'sources' => 'array', 'status' => 'boolean', 'auth_type' => UserAuthTypeEnum::class, 'password' => 'hashed', @@ -137,6 +139,7 @@ class User extends Authenticatable implements HasMedia, Auditable protected $attributes = [ 'data' => '{}', 'settings' => '{}', + 'sources' => '{}', ]; public function generateTags(): array diff --git a/app/Transfers/Aurora/FetchAuroraCustomerNote.php b/app/Transfers/Aurora/FetchAuroraCustomerNote.php index 1ebeef3b56..13e68b5fd7 100644 --- a/app/Transfers/Aurora/FetchAuroraCustomerNote.php +++ b/app/Transfers/Aurora/FetchAuroraCustomerNote.php @@ -7,46 +7,183 @@ namespace App\Transfers\Aurora; +use App\Models\SysAdmin\User; use Illuminate\Support\Facades\DB; class FetchAuroraCustomerNote extends FetchAurora { protected function parseModel(): void { + // print_r($this->auroraModelData); + + if ($this->auroraModelData->{'Direct Object'} != 'Note') { + return; + } + + if ($this->auroraModelData->{'Indirect Object'} != 'Customer') { + return; + } + + + $details = ''; + if ($this->auroraModelData->{'History Details'}) { + $details = $this->auroraModelData->{'History Details'}; + } + + $newValues = []; + $event = 'customer_note'; + $tags = ['customer_notes']; + $note = $this->auroraModelData->{'History Abstract'}; + if ($note == 'Old Database Note (Attachment)') { + return; + } elseif ($note == 'Contact data imported from Act') { + $event = 'migration'; + } elseif ($note == 'Old Database Note (Field Changed)') { + $event = 'updated'; + $tags = ['crm']; + $details = null; + + $note = ''; + + + $historyDetails = $this->auroraModelData->{'History Details'}; + $historyDetails = preg_replace('/-------------------------------------------/', ';', $historyDetails); + + $changedFields = preg_split('/;\n/', $historyDetails); + foreach ($changedFields as $changedField) { + + + + // $changedField = preg_replace('/ID\/Status - Customer - /', 'ID/Status - ', $changedField); + + $changedField = trim($changedField); + + $changedField = preg_replace('/ -$/', ' - ', $changedField); + + /* + if($changedField=='ID/Status -'){ + $changedField='ID/Status - '; + }elseif($changedField=='Gold Reward Member -'){ + $changedField='Gold Reward Member - '; + }elseif($changedField=='Trade Name -'){ + $changedField='Trade Name - '; + } + */ + + + // print ">>>$changedField<<<\n"; + + + + list($fieldName, $fieldValue) = explode(' - ', $changedField, 2); + + // $fieldData = explode(' - ', $changedField); + + // if (count($fieldData) != 2) { + // dd($this->auroraModelData); + // } + $fieldName = trim($fieldName); + $fieldValue = trim(preg_replace('/;$/', '', $fieldValue)); + + + //print_r($fieldData); + + // $fieldName = trim($fieldData[0]); + // $fieldValue = trim(preg_replace('/;$/', '', $fieldData[1])); + + if ($fieldName == 'Gold Reward Member') { + continue; + } + if ($fieldName == 'Email Check' and $fieldValue == '') { + continue; + } + + $newValues[$fieldName] = $fieldValue; + } + + if (count($newValues) == 0) { + return; + } + } + + // dd($newValues); + $customer = $this->parseCustomer( $this->organisation->id.':'.$this->auroraModelData->{'Indirect Object Key'} ); $this->parsedData['customer'] = $customer; + $user = null; - if ($this->auroraModelData->{'Subject'} == 'Staff') { + if ($this->auroraModelData->{'Subject'} == 'Staff' and $this->auroraModelData->{'Subject Key'} > 0) { $employee = $this->parseEmployee( $this->organisation->id.':'.$this->auroraModelData->{'Subject Key'} ); + + if (!$employee) { + dd($this->auroraModelData); + } + $user = $employee->getUser(); + if (!$user) { + $userHasModel = DB::table('user_has_models') + ->where('model_id', $employee->id) + ->where('model_type', 'Employee') + ->first(); + if ($userHasModel) { + $user = User::withTrashed()->find($userHasModel->user_id); + } - } + } + + if (!$user) { + DB::connection('aurora') + ->table('User Deleted Dimension') + ->select('aiku_related_id') + ->where('User Parent Key', $this->auroraModelData->{'Subject Key'}) + ->update(['aiku_related_id' => $employee->id]); + } + + if (!$user) { + dd($this->auroraModelData); + } + + + } $this->parsedData['customer_note'] = [ - 'note' => $this->auroraModelData->{'History Abstract'}, + 'note' => $note, 'created_at' => $this->auroraModelData->{'History Date'}, 'source_id' => $this->organisation->id.':'.$this->auroraModelData->{'History Key'}, 'fetched_at' => now(), 'last_fetched_at' => now(), + 'event' => $event, + 'tags' => $tags ]; + if ($details) { + if ($this->isHTML($details)) { + $this->parsedData['customer_note']['note_details_html'] = $details; + } else { + $this->parsedData['customer_note']['note_details'] = $details; + } + } + + + if (count($newValues) > 0) { + $this->parsedData['customer_note']['new_values'] = $newValues; + } + if ($user) { $this->parsedData['customer_note']['user_type'] = 'User'; $this->parsedData['customer_note']['user_id'] = $user->id; - } else { - dd($this->auroraModelData); } } @@ -57,4 +194,10 @@ protected function fetchData($id): object|null ->table('History Dimension') ->where('History Key', $id)->first(); } + + protected function isHTML($string): bool + { + return $string != strip_tags($string); + } + } diff --git a/app/Transfers/Aurora/FetchAuroraDeletedUser.php b/app/Transfers/Aurora/FetchAuroraDeletedUser.php index af73010114..f53c8b8a26 100644 --- a/app/Transfers/Aurora/FetchAuroraDeletedUser.php +++ b/app/Transfers/Aurora/FetchAuroraDeletedUser.php @@ -15,7 +15,7 @@ class FetchAuroraDeletedUser extends FetchAurora { protected function parseModel(): void { - $this->parsedData['employee'] = null; + if (!$this->auroraModelData->{'User Deleted Metadata'}) { $auroraDeletedData = new stdClass(); } else { @@ -23,15 +23,32 @@ protected function parseModel(): void $auroraDeletedData = $auroraDeletedData->data; } + $parent = null; + if ($auroraDeletedData->{'User Type'} == 'Staff') { + // $this->parsedData['parent_type'] = 'Staff'; + $parent = $this->parseEmployee($this->organisation->id.':'.$auroraDeletedData->{'User Parent Key'}); + } + $data = [ 'deleted' => ['source' => 'aurora'] ]; + $relatedUsername = $this->auroraModelData->{'User Deleted Handle'}; + if ($this->auroraModelData->aiku_alt_username) { + $relatedUsername = $this->auroraModelData->aiku_alt_username; + } + + $this->parsedData['related_username'] = $relatedUsername; + $username = $auroraDeletedData->{'User Handle'}.'-deleted_aurora_'.$this->organisation->id.'_'.$this->auroraModelData->{'User Deleted Key'}; + $this->parsedData['parent'] = $parent; + + + $this->parsedData['user'] = [ @@ -46,7 +63,8 @@ protected function parseModel(): void 'data' => $data, 'deleted_at' => $this->auroraModelData->{'User Deleted Date'}, 'fetched_at' => now(), - 'last_fetched_at' => now() + 'last_fetched_at' => now(), + 'password' => Str::random(60), ]; } diff --git a/app/Transfers/Aurora/FetchAuroraUser.php b/app/Transfers/Aurora/FetchAuroraUser.php index 7c50f31aee..9775abb099 100644 --- a/app/Transfers/Aurora/FetchAuroraUser.php +++ b/app/Transfers/Aurora/FetchAuroraUser.php @@ -22,12 +22,14 @@ protected function parseModel(): void return; } - $parent = null; - $this->parsedData['parent_type'] = 'Guest'; + + if ($this->auroraModelData->{'User Type'} == 'Staff') { - $this->parsedData['parent_type'] = 'Staff'; $parent = $this->parseEmployee($this->organisation->id.':'.$this->auroraModelData->{'User Parent Key'}); + } else { + return; + } $legacyPassword = $this->auroraModelData->{'User Password'}; diff --git a/app/Transfers/Aurora/WithAuroraParsers.php b/app/Transfers/Aurora/WithAuroraParsers.php index 51ccec81cb..c0a5039f27 100644 --- a/app/Transfers/Aurora/WithAuroraParsers.php +++ b/app/Transfers/Aurora/WithAuroraParsers.php @@ -385,6 +385,7 @@ public function parseCustomer(string $sourceId): ?Customer if (!$sourceId) { return null; } + $customer = Customer::withTrashed()->where('source_id', $sourceId)->first(); if (!$customer) { $sourceData = explode(':', $sourceId); diff --git a/app/Transfers/WowsbarOrganisationService.php b/app/Transfers/WowsbarOrganisationService.php index de41843889..40e32407f7 100644 --- a/app/Transfers/WowsbarOrganisationService.php +++ b/app/Transfers/WowsbarOrganisationService.php @@ -394,14 +394,4 @@ public function fetchUser($id): ?array return null; } - - #[\Override] public function fetchDeletedUser($id) - { - // TODO: Implement fetchDeletedUser() method. - } - - #[\Override] public function fetchUser($id) - { - // TODO: Implement fetchUser() method. - } } diff --git a/database/migrations/2022_08_15_000000_create_users_table.php b/database/migrations/2022_08_15_000000_create_users_table.php index 5674adb47e..c906035464 100644 --- a/database/migrations/2022_08_15_000000_create_users_table.php +++ b/database/migrations/2022_08_15_000000_create_users_table.php @@ -50,6 +50,7 @@ public function up(): void $table->datetimeTz('last_fetched_at')->nullable(); $table = $this->softDeletes($table); $table->string('source_id')->nullable()->unique(); + $table->jsonb('sources'); $table->string('legacy_password')->nullable()->index()->comment('source password'); $table->unique(['group_id', 'username']); diff --git a/routes/channels.php b/routes/channels.php index f6019dc920..11a7f04a74 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -60,4 +60,4 @@ Broadcast::channel("header-footer.{website}.preview", function (User $user) { return true; -}); \ No newline at end of file +});