Mass changes
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(Tests\TestCase::class, RefreshDatabase::class);
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('returns client cases when searching by contract reference', function () {
|
||||
// Arrange: create a user and authenticate
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Account;
|
||||
use App\Models\ClientCase;
|
||||
use App\Models\Contract;
|
||||
use App\Models\Email;
|
||||
use App\Models\Import;
|
||||
use App\Models\Person\AddressType;
|
||||
use App\Models\Person\Person;
|
||||
use App\Models\Person\PersonAddress;
|
||||
use App\Models\Person\PersonPhone;
|
||||
use App\Models\Person\PhoneType;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
it('attaches chain entities (client_case, person, email, phone, address) for existing contract', function () {
|
||||
Storage::fake('local');
|
||||
|
||||
$user = User::factory()->create();
|
||||
\Illuminate\Support\Facades\Auth::login($user);
|
||||
$person = Person::factory()->create(['user_id' => $user->id]);
|
||||
$clientCase = ClientCase::factory()->create(['person_id' => $person->id, 'client_ref' => 'CC-1']);
|
||||
$contract = Contract::factory()->create(['reference' => 'C-CHAIN', 'client_case_id' => $clientCase->id]);
|
||||
$typeId = DB::table('account_types')->insertGetId(['name' => 'Current', 'description' => 'Test', 'created_at' => now(), 'updated_at' => now()]);
|
||||
Account::create(['reference' => 'AC-1', 'contract_id' => $contract->id, 'type_id' => $typeId]);
|
||||
Email::factory()->create(['person_id' => $person->id, 'value' => 'chain@example.com']);
|
||||
$phoneType = PhoneType::factory()->create();
|
||||
$addressType = AddressType::factory()->create();
|
||||
PersonPhone::factory()->create(['person_id' => $person->id, 'nu' => '123456', 'type_id' => $phoneType->id]);
|
||||
PersonAddress::factory()->create(['person_id' => $person->id, 'address' => 'Main St 1', 'country' => 'SI', 'type_id' => $addressType->id]);
|
||||
|
||||
$csv = "contract_ref\nC-CHAIN";
|
||||
Storage::disk('local')->put('imports/chain.csv', $csv);
|
||||
|
||||
$import = Import::factory()->create([
|
||||
'disk' => 'local',
|
||||
'path' => 'imports/chain.csv',
|
||||
'meta' => ['has_header' => true, 'forced_delimiter' => ','],
|
||||
]);
|
||||
|
||||
// Map only contract.reference (others should be derived via chain)
|
||||
DB::table('import_mappings')->insert([
|
||||
['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'contract.reference', 'position' => 1],
|
||||
// declare roots so they appear in simulation (simulate mapping presence with dummy value fields referencing same column) optional
|
||||
['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'person.first_name', 'position' => 2],
|
||||
['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'client_case.title', 'position' => 3],
|
||||
['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'email.value', 'position' => 4],
|
||||
['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'phone.nu', 'position' => 5],
|
||||
['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'address.address', 'position' => 6],
|
||||
]);
|
||||
|
||||
$service = app(\App\Services\ImportSimulationService::class);
|
||||
$result = $service->simulate($import, 10, false);
|
||||
|
||||
$row = $result['rows'][0]['entities'];
|
||||
|
||||
expect($row['contract']['action'])->toBe('update');
|
||||
expect($row['client_case']['existing_chain'] ?? false)->toBeTrue();
|
||||
expect($row['person']['existing_chain'] ?? false)->toBeTrue();
|
||||
expect($row['email']['existing_chain'] ?? false)->toBeTrue();
|
||||
expect($row['phone']['existing_chain'] ?? false)->toBeTrue();
|
||||
expect($row['address']['existing_chain'] ?? false)->toBeTrue();
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Email;
|
||||
use App\Models\Import;
|
||||
use App\Models\ImportEntity;
|
||||
use App\Models\Person\Person;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
it('simulates generic entities with deduplication and filtering', function () {
|
||||
Storage::fake('local');
|
||||
// Ensure import_entities seeds (or minimal row) exist
|
||||
ImportEntity::query()->firstOrCreate(['key' => 'emails'], [
|
||||
'canonical_root' => 'email',
|
||||
'label' => 'Emails',
|
||||
'fields' => ['value'],
|
||||
]);
|
||||
|
||||
// Existing email to trigger duplicate_db (attach minimal person)
|
||||
$person = Person::factory()->create();
|
||||
Email::create(['value' => 'test@example.com', 'person_id' => $person->id]);
|
||||
|
||||
$csv = "email\nTest@example.com"; // will normalize to same identity
|
||||
Storage::disk('local')->put('imports/sample.csv', $csv);
|
||||
|
||||
$import = Import::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'user_id' => User::factory()->create()->id,
|
||||
'import_template_id' => null,
|
||||
'client_id' => null,
|
||||
'source_type' => 'csv',
|
||||
'file_name' => 'sample.csv',
|
||||
'original_name' => 'sample.csv',
|
||||
'disk' => 'local',
|
||||
'path' => 'imports/sample.csv',
|
||||
'size' => strlen($csv),
|
||||
'status' => 'uploaded',
|
||||
'meta' => [
|
||||
'has_header' => true,
|
||||
'columns' => ['email'],
|
||||
],
|
||||
]);
|
||||
|
||||
// Mapping: email.value -> column email
|
||||
\DB::table('import_mappings')->insert([
|
||||
'import_id' => $import->id,
|
||||
'source_column' => 'email',
|
||||
'target_field' => 'email.value',
|
||||
'position' => 0,
|
||||
]);
|
||||
|
||||
$service = app(\App\Services\ImportSimulationService::class);
|
||||
$result = $service->simulate($import, 10, false);
|
||||
|
||||
expect($result['entities'])->toContain('email');
|
||||
expect($result['summaries']['email']['total_rows'])->toBe(1);
|
||||
// Because existing duplicate in DB
|
||||
expect($result['summaries']['email']['duplicate_db'] ?? 0)->toBe(1);
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Account;
|
||||
use App\Models\Contract;
|
||||
use App\Models\Email;
|
||||
use App\Models\Import;
|
||||
use App\Models\Person\Person;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('simulates multiple roots with duplicate flags', function () {
|
||||
$user = User::factory()->create();
|
||||
Auth::login($user);
|
||||
|
||||
$contract = Contract::factory()->create(['reference' => 'C-1']);
|
||||
$typeId = DB::table('account_types')->insertGetId(['name' => 'Current', 'description' => 'Test', 'created_at' => now(), 'updated_at' => now()]);
|
||||
$account = Account::create(['reference' => 'A-1', 'contract_id' => $contract->id, 'type_id' => $typeId]);
|
||||
|
||||
Email::factory()->create(['value' => 'test@example.com', 'person_id' => Person::factory()->create()->id]);
|
||||
|
||||
$csv = implode("\n", [
|
||||
'contract_ref,account_ref,payment_ref,email_value,payment_amount',
|
||||
'C-1,A-1,P-1,test@example.com,10',
|
||||
'C-1,A-1,P-1,NEW@example.com,10',
|
||||
'C-1,A-1,P-2,test@example.com,15',
|
||||
]);
|
||||
|
||||
Storage::fake('local');
|
||||
Storage::disk('local')->put('imports/test.csv', $csv);
|
||||
|
||||
$import = Import::factory()->create([
|
||||
'disk' => 'local',
|
||||
'path' => 'imports/test.csv',
|
||||
'meta' => [
|
||||
'has_header' => true,
|
||||
'forced_delimiter' => ',',
|
||||
],
|
||||
]);
|
||||
|
||||
DB::table('import_mappings')->insert([
|
||||
['import_id' => $import->id, 'source_column' => 'contract_ref', 'target_field' => 'contract.reference', 'position' => 1],
|
||||
['import_id' => $import->id, 'source_column' => 'account_ref', 'target_field' => 'account.reference', 'position' => 2],
|
||||
['import_id' => $import->id, 'source_column' => 'payment_ref', 'target_field' => 'payment.reference', 'position' => 3],
|
||||
['import_id' => $import->id, 'source_column' => 'email_value', 'target_field' => 'email.value', 'position' => 4],
|
||||
['import_id' => $import->id, 'source_column' => 'payment_amount', 'target_field' => 'payment.amount', 'position' => 5],
|
||||
]);
|
||||
|
||||
$service = app(\App\Services\ImportSimulationService::class);
|
||||
$result = $service->simulate($import, 100, false);
|
||||
|
||||
expect($result['entities'])->toContain('contract', 'account', 'payment', 'email');
|
||||
expect($result['rows'][0]['entities']['email']['duplicate_db'] ?? false)->toBeTrue();
|
||||
expect($result['rows'][1]['entities']['payment']['status'])->toBe('duplicate');
|
||||
expect($result['rows'][2]['entities']['payment']['status'])->toBe('ok');
|
||||
});
|
||||
Reference in New Issue
Block a user