From f5a5881454f72af72af217def416f87ea04a9d1f Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 29 Nov 2017 13:12:20 +0200 Subject: [PATCH] Working on subscriptions UI --- .../Controllers/IntegrationController.php | 11 ++------ app/Listeners/SubscriptionListener.php | 26 +++++++++++-------- app/Models/Account.php | 4 +-- app/Models/Subscription.php | 7 +++++ ..._11_15_114422_add_subdomain_to_lookups.php | 6 ++++- docs/api.rst | 5 ---- resources/views/accounts/api_tokens.blade.php | 3 +-- 7 files changed, 32 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/IntegrationController.php b/app/Http/Controllers/IntegrationController.php index 1437a215e8..07728dd54e 100644 --- a/app/Http/Controllers/IntegrationController.php +++ b/app/Http/Controllers/IntegrationController.php @@ -24,15 +24,8 @@ class IntegrationController extends Controller return Response::json('Event is invalid', 500); } - $subscription = Subscription::where('account_id', '=', Auth::user()->account_id) - ->where('event_id', '=', $eventId)->first(); - - if (! $subscription) { - $subscription = new Subscription(); - $subscription->account_id = Auth::user()->account_id; - $subscription->event_id = $eventId; - } - + $subscription = Subscription::createNew(); + $subscription->event_id = $eventId; $subscription->target_url = trim(Input::get('target_url')); $subscription->save(); diff --git a/app/Listeners/SubscriptionListener.php b/app/Listeners/SubscriptionListener.php index 15fd543d54..6f6298c3e6 100644 --- a/app/Listeners/SubscriptionListener.php +++ b/app/Listeners/SubscriptionListener.php @@ -132,21 +132,25 @@ class SubscriptionListener return; } - $subscription = $entity->account->getSubscription($eventId); + $subscriptions = $entity->account->getSubscriptions($eventId); - if ($subscription) { - $manager = new Manager(); - $manager->setSerializer(new ArraySerializer()); - $manager->parseIncludes($include); + if (! $subscriptions->count()) { + return; + } - $resource = new Item($entity, $transformer, $entity->getEntityType()); - $data = $manager->createData($resource)->toArray(); + $manager = new Manager(); + $manager->setSerializer(new ArraySerializer()); + $manager->parseIncludes($include); - // For legacy Zapier support - if (isset($data['client_id'])) { - $data['client_name'] = $entity->client->getDisplayName(); - } + $resource = new Item($entity, $transformer, $entity->getEntityType()); + $data = $manager->createData($resource)->toArray(); + // For legacy Zapier support + if (isset($data['client_id'])) { + $data['client_name'] = $entity->client->getDisplayName(); + } + + foreach ($subscriptions as $subscription) { Utils::notifyZapier($subscription, $data); } } diff --git a/app/Models/Account.php b/app/Models/Account.php index 118dedc228..333e7b5e73 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -1285,9 +1285,9 @@ class Account extends Eloquent * * @return \Illuminate\Database\Eloquent\Model|null|static */ - public function getSubscription($eventId) + public function getSubscriptions($eventId) { - return Subscription::where('account_id', '=', $this->id)->where('event_id', '=', $eventId)->first(); + return Subscription::where('account_id', '=', $this->id)->where('event_id', '=', $eventId)->get(); } /** diff --git a/app/Models/Subscription.php b/app/Models/Subscription.php index a01a30e7f4..ad29cd07aa 100644 --- a/app/Models/Subscription.php +++ b/app/Models/Subscription.php @@ -38,5 +38,12 @@ class Subscription extends EntityModel return ENTITY_SUBSCRIPTION; } + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function account() + { + return $this->belongsTo('App\Models\Account'); + } } diff --git a/database/migrations/2017_11_15_114422_add_subdomain_to_lookups.php b/database/migrations/2017_11_15_114422_add_subdomain_to_lookups.php index 818de16b09..0872cef21c 100644 --- a/database/migrations/2017_11_15_114422_add_subdomain_to_lookups.php +++ b/database/migrations/2017_11_15_114422_add_subdomain_to_lookups.php @@ -71,7 +71,10 @@ class AddSubdomainToLookups extends Migration }); $accountPublicIds = []; - foreach (Subscription::withTrashed()->orderBy('id')->get() as $subscription) { + foreach (Subscription::withTrashed() + ->with('account.users') + ->orderBy('id') + ->get() as $subscription) { $accountId = $subscription->account_id; if (isset($accountPublicIds[$accountId])) { $publicId = $accountPublicIds[$accountId]; @@ -81,6 +84,7 @@ class AddSubdomainToLookups extends Migration $accountPublicIds[$accountId] = 2; } $subscription->public_id = $publicId; + $subscription->user_id = $subscription->account->users[0]->id; $subscription->save(); } diff --git a/docs/api.rst b/docs/api.rst index b67bb60661..0b2e0f7791 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -126,8 +126,3 @@ To email an invoice use the email_invoice command passing the id of the invoice. curl -X POST ninja.dev/api/v1/email_invoice -d '{"id":1}' \ -H "Content-Type:application/json" -H "X-Ninja-Token: TOKEN" - -Subscriptions -""""""""""""" - -You can use subscriptions to have Invoice Ninja POST newly created records to a third-party application. To enable this feature you need to manually add a record to the subscriptions table. To determine the event_id find the associated EVENT_CREATE_ value from app/Constants.php. diff --git a/resources/views/accounts/api_tokens.blade.php b/resources/views/accounts/api_tokens.blade.php index 5352446ae4..1f63623071 100644 --- a/resources/views/accounts/api_tokens.blade.php +++ b/resources/views/accounts/api_tokens.blade.php @@ -29,8 +29,7 @@ ->setOptions('aoColumnDefs', [['bSortable'=>false, 'aTargets'=>[2]]]) ->render('datatable') !!} -

 

-

 

+

 
 

@if (Utils::hasFeature(FEATURE_API))