Skip to content

Commit

Permalink
feat: add reminders/upcoming API (#5783)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhymxu authored Jan 6, 2022
1 parent 30d97f9 commit a3e9b79
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/Http/Controllers/Api/ApiReminderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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);
}
}
41 changes: 41 additions & 0 deletions app/Http/Resources/Reminder/ReminderOutbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Resources\Reminder;

use App\Helpers\DateHelper;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\Contact\ContactShort as ContactShortResource;

/**
* @extends JsonResource<\App\Models\Contact\ReminderOutbox>
*/
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),
];
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
39 changes: 39 additions & 0 deletions tests/Api/ApiReminderControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}

0 comments on commit a3e9b79

Please sign in to comment.