Skip to content

Commit 1327de7

Browse files
committed
Refactor CleanupTrialAssetsTest and Cleanup job to streamline trial asset removal logic and add helper for user subscription setup
1 parent 7290e53 commit 1327de7

2 files changed

Lines changed: 58 additions & 33 deletions

File tree

app/Jobs/Cleanup.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -185,33 +185,39 @@ private function upgradeTrialAssetsToAssets(): void
185185
{
186186
Asset::withoutGlobalScope('tenant_scope')
187187
->whereNotNull('ynh_trial_id')
188-
->chunkById(100, function ($assets) {
189-
$assets->each(function (Asset $asset) {
188+
->get()
189+
->each(function (Asset $asset) {
190190

191-
/** @var ?Trial $trial */
192-
$trial = Trial::withoutGlobalScope('tenant_scope')->find($asset->ynh_trial_id);
191+
Log::debug("Processing {$asset->asset} with trial id {$asset->ynh_trial_id}...");
193192

194-
if (!$trial || $trial->created_at->lt(now()->subDays(15))) {
193+
/** @var ?Trial $trial */
194+
$trial = Trial::withoutGlobalScope('tenant_scope')->find($asset->ynh_trial_id);
195195

196-
/** @var ?User $user */
197-
$user = User::withoutGlobalScope('tenant_scope')->find($asset->created_by);
198-
$tenantId = $user?->tenant_id;
196+
if (!$trial || $trial->created_at->lt(now()->subDays(15))) {
199197

200-
if (!$tenantId) {
201-
$asset->delete();
202-
return;
203-
}
198+
/** @var ?User $user */
199+
$user = User::withoutGlobalScope('tenant_scope')->find($asset->created_by);
200+
$tenantId = $user?->tenant_id;
204201

205-
$users = User::withoutGlobalScope('tenant_scope')->where('tenant_id', $tenantId)->get();
206-
$hasPayingUser = $users->contains(fn(User $user) => $user->subscriber());
202+
if (!$tenantId) {
203+
$asset->delete();
204+
Log::debug("Asset {$asset->asset} with trial id {$asset->ynh_trial_id} removed.");
205+
return;
206+
}
207207

208-
// If the tenant has a subscription, the trial asset is now an asset
209-
if ($hasPayingUser) {
210-
$asset->ynh_trial_id = null;
211-
$asset->save();
212-
}
208+
$users = User::withoutGlobalScope('tenant_scope')->where('tenant_id', $tenantId)->get();
209+
$hasPayingUser = $users->contains(fn(User $user) => $user->subscriber());
210+
211+
// If the tenant has a subscription, the trial asset is now an asset
212+
if (!$hasPayingUser) {
213+
$asset->delete();
214+
Log::debug("Asset {$asset->asset} with trial id {$asset->ynh_trial_id} removed.");
215+
} else {
216+
Log::debug("Trial id {$asset->ynh_trial_id} removed for {$asset->asset}.");
217+
$asset->ynh_trial_id = null;
218+
$asset->save();
213219
}
214-
});
220+
}
215221
});
216222
}
217223

tests/Feature/CleanupTrialAssetsTest.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,47 @@
44

55
use App\Jobs\Cleanup;
66
use App\Models\Asset;
7+
use App\Models\Role;
78
use App\Models\Tenant;
89
use App\Models\Trial;
910
use App\Models\User;
1011
use Carbon\Carbon;
1112
use Illuminate\Support\Facades\DB;
1213
use Tests\TestCaseWithDb;
14+
use Wave\Plan;
1315
use Wave\Subscription;
1416

1517
class CleanupTrialAssetsTest extends TestCaseWithDb
1618
{
19+
private function makeUserPaying(User $user)
20+
{
21+
$role = Role::firstOrCreate(['name' => Role::STANDARD_PLAN, 'guard_name' => 'web']);
22+
$plan = Plan::create([
23+
'name' => 'Basic Plan',
24+
'description' => 'A basic plan',
25+
'features' => 'Feature 1, Feature 2',
26+
'active' => 1,
27+
'role_id' => $role->id,
28+
'monthly_price' => 9.99,
29+
'yearly_price' => 99.99,
30+
'onetime_price' => 0,
31+
'default' => 0,
32+
'currency' => 'EUR',
33+
]);
34+
Subscription::create([
35+
'billable_id' => $user->id,
36+
'billable_type' => 'user',
37+
'plan_id' => $plan->id,
38+
'status' => 'active',
39+
'vendor_slug' => 'slug',
40+
'vendor_product_id' => 'prod',
41+
'vendor_transaction_id' => 'trans',
42+
'vendor_customer_id' => 'cust',
43+
'vendor_subscription_id' => 'sub',
44+
'cycle' => 'month',
45+
]);
46+
}
47+
1748
public function test_cleanup_deletes_old_trial_assets_without_subscription()
1849
{
1950
// Disable foreign key checks for this test or create necessary relations
@@ -49,19 +80,7 @@ public function test_cleanup_sets_trial_id_to_null_if_subscription_exists()
4980
$tenant = Tenant::create(['name' => 'Subscribed Tenant']);
5081
$user = User::factory()->create(['tenant_id' => $tenant->id]);
5182

52-
// Create a subscription for the user
53-
Subscription::create([
54-
'billable_type' => get_class($user),
55-
'billable_id' => $user->id,
56-
'plan_id' => 1, // Assume plan 1 exists or use a real ID
57-
'status' => 'active',
58-
'vendor_slug' => 'paddle',
59-
'vendor_product_id' => 'prod_123',
60-
'vendor_transaction_id' => 'trans_123',
61-
'vendor_customer_id' => 'cust_123',
62-
'vendor_subscription_id' => 'sub_123',
63-
'cycle' => 'month',
64-
]);
83+
$this->makeUserPaying($user);
6584

6685
$oldTrial = Trial::create([
6786
'hash' => 'subscribed_trial',

0 commit comments

Comments
 (0)