added it_get_user_invoices test

This commit is contained in:
Peter Papp
2021-03-04 14:15:07 +01:00
parent 7e4bd191d7
commit d4df26bf35
5 changed files with 121 additions and 100 deletions
+48 -31
View File
@@ -2,7 +2,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\User; use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
class InvoiceResource extends JsonResource class InvoiceResource extends JsonResource
@@ -15,29 +15,8 @@ class InvoiceResource extends JsonResource
*/ */
public function toArray($request) public function toArray($request)
{ {
$user = User::where('stripe_id', $this->customer)->first(); $user = User::where('stripe_id', $this->customer)
$invoice_items = []; ->first();
$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,
]);
}
return [ return [
'data' => [ 'data' => [
@@ -50,31 +29,69 @@ class InvoiceResource extends JsonResource
'created_at_formatted' => format_date($this->date(), '%d. %B. %Y'), 'created_at_formatted' => format_date($this->date(), '%d. %B. %Y'),
'created_at' => $this->created, 'created_at' => $this->created,
'order' => $this->number, 'order' => $this->number,
'user_id' => $user ? $user->id : null, 'user_id' => $user->id ?? null,
'client' => [ 'client' => [
'billing_address' => $this->customer_address, 'billing_address' => $this->customer_address,
'billing_name' => $this->customer_name, 'billing_name' => $this->customer_name,
'billing_phone_number' => $this->customer_phone, 'billing_phone_number' => $this->customer_phone,
], ],
'seller' => null, 'seller' => null,
'invoice_items' => $invoice_items, 'invoice_items' => $this->get_invoice_items(),
'invoice_subscriptions' => $invoice_subscriptions, 'invoice_subscriptions' => $this->get_invoice_subscriptions(),
]
], ],
$this->mergeWhen($user, [ $this->mergeWhen($user, [
'relationships' => [ 'relationships' => [
'user' => [ 'user' => [
'data' => [ 'data' => [
'id' => (string)$user->id, 'id' => $user->id,
'type' => 'user', 'type' => 'user',
'attributes' => [ 'attributes' => [
'name' => $user->name, 'name' => $user->settings->name,
'avatar' => $user->avatar, '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;
}
} }
+7 -13
View File
@@ -14,17 +14,11 @@ class UserSubscription extends JsonResource
*/ */
public function toArray($request) public function toArray($request)
{ {
$stripe = resolve('App\Services\StripeService'); $active_subscription = $this->subscription('main')
->asStripeSubscription();
$active_subscription = $this->subscription('main')->asStripeSubscription(); $subscription = resolve('App\Services\StripeService')
->getPlan($this->subscription('main')->stripe_plan);
// 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"];
return [ return [
'data' => [ 'data' => [
@@ -38,9 +32,9 @@ class UserSubscription extends JsonResource
'capacity' => (int)$subscription['product']['metadata']['capacity'], 'capacity' => (int)$subscription['product']['metadata']['capacity'],
'capacity_formatted' => format_gigabytes($subscription['product']['metadata']['capacity']), 'capacity_formatted' => format_gigabytes($subscription['product']['metadata']['capacity']),
'slug' => $subscription['plan']['id'], 'slug' => $subscription['plan']['id'],
'canceled_at' => format_date($canceled_at, '%d. %B. %Y'), 'canceled_at' => format_date($active_subscription["canceled_at"], '%d. %B. %Y'),
'created_at' => format_date($current_period_start, '%d. %B. %Y'), 'created_at' => format_date($active_subscription["current_period_start"], '%d. %B. %Y'),
'ends_at' => format_date($current_period_end, '%d. %B. %Y'), 'ends_at' => format_date($active_subscription["current_period_end"], '%d. %B. %Y'),
] ]
] ]
]; ];
+9 -45
View File
@@ -18,44 +18,28 @@ class User extends Authenticatable
{ {
use Notifiable, Billable, Sortable, HasFactory, HasApiTokens; use Notifiable, Billable, Sortable, HasFactory, HasApiTokens;
protected $guarded = ['id', 'role']; protected $guarded = [
'id',
/** 'role'
* The attributes that are mass assignable. ];
*
* @var array protected $fillable = [
*/ 'email', 'password',
protected $fillable = [
'name', 'email', 'password', 'avatar',
]; ];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [ protected $hidden = [
'password', 'remember_token', 'password', 'remember_token',
]; ];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [ protected $casts = [
'email_verified_at' => 'datetime', 'email_verified_at' => 'datetime',
]; ];
protected $appends = [ protected $appends = [
'used_capacity', 'storage' 'used_capacity',
'storage'
]; ];
/**
* Sortable columns
*
* @var string[]
*/
public $sortable = [ public $sortable = [
'id', 'id',
'name', 'name',
@@ -144,26 +128,6 @@ class User extends Authenticatable
->get(); ->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 * Set user billing info
* *
+21
View File
@@ -3,6 +3,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class UserSettings extends Model class UserSettings extends Model
{ {
@@ -12,4 +13,24 @@ class UserSettings extends Model
'id', 'id',
'storage_capacity' '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');
}
} }
+27 -2
View File
@@ -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) $user = User::factory(User::class)
->create($this->user); ->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']
]);
}
} }