From a3e9b79236b2e388a4823f0ffb061551de05e406 Mon Sep 17 00:00:00 2001 From: Dung Nguyen Date: Thu, 6 Jan 2022 13:34:43 +0700 Subject: [PATCH] feat: add reminders/upcoming API (#5783) --- .../Controllers/Api/ApiReminderController.php | 26 ++++++++++++ .../Resources/Reminder/ReminderOutbox.php | 41 +++++++++++++++++++ routes/api.php | 1 + tests/Api/ApiReminderControllerTest.php | 39 ++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 app/Http/Resources/Reminder/ReminderOutbox.php diff --git a/app/Http/Controllers/Api/ApiReminderController.php b/app/Http/Controllers/Api/ApiReminderController.php index 9451431c48d..fbbeb27105c 100644 --- a/app/Http/Controllers/Api/ApiReminderController.php +++ b/app/Http/Controllers/Api/ApiReminderController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api; use Illuminate\Http\Request; +use App\Helpers\AccountHelper; use App\Models\Contact\Contact; use App\Models\Contact\Reminder; use Illuminate\Database\QueryException; @@ -12,6 +13,7 @@ use App\Services\Contact\Reminder\DestroyReminder; use Illuminate\Database\Eloquent\ModelNotFoundException; use App\Http\Resources\Reminder\Reminder as ReminderResource; +use App\Http\Resources\Reminder\ReminderOutbox as ReminderOutboxResource; class ApiReminderController extends ApiController { @@ -153,4 +155,28 @@ public function reminders(Request $request, $contactId) return ReminderResource::collection($reminders); } + + /** + * Get the reminders for the month given in parameter. + * - 0 means current month + * - 1 means month+1 + * - 2 means month+2... + * + * @param Request $request + * @param int $month + * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection|\Illuminate\Http\JsonResponse + */ + public function upcoming(Request $request, int $month = 0) + { + try { + $reminders = AccountHelper::getUpcomingRemindersForMonth( + auth()->user()->account, + $month + ); + } catch (QueryException $e) { + return $this->respondInvalidQuery(); + } + + return ReminderOutboxResource::collection($reminders); + } } diff --git a/app/Http/Resources/Reminder/ReminderOutbox.php b/app/Http/Resources/Reminder/ReminderOutbox.php new file mode 100644 index 00000000000..a5f7f6632d1 --- /dev/null +++ b/app/Http/Resources/Reminder/ReminderOutbox.php @@ -0,0 +1,41 @@ + + */ +class ReminderOutbox extends JsonResource +{ + /** + * Transform the resource into an array. + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function toArray($request) + { + return [ + 'id' => $this->id, + 'reminder_id' => $this->reminder_id, + 'object' => $this->nature, + 'planned_date' => $this->planned_date, + 'title' => $this->reminder->title, + 'description' => $this->reminder->description, + 'frequency_type' => $this->reminder->frequency_type, + 'frequency_number' => $this->reminder->frequency_number, + 'initial_date' => DateHelper::getTimestamp($this->reminder->initial_date), + 'delible' => (bool) $this->reminder->delible, + 'account' => [ + 'id' => $this->account_id, + ], + 'contact' => new ContactShortResource($this->reminder->contact), + 'created_at' => DateHelper::getTimestamp($this->created_at), + 'updated_at' => DateHelper::getTimestamp($this->updated_at), + ]; + } +} diff --git a/routes/api.php b/routes/api.php index f494077fd9a..8722b95095d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -90,6 +90,7 @@ Route::get('/contacts/{contact}/activities', 'ApiActivitiesController@activities'); // Reminders + Route::get('reminders/upcoming/{month}', 'ApiReminderController@upcoming'); Route::apiResource('reminders', 'ApiReminderController') ->names(['index' => 'reminders']); Route::get('/contacts/{contact}/reminders', 'ApiReminderController@reminders'); diff --git a/tests/Api/ApiReminderControllerTest.php b/tests/Api/ApiReminderControllerTest.php index 959b6ea535b..d87253e2218 100644 --- a/tests/Api/ApiReminderControllerTest.php +++ b/tests/Api/ApiReminderControllerTest.php @@ -367,4 +367,43 @@ public function reminders_delete_error() 'The selected reminder id is invalid.', ]); } + + /** @test */ + public function it_gets_all_upcoming_reminders() + { + $user = $this->signin(); + + Carbon::setTestNow(Carbon::create(2017, 1, 1)); + + // add 2 reminders for the month of March + $reminder1 = factory(Reminder::class)->create([ + 'account_id' => $user->account_id, + 'initial_date' => '2017-03-03 00:00:00', + ]); + $reminder1->schedule($user); + + $reminder2 = factory(Reminder::class)->create([ + 'account_id' => $user->account_id, + 'initial_date' => '2017-03-03 00:00:00', + 'delible' => false, + ]); + $reminder2->schedule($user); + + $response = $this->json('GET', '/api/reminders/upcoming/2'); + + $response->assertStatus(200); + $response->assertJsonStructure([ + 'data' => ['*' => $this->jsonReminder], + ]); + $response->assertJsonFragment([ + 'object' => 'reminder', + 'reminder_id' => $reminder1->id, + 'delible' => true, + ]); + $response->assertJsonFragment([ + 'object' => 'reminder', + 'reminder_id' => $reminder2->id, + 'delible' => false, + ]); + } }