From c8cfe1524f940999e2ae9ecf45a74e83d95ef3ab Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Thu, 11 Oct 2018 16:57:35 -0400 Subject: [PATCH 1/5] wip --- .../Api/Contact/ApiConversationController.php | 27 +++++++++++++++++++ routes/api.php | 1 + 2 files changed, 28 insertions(+) diff --git a/app/Http/Controllers/Api/Contact/ApiConversationController.php b/app/Http/Controllers/Api/Contact/ApiConversationController.php index f66dd571923..5b4535f8479 100644 --- a/app/Http/Controllers/Api/Contact/ApiConversationController.php +++ b/app/Http/Controllers/Api/Contact/ApiConversationController.php @@ -33,6 +33,33 @@ public function index(Request $request) return ConversationResource::collection($conversations); } + /** + * Get the list of conversations for a specific contact. + * + * @return \Illuminate\Http\Response + */ + public function conversations(Request $request, $contactId) + { + try { + $contact = Contact::where('account_id', auth()->user()->account_id) + ->where('id', $contactId) + ->firstOrFail(); + } catch (ModelNotFoundException $e) { + return $this->respondNotFound(); + } + + try { + $conversations = auth()->user()->account->conversations() + ->where('contact_id', $contactId) + ->orderBy($this->sort, $this->sortDirection) + ->paginate($this->getLimitPerPage()); + } catch (QueryException $e) { + return $this->respondInvalidQuery(); + } + + return ConversationResource::collection($conversations); + } + /** * Get the detail of a given conversation. * diff --git a/routes/api.php b/routes/api.php index 0e0e9c25618..7601344b1c0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -80,6 +80,7 @@ Route::post('/conversations/{conversation}/messages', 'Api\\Contact\\ApiMessageController@store'); Route::put('/conversations/{conversation}/messages/{message}', 'Api\\Contact\\ApiMessageController@update'); Route::delete('/conversations/{conversation}/messages/{message}', 'Api\\Contact\\ApiMessageController@destroy'); + Route::get('/contacts/{contact}/conversations', 'Api\\ApiConversationController@conversations'); // Activities Route::resource('activities', 'Api\\ApiActivityController', ['except' => [ From 0ad740d3d75598abb1d6e23a61d0152a8a809c77 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Thu, 11 Oct 2018 18:12:50 -0400 Subject: [PATCH 2/5] add tests --- .../Api/Contact/ApiConversationController.php | 1 + routes/api.php | 2 +- .../Contact/ApiConversationControllerTest.php | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/Contact/ApiConversationController.php b/app/Http/Controllers/Api/Contact/ApiConversationController.php index 5b4535f8479..d107c5193bb 100644 --- a/app/Http/Controllers/Api/Contact/ApiConversationController.php +++ b/app/Http/Controllers/Api/Contact/ApiConversationController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api\Contact; use Illuminate\Http\Request; +use App\Models\Contact\Contact; use App\Models\Contact\Conversation; use Illuminate\Database\QueryException; use App\Http\Controllers\Api\ApiController; diff --git a/routes/api.php b/routes/api.php index 7601344b1c0..fb8be7fe2b8 100644 --- a/routes/api.php +++ b/routes/api.php @@ -80,7 +80,7 @@ Route::post('/conversations/{conversation}/messages', 'Api\\Contact\\ApiMessageController@store'); Route::put('/conversations/{conversation}/messages/{message}', 'Api\\Contact\\ApiMessageController@update'); Route::delete('/conversations/{conversation}/messages/{message}', 'Api\\Contact\\ApiMessageController@destroy'); - Route::get('/contacts/{contact}/conversations', 'Api\\ApiConversationController@conversations'); + Route::get('/contacts/{contact}/conversations', 'Api\\Contact\\ApiConversationController@conversations'); // Activities Route::resource('activities', 'Api\\ApiActivityController', ['except' => [ diff --git a/tests/Api/Contact/ApiConversationControllerTest.php b/tests/Api/Contact/ApiConversationControllerTest.php index ca09299c8ee..90e728443de 100644 --- a/tests/Api/Contact/ApiConversationControllerTest.php +++ b/tests/Api/Contact/ApiConversationControllerTest.php @@ -121,6 +121,23 @@ public function test_it_gets_a_conversation() ]); } + public function test_it_gets_a_conversation_for_a_specific_contact() + { + $user = $this->signin(); + + $conversation = $this->createConversation($user); + + $response = $this->json('GET', '/api/contacts/' . $conversation['contact_id'] . '/conversations'); + + $response->assertStatus(200); + + $response->assertJsonStructure([ + 'data' => [ + '*' => $this->jsonConversations, + ], + ]); + } + public function test_it_creates_a_conversation() { $user = $this->signin(); From 98fd73dda7ee2f387c2b8ee0c2cf8cc1b9a689b2 Mon Sep 17 00:00:00 2001 From: djaiss Date: Thu, 11 Oct 2018 22:13:02 +0000 Subject: [PATCH 3/5] Apply fixes from StyleCI --- tests/Api/Contact/ApiConversationControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Api/Contact/ApiConversationControllerTest.php b/tests/Api/Contact/ApiConversationControllerTest.php index 90e728443de..68c18ef268a 100644 --- a/tests/Api/Contact/ApiConversationControllerTest.php +++ b/tests/Api/Contact/ApiConversationControllerTest.php @@ -127,7 +127,7 @@ public function test_it_gets_a_conversation_for_a_specific_contact() $conversation = $this->createConversation($user); - $response = $this->json('GET', '/api/contacts/' . $conversation['contact_id'] . '/conversations'); + $response = $this->json('GET', '/api/contacts/'.$conversation['contact_id'].'/conversations'); $response->assertStatus(200); From 58215a391da4802c24927ce0484a4ce30961411b Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Thu, 11 Oct 2018 18:20:12 -0400 Subject: [PATCH 4/5] fix palsm --- app/Http/Controllers/Api/Contact/ApiConversationController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/Contact/ApiConversationController.php b/app/Http/Controllers/Api/Contact/ApiConversationController.php index d107c5193bb..e3aa854fcc2 100644 --- a/app/Http/Controllers/Api/Contact/ApiConversationController.php +++ b/app/Http/Controllers/Api/Contact/ApiConversationController.php @@ -42,7 +42,7 @@ public function index(Request $request) public function conversations(Request $request, $contactId) { try { - $contact = Contact::where('account_id', auth()->user()->account_id) + Contact::where('account_id', auth()->user()->account_id) ->where('id', $contactId) ->firstOrFail(); } catch (ModelNotFoundException $e) { From 8c332f14a96a221b52c87e6f93f3777992a81714 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Thu, 11 Oct 2018 18:28:35 -0400 Subject: [PATCH 5/5] update changelog --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index e1f0e421037..ff09036d8f0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ UNRELEASED CHANGES: +* Add ability to retrieve all conversations for one contact through the API * Fix gravatar not displayed on dashboard view RELEASED VERSIONS: