Skip to content

Commit

Permalink
feat: add life events feature (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Sep 25, 2018
1 parent 1908b96 commit e3e9fcb
Show file tree
Hide file tree
Showing 116 changed files with 4,212 additions and 98 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
UNRELEASED CHANGES:

* Add ability to track life events
* Fix select boxes not working properly anymore
* Improve search
* Fix reminders displaying wrong date
* Add ability to define the default email address used for support
Expand Down
14 changes: 9 additions & 5 deletions app/Helpers/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Jenssegers\Date\Date;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;

class DateHelper
Expand Down Expand Up @@ -254,16 +255,19 @@ public static function getNextTheoriticalBillingDate(String $interval)
* @param int min
* @param int max
*
* @return array
* @return Collection
*/
public static function getListOfYears($max = 120, $min = 0)
{
$years = [];
$years = collect([]);
$maxYear = now(static::getTimezone())->subYears($min)->year;
$minYear = now(static::getTimezone())->subYears($max)->year;

for ($year = $maxYear; $year >= $minYear; $year--) {
array_push($years, $year);
$years->push([
'id' => $year,
'name' => $year,
]);
}

return $years;
Expand All @@ -272,7 +276,7 @@ public static function getListOfYears($max = 120, $min = 0)
/**
* Gets a list of all the months in a year.
*
* @return array
* @return Collection
*/
public static function getListOfMonths()
{
Expand All @@ -294,7 +298,7 @@ public static function getListOfMonths()
/**
* Gets a list of all the days in a month.
*
* @return array
* @return Collection
*/
public static function getListOfDays()
{
Expand Down
125 changes: 125 additions & 0 deletions app/Http/Controllers/Api/Contact/ApiLifeEventController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace App\Http\Controllers\Api\Contact;

use Illuminate\Http\Request;
use App\Models\Contact\LifeEvent;
use App\Http\Controllers\Api\ApiController;
use App\Exceptions\MissingParameterException;
use App\Services\Contact\LifeEvent\CreateLifeEvent;
use App\Services\Contact\LifeEvent\UpdateLifeEvent;
use App\Services\Contact\LifeEvent\DestroyLifeEvent;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Resources\LifeEvent\LifeEvent as LifeEventResource;

class ApiLifeEventController extends ApiController
{
/**
* Get the list of life events.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$lifeEvents = auth()->user()->account->lifeEvents()
->orderBy($this->sort, $this->sortDirection)
->paginate($this->getLimitPerPage());

return LifeEventResource::collection($lifeEvents);
}

/**
* Get the detail of a given life event.
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function show(Request $request, int $lifeEventId)
{
try {
$lifeEvent = LifeEvent::where('account_id', auth()->user()->account_id)
->findOrFail($lifeEventId);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

return new LifeEventResource($lifeEvent);
}

/**
* Store the life event.
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
try {
$lifeEvent = (new CreateLifeEvent)->execute(
$request->all()
+
[
'account_id' => auth()->user()->account->id,
]
);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->setHTTPStatusCode(500)
->setErrorCode(41)
->respondWithError(config('api.error_codes.41'));
}

return new LifeEventResource($lifeEvent);
}

/**
* Update the life event.
*
* @param Request $request
* @param int $lifeEventId
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $lifeEventId)
{
try {
$lifeEvent = (new UpdateLifeEvent)->execute(
$request->all()
+
[
'account_id' => auth()->user()->account->id,
'life_event_id' => $lifeEventId,
]
);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->setHTTPStatusCode(500)
->setErrorCode(41)
->respondWithError(config('api.error_codes.41'));
}

return new LifeEventResource($lifeEvent);
}

/**
* Destroy the life event.
*
* @param Request $request
* @param int $lifeEventId
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $lifeEventId)
{
try {
(new DestroyLifeEvent)->execute([
'account_id' => auth()->user()->account->id,
'life_event_id' => $lifeEventId,
]);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

return $this->respondObjectDeleted((int) $lifeEventId);
}
}
125 changes: 125 additions & 0 deletions app/Http/Controllers/Contacts/LifeEventsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace App\Http\Controllers\Contacts;

use App\Helpers\DateHelper;
use Illuminate\Http\Request;
use App\Models\Contact\Contact;
use Illuminate\Support\Collection;
use App\Http\Controllers\Controller;
use App\Models\Contact\LifeEventCategory;
use App\Services\Contact\LifeEvent\CreateLifeEvent;
use App\Services\Contact\LifeEvent\DestroyLifeEvent;
use App\Http\Resources\LifeEvent\LifeEventType as LifeEventTypeResource;
use App\Http\Resources\LifeEvent\LifeEventCategory as LifeEventCategoryResource;

class LifeEventsController extends Controller
{
/**
* Get the list of life event categories.
*
* @param Request $request
* @return Collection
*/
public function categories(Request $request)
{
$lifeEventCategories = auth()->user()->account->lifeEventCategories;

return LifeEventCategoryResource::collection($lifeEventCategories);
}

/**
* Get the list of life event types for a given life event category.
* @param Request $request
* @param int $LifeEventCategoryId
* @return Collection
*/
public function types(Request $request, int $lifeEventCategoryId)
{
$lifeEventCategory = LifeEventCategory::findOrFail($lifeEventCategoryId);
$lifeEventTypes = $lifeEventCategory->lifeEventTypes;

return LifeEventTypeResource::collection($lifeEventTypes);
}

/**
* Display the list of life events.
*
* @param Contact $contact
* @return \Illuminate\Http\Response
*/
public function index(Request $request, Contact $contact)
{
$lifeEventsCollection = collect([]);
$lifeEvents = $contact->lifeEvents()->get();

foreach ($lifeEvents as $lifeEvent) {
$data = [
'id' => $lifeEvent->id,
'life_event_type' => $lifeEvent->lifeEventType->name,
'default_life_event_type_key' => $lifeEvent->lifeEventType->default_life_event_type_key,
'name' => $lifeEvent->name,
'note' => $lifeEvent->note,
'happened_at' => DateHelper::getShortDate($lifeEvent->happened_at),
];
$lifeEventsCollection->push($data);
}

return $lifeEventsCollection;
}

/**
* Store the life event.
*
* @param Request $request
* @param Contact $contact
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Contact $contact)
{
$data = [
'account_id' => auth()->user()->account->id,
'contact_id' => $contact->id,
'life_event_type_id' => $request->get('life_event_type_id'),
'happened_at' => $request->get('happened_at'),
'name' => $request->get('name'),
'note' => $request->get('note'),
];

// create the conversation
try {
$lifeEvent = (new CreateLifeEvent)->execute($data);
} catch (\Exception $e) {
return back()
->withInput()
->withErrors(trans('app.error_save'));
}

return $lifeEvent;
}

/**
* Destroy the life event.
* @param Request $request
* @param Contact $contat
* @param LifeEvent $lifeEvent
* @return bool
*/
public function destroy(Request $request, Contact $contat, $lifeEventId)
{
$data = [
'account_id' => auth()->user()->account->id,
'life_event_id' => $lifeEventId,
];

try {
(new DestroyLifeEvent)->execute($data);
} catch (\Exception $e) {
return back()
->withInput()
->withErrors(trans('app.error_save'));
}

return 'done';
}
}
18 changes: 17 additions & 1 deletion app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,30 @@ public function show(Contact $contact)
// list of active features
$modules = $contact->account->modules()->active()->get();

// add `---` at the top of the dropdowns
$days = DateHelper::getListOfDays();
$days->prepend([
'id' => 0,
'name' => '---',
]);

$months = DateHelper::getListOfMonths();
$months->prepend([
'id' => 0,
'name' => '---',
]);

return view('people.profile')
->withLoveRelationships($loveRelationships)
->withFamilyRelationships($familyRelationships)
->withFriendRelationships($friendRelationships)
->withWorkRelationships($workRelationships)
->withReminders($reminders)
->withModules($modules)
->withContact($contact);
->withContact($contact)
->withDays($days)
->withMonths($months)
->withYears(DateHelper::getListOfYears());
}

/**
Expand Down
Loading

0 comments on commit e3e9fcb

Please sign in to comment.