mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-29 03:10:51 +00:00
format usage estimates refactoring & test
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\Actions;
|
||||
|
||||
use ByteUnits\Metric;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class FormatUsageEstimatesAction
|
||||
{
|
||||
public function __invoke(string $currency, Collection $usage)
|
||||
{
|
||||
return $usage->map(function ($estimate) use ($currency) {
|
||||
|
||||
// Format usage
|
||||
$usage = match ($estimate['feature']) {
|
||||
'bandwidth' => Metric::megabytes($estimate['usage'])->format(),
|
||||
'storage' => Metric::megabytes($estimate['usage'])->format(),
|
||||
};
|
||||
|
||||
// Normalize units
|
||||
$amount = $estimate['amount'] / 1000;
|
||||
|
||||
return [
|
||||
'feature' => $estimate['feature'],
|
||||
'amount' => $amount,
|
||||
'cost' => format_currency($amount, $currency),
|
||||
'usage' => $usage,
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Users\Resources;
|
||||
|
||||
use App\Users\Actions\FormatUsageEstimatesAction;
|
||||
use ByteUnits\Metric;
|
||||
use Domain\Folders\Resources\FolderCollection;
|
||||
use VueFileManager\Subscription\Domain\Usage\Actions\SumUsageForCurrentPeriodAction;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use VueFileManager\Subscription\Domain\Credits\Resources\BalanceResource;
|
||||
use VueFileManager\Subscription\Domain\Subscriptions\Resources\SubscriptionResource;
|
||||
@@ -61,44 +63,17 @@ class UserResource extends JsonResource
|
||||
|
||||
private function getUsageEstimates()
|
||||
{
|
||||
$estimates = $this
|
||||
->subscription
|
||||
->plan
|
||||
->meteredFeatures
|
||||
->map(function ($feature) {
|
||||
// Get plan currency
|
||||
$currency = $this->subscription->plan->currency;
|
||||
|
||||
// Get first tier
|
||||
$tier = $feature->tiers()->first();
|
||||
// Get usage
|
||||
$usage = resolve(SumUsageForCurrentPeriodAction::class)($this->subscription);
|
||||
|
||||
$usageQuery = $this->subscription
|
||||
->usages()
|
||||
->where('created_at', '>=', now()->subDays(30))
|
||||
->where('subscription_id', $this->subscription->id)
|
||||
->where('metered_feature_id', $feature->id);
|
||||
|
||||
$usage = match ($feature->aggregate_strategy) {
|
||||
'sum_of_usage' => $usageQuery->sum('quantity'),
|
||||
'maximum_usage' => $usageQuery->max('quantity'),
|
||||
};
|
||||
|
||||
$amount = ($tier->per_unit / 1000) * $usage;
|
||||
|
||||
$formattedUsage = match ($feature->key) {
|
||||
'bandwidth' => Metric::megabytes($usage)->format(),
|
||||
'storage' => Metric::megabytes($usage)->format(),
|
||||
};
|
||||
|
||||
// return sum of money
|
||||
return [
|
||||
'feature' => $feature->key,
|
||||
'amount' => $amount,
|
||||
'cost' => format_currency($amount, $this->subscription->plan->currency),
|
||||
'usage' => $formattedUsage,
|
||||
];
|
||||
});
|
||||
// Format usages
|
||||
$estimates = resolve(FormatUsageEstimatesAction::class)($currency, $usage);
|
||||
|
||||
return [
|
||||
'costEstimate' => format_currency($estimates->sum('amount'), $this->subscription->plan->currency),
|
||||
'costEstimate' => format_currency($estimates->sum('amount'), $currency),
|
||||
'featureEstimates' => $estimates->toArray(),
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user