From 1e83b729d56c4967ee26f7f51d13c199efa538a6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 1 Apr 2021 20:33:50 +1100 Subject: [PATCH 1/2] Minor fixes for subscriptions --- .../Subscription/StoreSubscriptionRequest.php | 6 ---- app/Models/Subscription.php | 1 + app/Transformers/SubscriptionTransformer.php | 1 + app/Utils/HtmlEngine.php | 1 + app/Utils/SystemHealth.php | 1 + ...8_modify_column_on_subscriptions_table.php | 30 +++++++++++++++++++ 6 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 database/migrations/2021_04_01_093128_modify_column_on_subscriptions_table.php diff --git a/app/Http/Requests/Subscription/StoreSubscriptionRequest.php b/app/Http/Requests/Subscription/StoreSubscriptionRequest.php index 8f8df53f400c..496f2747d740 100644 --- a/app/Http/Requests/Subscription/StoreSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/StoreSubscriptionRequest.php @@ -62,12 +62,6 @@ class StoreSubscriptionRequest extends Request { $input = $this->all(); - // if(array_key_exists('webhook_configuration', $input) && (!is_object(json_decode($input['webhook_configuration'])))) - // $input['webhook_configuration'] = new \stdClass; - - // if(!array_key_exists('webhook_configuration', $input)) - // $input['webhook_configuration'] = new \stdClass; - $this->replace($input); } } diff --git a/app/Models/Subscription.php b/app/Models/Subscription.php index d0778797c9e9..1c888095cb99 100644 --- a/app/Models/Subscription.php +++ b/app/Models/Subscription.php @@ -21,6 +21,7 @@ class Subscription extends BaseModel use HasFactory, SoftDeletes; protected $fillable = [ + 'assigned_user_id', 'product_ids', 'recurring_product_ids', 'frequency_id', diff --git a/app/Transformers/SubscriptionTransformer.php b/app/Transformers/SubscriptionTransformer.php index 47b337bf718d..ea496f3291b6 100644 --- a/app/Transformers/SubscriptionTransformer.php +++ b/app/Transformers/SubscriptionTransformer.php @@ -40,6 +40,7 @@ class SubscriptionTransformer extends EntityTransformer 'user_id' => $this->encodePrimaryKey($subscription->user_id), 'group_id' => $this->encodePrimaryKey($subscription->group_id), 'product_ids' => (string)$subscription->product_ids, + 'name' => (string)$subscription->name, 'recurring_product_ids' => (string)$subscription->recurring_product_ids, 'assigned_user_id' => $this->encodePrimaryKey($subscription->assigned_user_id), 'company_id' => $this->encodePrimaryKey($subscription->company_id), diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 45512439cd73..23a37b03cc8a 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -192,6 +192,7 @@ class HtmlEngine $data['$taxes'] = ['value' => Number::formatMoney($this->entity_calc->getItemTotalTaxes(), $this->client) ?: ' ', 'label' => ctrans('texts.taxes')]; $data['$invoice.taxes'] = &$data['$taxes']; + $data['$user.name'] = ['value' => $this->entity->user->present()->name(), 'label' => ctrans('texts.name')]; $data['$user_iban'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'company1', $this->settings->custom_value1, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'company1')]; $data['$invoice.custom1'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice1', $this->entity->custom_value1, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice1')]; $data['$invoice.custom2'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice2', $this->entity->custom_value2, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice2')]; diff --git a/app/Utils/SystemHealth.php b/app/Utils/SystemHealth.php index 3ffee1fd4638..b130585ac28d 100644 --- a/app/Utils/SystemHealth.php +++ b/app/Utils/SystemHealth.php @@ -79,6 +79,7 @@ class SystemHealth 'exec' => (bool)self::checkExecWorks(), 'open_basedir' => (bool)self::checkOpenBaseDir(), 'mail_mailer' => (string)self::checkMailMailer(), + 'flutter_renderer' => (string)config('ninja.flutter_canvas_kit'), ]; } diff --git a/database/migrations/2021_04_01_093128_modify_column_on_subscriptions_table.php b/database/migrations/2021_04_01_093128_modify_column_on_subscriptions_table.php new file mode 100644 index 000000000000..430c5131ba49 --- /dev/null +++ b/database/migrations/2021_04_01_093128_modify_column_on_subscriptions_table.php @@ -0,0 +1,30 @@ +unsignedInteger('assigned_user_id')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} From a2e0fd08490f2b90f997a18990cc88111899c79b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 1 Apr 2021 20:56:50 +1100 Subject: [PATCH 2/2] Unique subscription nameS --- .../Requests/Subscription/StoreSubscriptionRequest.php | 2 +- app/Transformers/SubscriptionTransformer.php | 3 +-- tests/Feature/SubscriptionApiTest.php | 9 ++++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/Http/Requests/Subscription/StoreSubscriptionRequest.php b/app/Http/Requests/Subscription/StoreSubscriptionRequest.php index 496f2747d740..2ce2d635d519 100644 --- a/app/Http/Requests/Subscription/StoreSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/StoreSubscriptionRequest.php @@ -54,7 +54,7 @@ class StoreSubscriptionRequest extends Request 'plan_map' => ['sometimes'], 'refund_period' => ['sometimes'], 'webhook_configuration' => ['array'], - 'name' => Rule::unique('subscriptions')->where('company_id', auth()->user()->company()->id) + 'name' => ['required', Rule::unique('subscriptions')->where('company_id', auth()->user()->company()->id)] ]; } diff --git a/app/Transformers/SubscriptionTransformer.php b/app/Transformers/SubscriptionTransformer.php index ea496f3291b6..559cea012b29 100644 --- a/app/Transformers/SubscriptionTransformer.php +++ b/app/Transformers/SubscriptionTransformer.php @@ -33,7 +33,6 @@ class SubscriptionTransformer extends EntityTransformer public function transform(Subscription $subscription): array { - $std = new \stdClass; return [ 'id' => $this->encodePrimaryKey($subscription->id), @@ -61,7 +60,7 @@ class SubscriptionTransformer extends EntityTransformer 'allow_plan_changes' => (bool)$subscription->allow_plan_changes, 'plan_map' => (string)$subscription->plan_map, 'refund_period' => (int)$subscription->refund_period, - 'webhook_configuration' => $subscription->webhook_configuration ?: $std, + 'webhook_configuration' => $subscription->webhook_configuration ?: [], 'purchase_page' => (string)route('client.subscription.purchase', $subscription->hashed_id), 'is_deleted' => (bool)$subscription->is_deleted, 'created_at' => (int)$subscription->created_at, diff --git a/tests/Feature/SubscriptionApiTest.php b/tests/Feature/SubscriptionApiTest.php index 8c4ec16f2f00..4c5148c410a6 100644 --- a/tests/Feature/SubscriptionApiTest.php +++ b/tests/Feature/SubscriptionApiTest.php @@ -11,14 +11,15 @@ namespace Tests\Feature; -use App\Models\Subscription; use App\Models\Product; +use App\Models\Subscription; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Support\Facades\Session; +use Illuminate\Support\Str; use Tests\MockAccountData; use Tests\TestCase; @@ -55,6 +56,7 @@ class SubscriptionApiTest extends TestCase $billing_subscription = Subscription::factory()->create([ 'product_ids' => $product->id, 'company_id' => $this->company->id, + 'name' => Str::random(5) ]); @@ -78,7 +80,7 @@ class SubscriptionApiTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/subscriptions', ['product_ids' => $product->id, 'allow_cancellation' => true]); + ])->post('/api/v1/subscriptions', ['product_ids' => $product->id, 'allow_cancellation' => true, 'name' => Str::random(5)]); // nlog($response); $response->assertStatus(200); @@ -93,7 +95,7 @@ class SubscriptionApiTest extends TestCase $response1 = $this ->withHeaders(['X-API-SECRET' => config('ninja.api_secret'),'X-API-TOKEN' => $this->token]) - ->post('/api/v1/subscriptions', ['product_ids' => $product->id]) + ->post('/api/v1/subscriptions', ['product_ids' => $product->id, 'name' => Str::random(5)]) ->assertStatus(200) ->json(); @@ -126,6 +128,7 @@ class SubscriptionApiTest extends TestCase $billing_subscription = Subscription::factory()->create([ 'product_ids' => $product->id, 'company_id' => $this->company->id, + 'name' => Str::random(5) ]); $response = $this