-
Notifications
You must be signed in to change notification settings - Fork 243
Implement Scheduled Case Retention Evaluation and Automated Case Deletion #8721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+519
−0
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
816505b
Configure new kernal command for evaluating retention
sanjacornelius 4a87002
Implement new job to run and delete cases
sanjacornelius 5875f22
Implement EvaludateCasesRetention command
sanjacornelius 98ab8d8
Implement unit tests
sanjacornelius 9db1e9a
Create caseNumber factory
sanjacornelius b1e3c39
Handle retention policy update deletions
sanjacornelius 082163c
Remove todo
sanjacornelius eca7b57
Disable job if feature flag is not enabled
sanjacornelius f3d2573
Default to 6_month retention period for processes that do not have re…
sanjacornelius dbc2536
set default retention period to 1 year
sanjacornelius 958cbe5
remove unused import
sanjacornelius dca5de8
Update test default retention period
sanjacornelius 23c5f1a
Update EvaluateProcessRetentionJob.php
sanjacornelius 09df9d8
Check if case retention policy is enabled before running job
sanjacornelius da5863a
fix truthy statement
sanjacornelius a24399f
fix issue with cached config
sanjacornelius dfa1502
Resolve failing tests: Cases not being deleted due to improper retent…
sanjacornelius 888d7a9
CusorBot Fix: use subquery instead of loading all IDs into memory
sanjacornelius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Console\Commands; | ||
|
|
||
| use Illuminate\Console\Command; | ||
| use ProcessMaker\Jobs\EvaluateProcessRetentionJob; | ||
| use ProcessMaker\Models\Process; | ||
|
|
||
| class EvaluateCaseRetention extends Command | ||
| { | ||
| /** | ||
| * The name and signature of the console command. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $signature = 'cases:retention:evaluate'; | ||
|
|
||
| /** | ||
| * The console command description. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $description = 'Evaluate and delete cases past their retention period'; | ||
|
|
||
| /** | ||
| * Execute the console command. | ||
| */ | ||
| public function handle() | ||
| { | ||
| // Only run if case retention policy is enabled | ||
| $enabled = config('app.case_retention_policy_enabled', false); | ||
| if (!$enabled) { | ||
| $this->info('Case retention policy is disabled'); | ||
| $this->error('Skipping case retention evaluation'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $this->info('Case retention policy is enabled'); | ||
| $this->info('Evaluating and deleting cases past their retention period'); | ||
|
|
||
| // Process all processes when retention policy is enabled | ||
| // Processes without retention_period will default to 1_year | ||
| Process::chunkById(100, function ($processes) { | ||
| foreach ($processes as $process) { | ||
| dispatch(new EvaluateProcessRetentionJob($process->id)); | ||
| } | ||
| }); | ||
|
|
||
| $this->info('Cases retention evaluation complete'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Jobs; | ||
|
|
||
| use Carbon\Carbon; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
| use Illuminate\Foundation\Queue\Queueable; | ||
| use Illuminate\Support\Facades\Log; | ||
| use ProcessMaker\Models\CaseNumber; | ||
| use ProcessMaker\Models\Process; | ||
| use ProcessMaker\Models\ProcessRequest; | ||
|
|
||
| class EvaluateProcessRetentionJob implements ShouldQueue | ||
| { | ||
| use Queueable; | ||
|
|
||
| /** | ||
| * Create a new job instance. | ||
| */ | ||
| public function __construct(public int $processId) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Execute the job. | ||
| */ | ||
| public function handle(): void | ||
| { | ||
| // Only run if case retention policy is enabled | ||
| $enabled = config('app.case_retention_policy_enabled', false); | ||
| if (!$enabled) { | ||
| return; | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $process = Process::find($this->processId); | ||
| if (!$process) { | ||
| Log::error('CaseRetentionJob: Process not found', ['process_id' => $this->processId]); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Default to 1_year if retention_period is not set | ||
| $retentionPeriod = $process->properties['retention_period'] ?? '1_year'; | ||
| $retentionMonths = match ($retentionPeriod) { | ||
| '6_months' => 6, | ||
| '1_year' => 12, | ||
| '3_years' => 36, | ||
| '5_years' => 60, | ||
| default => 12, // Default to 1_year | ||
| }; | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Default retention_updated_at to now if not set | ||
| // This means the retention policy applies from now for processes without explicit retention settings | ||
| $retentionUpdatedAt = isset($process->properties['retention_updated_at']) | ||
| ? Carbon::parse($process->properties['retention_updated_at']) | ||
| : Carbon::now(); | ||
|
|
||
| // Check if there are any process requests for this process | ||
| // If not, nothing to delete | ||
| if (!ProcessRequest::where('process_id', $this->processId)->exists()) { | ||
| return; | ||
| } | ||
|
|
||
| // Handle two scenarios: | ||
| // 1. Cases created BEFORE retention_updated_at: Delete if older than retention period from retention_updated_at | ||
| // (These cases were subject to the old retention policy, but we apply current retention from update date) | ||
| // 2. Cases created AFTER retention_updated_at: Delete if older than retention period from their creation date | ||
| // (These cases are subject to the new retention policy) | ||
|
|
||
| $now = Carbon::now(); | ||
|
|
||
| // For cases created before retention_updated_at: cutoff is retention_updated_at - retention_period | ||
| $oldCasesCutoff = $retentionUpdatedAt->copy()->subMonths($retentionMonths); | ||
|
|
||
| // For cases created after retention_updated_at: cutoff is now - retention_period | ||
| $newCasesCutoff = $now->copy()->subMonths($retentionMonths); | ||
|
|
||
| // Use subquery to get process request IDs | ||
| $processRequestSubquery = ProcessRequest::where('process_id', $this->processId)->select('id'); | ||
|
|
||
| CaseNumber::whereIn('process_request_id', $processRequestSubquery) | ||
| ->where(function ($query) use ($retentionUpdatedAt, $oldCasesCutoff, $newCasesCutoff) { | ||
| // Cases created before retention_updated_at: delete if created before (retention_updated_at - retention_period) | ||
| $query->where(function ($q) use ($retentionUpdatedAt, $oldCasesCutoff) { | ||
| $q->where('created_at', '<', $retentionUpdatedAt) | ||
| ->where('created_at', '<', $oldCasesCutoff); | ||
| }) | ||
| // Cases created after retention_updated_at: delete if created before (now - retention_period) | ||
| ->orWhere(function ($q) use ($retentionUpdatedAt, $newCasesCutoff) { | ||
| $q->where('created_at', '>=', $retentionUpdatedAt) | ||
| ->where('created_at', '<', $newCasesCutoff); | ||
| }); | ||
sanjacornelius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| ->chunkById(100, function ($cases) { | ||
| $caseIds = $cases->pluck('id'); | ||
| // Delete the cases | ||
| CaseNumber::whereIn('id', $caseIds)->delete(); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // TODO: Add logs to track the number of cases deleted | ||
| // Get deleted timestamp | ||
| // $deletedAt = Carbon::now(); | ||
| // RetentionPolicyLog::record($process->id, $caseIds, $deletedAt); | ||
| }); | ||
sanjacornelius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
database/factories/ProcessMaker/Models/CaseNumberFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| namespace Database\Factories\ProcessMaker\Models; | ||
|
|
||
| use Illuminate\Database\Eloquent\Factories\Factory; | ||
| use ProcessMaker\Models\CaseNumber; | ||
| use ProcessMaker\Models\ProcessRequest; | ||
|
|
||
| class CaseNumberFactory extends Factory | ||
| { | ||
| protected $model = CaseNumber::class; | ||
|
|
||
| public function definition(): array | ||
| { | ||
| return [ | ||
| 'process_request_id' => function () { | ||
| return ProcessRequest::factory()->create()->getKey(); | ||
| }, | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.