Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117775916
D5742.1775242364.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
D5742.1775242364.diff
View Options
diff --git a/src/app/Http/Controllers/API/V4/WalletsController.php b/src/app/Http/Controllers/API/V4/WalletsController.php
--- a/src/app/Http/Controllers/API/V4/WalletsController.php
+++ b/src/app/Http/Controllers/API/V4/WalletsController.php
@@ -245,7 +245,7 @@
return $this->errorResponse(403);
}
- $raw_count = DB::raw('(select count(*) from referrals where referrals.code = code) as refcount');
+ $raw_count = DB::raw('(select count(*) from referrals where referrals.code = referral_codes.code) as refcount');
$codes = ReferralCode::where('user_id', $wallet->user_id)->select('code', 'program_id', $raw_count);
$result = ReferralProgram::withObjectTenantContext($wallet->owner)
@@ -254,6 +254,7 @@
$join->on('referral_programs.id', '=', 'codes.program_id');
})
->select('id', 'name', 'description', 'tenant_id', 'codes.code', 'codes.refcount')
+ ->orderByDesc('created_at')
->get()
->map(static function ($program) use ($wallet) {
if (empty($program->code)) {
diff --git a/src/tests/Feature/Controller/WalletsTest.php b/src/tests/Feature/Controller/WalletsTest.php
--- a/src/tests/Feature/Controller/WalletsTest.php
+++ b/src/tests/Feature/Controller/WalletsTest.php
@@ -317,6 +317,36 @@
$this->assertCount(1, $program->codes->fresh());
$this->assertStringContainsString("/signup/referral/{$code->code}", $json['list'][0]['url']);
$this->assertSame(2, $json['list'][0]['refcount']);
+
+ // Add another program and check if counters are ok
+ $program2 = ReferralProgram::create([
+ 'name' => "Test 2 Referral",
+ 'description' => "Test Referral Description",
+ 'active' => true,
+ ]);
+ $program2->created_at = \now()->subDays(1);
+ $program2->save();
+
+ $response = $this->actingAs($user)->get("api/v4/wallets/{$wallet->id}/referral-programs");
+ $response->assertStatus(200);
+
+ $json = $response->json();
+
+ $this->assertSame($program->id, $json['list'][0]['id']);
+ $this->assertSame($program2->id, $json['list'][1]['id']);
+ $this->assertSame(2, $json['list'][0]['refcount']);
+ $this->assertSame(0, $json['list'][1]['refcount']);
+
+ // And again with codes created
+ $response = $this->actingAs($user)->get("api/v4/wallets/{$wallet->id}/referral-programs");
+ $response->assertStatus(200);
+
+ $json = $response->json();
+
+ $this->assertSame($program->id, $json['list'][0]['id']);
+ $this->assertSame($program2->id, $json['list'][1]['id']);
+ $this->assertSame(2, $json['list'][0]['refcount']);
+ $this->assertSame(0, $json['list'][1]['refcount']);
}
/**
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Apr 3, 6:52 PM (3 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18825758
Default Alt Text
D5742.1775242364.diff (2 KB)
Attached To
Mode
D5742: Referral Programs: Fix referrals count, list newest programs on top
Attached
Detach File
Event Timeline