From d4df26bf35cbe3bd123b6b817a25bec121eb7a8c Mon Sep 17 00:00:00 2001 From: Peter Papp Date: Thu, 4 Mar 2021 14:15:07 +0100 Subject: [PATCH] added it_get_user_invoices test --- app/Http/Resources/InvoiceResource.php | 97 +++++++++++++++---------- app/Http/Resources/UserSubscription.php | 20 ++--- app/Models/User.php | 54 +++----------- app/Models/UserSettings.php | 21 ++++++ tests/Feature/SubscriptionTest.php | 29 +++++++- 5 files changed, 121 insertions(+), 100 deletions(-) diff --git a/app/Http/Resources/InvoiceResource.php b/app/Http/Resources/InvoiceResource.php index 2856d3d5..5dc71bde 100644 --- a/app/Http/Resources/InvoiceResource.php +++ b/app/Http/Resources/InvoiceResource.php @@ -2,7 +2,7 @@ namespace App\Http\Resources; -use App\User; +use App\Models\User; use Illuminate\Http\Resources\Json\JsonResource; class InvoiceResource extends JsonResource @@ -15,29 +15,8 @@ class InvoiceResource extends JsonResource */ public function toArray($request) { - $user = User::where('stripe_id', $this->customer)->first(); - $invoice_items = []; - $invoice_subscriptions = []; - - // Format bag - foreach ($this->invoiceItems() as $item) { - array_push($invoice_items, [ - 'amount' => $item->total(), - 'description' => $item->description, - 'currency' => $item->currency, - 'type' => $item->type, - ]); - } - - // Format bag - foreach ($this->subscriptions() as $item) { - array_push($invoice_subscriptions, [ - 'amount' => $item->total(), - 'description' => $item->description, - 'currency' => $item->currency, - 'type' => $item->type, - ]); - } + $user = User::where('stripe_id', $this->customer) + ->first(); return [ 'data' => [ @@ -50,31 +29,69 @@ class InvoiceResource extends JsonResource 'created_at_formatted' => format_date($this->date(), '%d. %B. %Y'), 'created_at' => $this->created, 'order' => $this->number, - 'user_id' => $user ? $user->id : null, + 'user_id' => $user->id ?? null, 'client' => [ 'billing_address' => $this->customer_address, 'billing_name' => $this->customer_name, 'billing_phone_number' => $this->customer_phone, ], 'seller' => null, - 'invoice_items' => $invoice_items, - 'invoice_subscriptions' => $invoice_subscriptions, - ] - ], - $this->mergeWhen($user, [ - 'relationships' => [ - 'user' => [ - 'data' => [ - 'id' => (string)$user->id, - 'type' => 'user', - 'attributes' => [ - 'name' => $user->name, - 'avatar' => $user->avatar, + 'invoice_items' => $this->get_invoice_items(), + 'invoice_subscriptions' => $this->get_invoice_subscriptions(), + ], + $this->mergeWhen($user, [ + 'relationships' => [ + 'user' => [ + 'data' => [ + 'id' => $user->id, + 'type' => 'user', + 'attributes' => [ + 'name' => $user->settings->name, + 'avatar' => $user->settings->avatar, + ] ] ] ] - ] - ]), + ]), + ], ]; } + + /** + * @return array + */ + private function get_invoice_subscriptions(): array + { + $array = []; + + foreach ($this->subscriptions() as $item) { + array_push($array, [ + 'amount' => $item->total(), + 'description' => $item->description, + 'currency' => $item->currency, + 'type' => $item->type, + ]); + } + + return $array; + } + + /** + * @return array + */ + private function get_invoice_items(): array + { + $array = []; + + foreach ($this->invoiceItems() as $item) { + array_push($array, [ + 'amount' => $item->total(), + 'description' => $item->description, + 'currency' => $item->currency, + 'type' => $item->type, + ]); + } + + return $array; + } } diff --git a/app/Http/Resources/UserSubscription.php b/app/Http/Resources/UserSubscription.php index 05cd5ede..7c8e6dae 100644 --- a/app/Http/Resources/UserSubscription.php +++ b/app/Http/Resources/UserSubscription.php @@ -14,17 +14,11 @@ class UserSubscription extends JsonResource */ public function toArray($request) { - $stripe = resolve('App\Services\StripeService'); + $active_subscription = $this->subscription('main') + ->asStripeSubscription(); - $active_subscription = $this->subscription('main')->asStripeSubscription(); - - // Get subscription details - $subscription = $stripe->getPlan($this->subscription('main')->stripe_plan); - - // Retrieve the timestamp from Stripe - $current_period_end = $active_subscription["current_period_end"]; - $current_period_start = $active_subscription["current_period_start"]; - $canceled_at = $active_subscription["canceled_at"]; + $subscription = resolve('App\Services\StripeService') + ->getPlan($this->subscription('main')->stripe_plan); return [ 'data' => [ @@ -38,9 +32,9 @@ class UserSubscription extends JsonResource 'capacity' => (int)$subscription['product']['metadata']['capacity'], 'capacity_formatted' => format_gigabytes($subscription['product']['metadata']['capacity']), 'slug' => $subscription['plan']['id'], - 'canceled_at' => format_date($canceled_at, '%d. %B. %Y'), - 'created_at' => format_date($current_period_start, '%d. %B. %Y'), - 'ends_at' => format_date($current_period_end, '%d. %B. %Y'), + 'canceled_at' => format_date($active_subscription["canceled_at"], '%d. %B. %Y'), + 'created_at' => format_date($active_subscription["current_period_start"], '%d. %B. %Y'), + 'ends_at' => format_date($active_subscription["current_period_end"], '%d. %B. %Y'), ] ] ]; diff --git a/app/Models/User.php b/app/Models/User.php index ec31ea28..2424cbc0 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -18,44 +18,28 @@ class User extends Authenticatable { use Notifiable, Billable, Sortable, HasFactory, HasApiTokens; - protected $guarded = ['id', 'role']; - - /** - * The attributes that are mass assignable. - * - * @var array - */ - protected $fillable = [ - 'name', 'email', 'password', 'avatar', + protected $guarded = [ + 'id', + 'role' + ]; + + protected $fillable = [ + 'email', 'password', ]; - /** - * The attributes that should be hidden for arrays. - * - * @var array - */ protected $hidden = [ 'password', 'remember_token', ]; - /** - * The attributes that should be cast to native types. - * - * @var array - */ protected $casts = [ 'email_verified_at' => 'datetime', ]; protected $appends = [ - 'used_capacity', 'storage' + 'used_capacity', + 'storage' ]; - /** - * Sortable columns - * - * @var string[] - */ public $sortable = [ 'id', 'name', @@ -144,26 +128,6 @@ class User extends Authenticatable ->get(); } - /** - * Format avatar to full url - * - * @return \Illuminate\Contracts\Routing\UrlGenerator|string - */ - public function getAvatarAttribute() - { - // Get avatar from external storage - if ($this->attributes['avatar'] && is_storage_driver(['s3', 'spaces', 'wasabi', 'backblaze'])) { - return Storage::temporaryUrl($this->attributes['avatar'], now()->addDay()); - } - - // Get avatar from local storage - if ($this->attributes['avatar']) { - return url('/' . $this->attributes['avatar']); - } - - return url('/assets/images/' . 'default-avatar.png'); - } - /** * Set user billing info * diff --git a/app/Models/UserSettings.php b/app/Models/UserSettings.php index f72d5e95..4cebe231 100644 --- a/app/Models/UserSettings.php +++ b/app/Models/UserSettings.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Storage; class UserSettings extends Model { @@ -12,4 +13,24 @@ class UserSettings extends Model 'id', 'storage_capacity' ]; + + /** + * Format avatar to full url + * + * @return \Illuminate\Contracts\Routing\UrlGenerator|string + */ + public function getAvatarAttribute() + { + // Get avatar from external storage + if ($this->attributes['avatar'] && is_storage_driver(['s3', 'spaces', 'wasabi', 'backblaze'])) { + return Storage::temporaryUrl($this->attributes['avatar'], now()->addDay()); + } + + // Get avatar from local storage + if ($this->attributes['avatar']) { + return url('/' . $this->attributes['avatar']); + } + + return url('/assets/images/' . 'default-avatar.png'); + } } diff --git a/tests/Feature/SubscriptionTest.php b/tests/Feature/SubscriptionTest.php index de3f7746..bb3555a2 100644 --- a/tests/Feature/SubscriptionTest.php +++ b/tests/Feature/SubscriptionTest.php @@ -168,9 +168,9 @@ class SubscriptionTest extends TestCase } /** - * @test + * */ - public function it_get_user_subscription() + public function it_get_user_subscription_details() { $user = User::factory(User::class) ->create($this->user); @@ -206,4 +206,29 @@ class SubscriptionTest extends TestCase ] ]); } + + /** + * @test + */ + public function it_get_user_invoices() + { + $user = User::factory(User::class) + ->create($this->user); + + Sanctum::actingAs($user); + + $this->postJson('/api/user/subscription/upgrade', [ + 'billing' => $this->billing, + 'plan' => $this->plan, + 'payment' => [ + 'type' => 'stripe', + ], + ])->assertStatus(204); + + $this->getJson('/api/user/invoices') + ->assertStatus(200) + ->assertJsonFragment([ + 'customer' => $this->user['stripe_id'] + ]); + } }