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

View File

@@ -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;
}
}

View File

@@ -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'),
]
]
];

View File

@@ -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
*

View File

@@ -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');
}
}

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)
->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']
]);
}
}