Page MenuHomePhorge

D5742.1774892054.diff
No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None

D5742.1774892054.diff

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']);
}
/**
@@ -326,12 +356,15 @@
{
Carbon::setTestNow(Carbon::createFromDate(2022, 2, 2));
- $john = $this->getTestUser('john@kolab.org');
- $jack = $this->getTestUser('jack@kolab.org');
- $wallet = $john->wallets()->first();
+ $package_kolab = Package::where('title', 'kolab')->first();
+ $user = $this->getTestUser('wallets-controller@kolabnow.com');
+ $user->assignPackage($package_kolab);
+ $wallet = $user->wallets()->first();
$wallet->balance = 1000;
$wallet->save();
+ $jack = $this->getTestUser('jack@kolab.org');
+
// Accessing a wallet of someone else
$response = $this->actingAs($jack)->get("api/v4/wallets/{$wallet->id}");
$response->assertStatus(403);
@@ -341,7 +374,7 @@
$response->assertStatus(404);
// Wallet owner
- $response = $this->actingAs($john)->get("api/v4/wallets/{$wallet->id}");
+ $response = $this->actingAs($user)->get("api/v4/wallets/{$wallet->id}");
$response->assertStatus(200);
$json = $response->json();
@@ -350,8 +383,8 @@
$this->assertSame('CHF', $json['currency']);
$this->assertSame($wallet->balance, $json['balance']);
$this->assertTrue(empty($json['description']));
- $this->assertStringContainsString('until about 2025-08-11', $json['notice']);
- $this->assertSame('2025-08-11', $json['nextPaymentDate']);
+ $this->assertStringContainsString('until about 2022-04-02 (2 months)', $json['notice']);
+ $this->assertSame('2022-04-02', $json['nextPaymentDate']);
}
/**

File Metadata

Mime Type
text/plain
Expires
Mon, Mar 30, 5:34 PM (2 d, 17 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18790449
Default Alt Text
D5742.1774892054.diff (4 KB)

Event Timeline