updates to UI and add archiving option

This commit is contained in:
Simon Pocrnjič
2025-10-05 19:45:49 +02:00
parent fe91c7e4bc
commit bab9d6561f
50 changed files with 3337 additions and 416 deletions
@@ -0,0 +1,56 @@
<?php
namespace Tests\Feature;
use App\Models\Account;
use App\Models\ArchiveSetting;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ArchiveContractAccountChainTest extends \Tests\TestCase
{
use RefreshDatabase;
public function test_archives_account_via_contracts_account_chain(): void
{
$case = ClientCase::factory()->create();
$contract = Contract::factory()->create([
'client_case_id' => $case->id,
'active' => 1,
]);
$accountTypeId = \DB::table('account_types')->insertGetId([
'name' => 'Type',
'description' => null,
'created_at' => now(),
'updated_at' => now(),
'deleted_at' => null,
]);
$account = Account::create([
'contract_id' => $contract->id,
'type_id' => $accountTypeId,
'active' => 1,
'initial_amount' => 0,
'balance_amount' => 0,
]);
ArchiveSetting::factory()->create([
'enabled' => true,
'strategy' => 'manual',
'soft' => true,
'entities' => [
['table' => 'contracts', 'focus' => true],
['table' => 'contracts.account'],
],
]);
$user = User::factory()->create();
$this->actingAs($user);
$this->post(route('clientCase.contract.archive', ['client_case' => $case->uuid, 'uuid' => $contract->uuid]))
->assertRedirect();
$this->assertDatabaseHas('accounts', ['id' => $account->id, 'active' => 0]);
}
}
@@ -0,0 +1,84 @@
<?php
namespace Tests\Feature;
use App\Models\Account;
use App\Models\ArchiveSetting;
use App\Models\Booking;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\Payment;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ArchiveContractChainedEntitiesTest extends \Tests\TestCase
{
use RefreshDatabase;
public function test_archives_payments_and_bookings_via_account_chain(): void
{
$case = ClientCase::factory()->create();
$contract = Contract::factory()->create([
'client_case_id' => $case->id,
'active' => 1,
]);
// Create account tied to contract
// Minimal account type requirement
$accountTypeId = \DB::table('account_types')->insertGetId([
'name' => 'Test Type',
'description' => 'Temp',
'created_at' => now(),
'updated_at' => now(),
'deleted_at' => null,
]);
$account = Account::create([
'contract_id' => $contract->id,
'type_id' => $accountTypeId,
'active' => 1,
'initial_amount' => 0,
'balance_amount' => 0,
]);
// Seed payments & bookings for that account
$payment = Payment::create([
'account_id' => $account->id,
'amount_cents' => 10000,
'currency' => 'EUR',
'reference' => 'P-TEST',
'paid_at' => now(),
'meta' => json_encode([]),
]);
$booking = Booking::create([
'account_id' => $account->id,
'payment_id' => $payment->id,
'amount_cents' => 10000,
'type' => 'debit',
'description' => 'Test Booking',
'booked_at' => now(),
]);
ArchiveSetting::factory()->create([
'enabled' => true,
'strategy' => 'manual',
'soft' => true,
'entities' => [
['table' => 'contracts', 'focus' => true],
['table' => 'account.payments'],
['table' => 'account.bookings'],
],
]);
$user = User::factory()->create();
$this->actingAs($user);
$this->post(route('clientCase.contract.archive', ['client_case' => $case->uuid, 'uuid' => $contract->uuid]))
->assertRedirect();
// Refresh models
$payment->refresh();
$booking->refresh();
$this->assertDatabaseHas('payments', ['id' => $payment->id, 'active' => 0]);
$this->assertDatabaseHas('bookings', ['id' => $booking->id, 'active' => 0]);
}
}
@@ -0,0 +1,66 @@
<?php
namespace Tests\Feature;
use App\Models\ArchiveSetting;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
class ArchiveContractSegmentTest extends \Tests\TestCase
{
use RefreshDatabase;
public function test_moves_contract_to_archive_segment_when_setting_has_segment_id(): void
{
$case = ClientCase::factory()->create();
$contract = Contract::factory()->create([
'client_case_id' => $case->id,
'active' => 1,
]);
$originalSegment = Segment::factory()->create(['active' => true]);
$archiveSegment = Segment::factory()->create(['active' => true]);
DB::table('client_case_segment')->insert([
'client_case_id' => $case->id,
'segment_id' => $originalSegment->id,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('contract_segment')->insert([
'contract_id' => $contract->id,
'segment_id' => $originalSegment->id,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
ArchiveSetting::factory()->create([
'enabled' => true,
'strategy' => 'manual',
'segment_id' => $archiveSegment->id,
'entities' => [
['table' => 'contracts', 'focus' => true],
],
]);
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post(route('clientCase.contract.archive', ['client_case' => $case->uuid, 'uuid' => $contract->uuid]));
$response->assertRedirect();
$activePivots = DB::table('contract_segment')
->where('contract_id', $contract->id)
->where('active', true)
->pluck('segment_id');
$this->assertTrue($activePivots->contains($archiveSegment->id));
$this->assertFalse($activePivots->contains($originalSegment->id));
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
use App\Models\ArchiveSetting;
use App\Models\ClientCase;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
/** @var \Tests\TestCase $this */
it('archives a single contract using archive settings', function () {
$user = User::factory()->create();
test()->actingAs($user);
$case = ClientCase::factory()->create(['active' => 1]);
$typeId = DB::table('contract_types')->insertGetId([
'name' => 'Standard',
'description' => 'Test',
'created_at' => now(),
'updated_at' => now(),
]);
$contract = $case->contracts()->create([
'uuid' => (string) Str::uuid(),
'reference' => 'T-TEST',
'start_date' => now()->toDateString(),
'end_date' => null,
'type_id' => $typeId,
'active' => 1,
]);
ArchiveSetting::factory()->create([
'entities' => [
['table' => 'contracts', 'conditions' => ['where' => ['client_case_id' => $case->id]]],
],
'strategy' => 'immediate',
'soft' => true,
'enabled' => true,
]);
test()->post(route('clientCase.contract.archive', [$case->uuid, $contract->uuid]))
->assertRedirect();
$contract->refresh();
expect($contract->active)->toBe(0);
});
+48
View File
@@ -0,0 +1,48 @@
<?php
use App\Models\ArchiveRun;
use App\Models\ArchiveSetting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
it('runs an archive setting via run-now endpoint and logs audit', function () {
$docId = null; // Define docId earlier
$user = User::factory()->create();
test()->actingAs($user);
// Insert sample document older than 200 days (minimal required columns)
$docId = DB::table('documents')->insertGetId([
'uuid' => (string) Str::uuid(),
'documentable_type' => 'App\\Models\\ClientCase', // generic
'documentable_id' => 1,
'name' => 'Old Doc',
'file_name' => 'old.txt',
'original_name' => 'old.txt',
'disk' => 'public',
'path' => 'documents/old.txt',
'mime_type' => 'text/plain',
'active' => 1,
'created_at' => now()->subDays(210),
'updated_at' => now()->subDays(210),
]);
$setting = ArchiveSetting::factory()->create([
'entities' => [[
'table' => 'documents',
'conditions' => ['older_than_days' => 180],
]],
'enabled' => true,
'strategy' => 'immediate',
]);
test()->post(route('settings.archive.run', $setting->id))->assertRedirect();
$setting->refresh();
$docRow = DB::table('documents')->where('id', $docId)->first();
expect(ArchiveRun::count())->toBe(1)
->and($docRow->active)->toBe(0);
});
+61
View File
@@ -0,0 +1,61 @@
<?php
/**
* @method \Illuminate\Testing\TestResponse post(string $uri, array $data = [])
* @method \Illuminate\Testing\TestResponse put(string $uri, array $data = [])
* @method \Illuminate\Testing\TestResponse delete(string $uri, array $data = [])
* @method $this actingAs(\App\Models\User $user, ?string $guard = null)
*/
use App\Models\ArchiveSetting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
/** @var \Tests\TestCase $this */
it('creates an archive setting', function () {
$user = User::factory()->create();
test()->actingAs($user);
$response = test()->post(route('settings.archive.store'), [
'entities' => [
['table' => 'documents', 'conditions' => ['older_than_days' => 30]],
],
'strategy' => 'immediate',
'soft' => true,
'enabled' => true,
]);
$response->assertRedirect();
expect(ArchiveSetting::count())->toBe(1);
});
it('updates an archive setting', function () {
$user = User::factory()->create();
test()->actingAs($user);
$setting = ArchiveSetting::factory()->create();
$response = test()->put(route('settings.archive.update', $setting->id), [
'entities' => $setting->entities,
'strategy' => 'queued',
'soft' => false,
'enabled' => false,
]);
$response->assertRedirect();
$setting->refresh();
expect($setting->strategy)->toBe('queued')
->and($setting->soft)->toBeFalse()
->and($setting->enabled)->toBeFalse();
});
it('deletes an archive setting', function () {
$user = User::factory()->create();
test()->actingAs($user);
$setting = ArchiveSetting::factory()->create();
$response = test()->delete(route('settings.archive.destroy', $setting->id));
$response->assertRedirect();
expect(ArchiveSetting::withTrashed()->count())->toBe(1) // soft deleted
->and(ArchiveSetting::count())->toBe(0);
});
+50
View File
@@ -0,0 +1,50 @@
<?php
use App\Models\ArchiveSetting;
use App\Models\ClientCase;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
it('reactivates a single contract when a reactivate rule exists', function () {
$user = User::factory()->create();
test()->actingAs($user);
$case = ClientCase::factory()->create(['active' => 1]);
$typeId = DB::table('contract_types')->insertGetId([
'name' => 'Standard',
'description' => 'Test',
'created_at' => now(),
'updated_at' => now(),
]);
$contract = $case->contracts()->create([
'uuid' => (string) Str::uuid(),
'reference' => 'T-REACT',
'start_date' => now()->toDateString(),
'end_date' => null,
'type_id' => $typeId,
'active' => 0, // initially archived
'deleted_at' => now(), // also soft deleted to test clearing
]);
ArchiveSetting::factory()->create([
'entities' => [
['table' => 'contracts', 'conditions' => ['where' => ['client_case_id' => $case->id]]],
],
'strategy' => 'immediate',
'soft' => true,
'enabled' => true,
'reactivate' => true,
]);
test()->post(route('clientCase.contract.archive', ['client_case' => $case->uuid, 'uuid' => $contract->uuid]))
->assertRedirect();
$contract->refresh();
expect($contract->active)->toBe(1)
->and($contract->deleted_at)->toBeNull();
});