Skip to content

Commit

Permalink
chore: refactor contact management with services (#2168)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Dec 21, 2018
1 parent 731a6f0 commit d242e0f
Show file tree
Hide file tree
Showing 23 changed files with 1,590 additions and 329 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ UNRELEASED CHANGES:
* Fix avatar display in searches
* Fix conversation add/update using contact add/update flash messages
* Fix incompatibility of people search queries with PostgreSQL
* Refactor how contacts are managed
* Add the notion of places

RELEASED VERSIONS:

Expand Down
18 changes: 18 additions & 0 deletions app/Helpers/RandomHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Str;

class RandomHelper
{
/**
* Generate a UUID.
*
* @return string
*/
public static function uuid()
{
return Str::uuid()->toString();
}
}
174 changes: 56 additions & 118 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
use App\Models\Contact\Contact;
use App\Services\VCard\ExportVCard;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use App\Models\Relationship\Relationship;
use Barryvdh\Debugbar\Facade as Debugbar;
use Illuminate\Support\Facades\Validator;
use App\Services\Contact\Contact\CreateContact;
use App\Services\Contact\Contact\UpdateContact;
use App\Services\Contact\Contact\DestroyContact;
use App\Http\Resources\Contact\ContactShort as ContactResource;

class ContactsController extends Controller
Expand Down Expand Up @@ -138,7 +139,7 @@ private function contacts(Request $request, bool $active)
}

/**
* Show the form for creating a new resource.
* Show the form to add a new contact.
*
* @return \Illuminate\Http\Response
*/
Expand All @@ -163,37 +164,21 @@ public function missing()
}

/**
* Store a newly created resource in storage.
* Store the contact.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'first_name' => 'required|max:50',
'last_name' => 'nullable|max:100',
'nickname' => 'nullable|max:100',
'gender' => 'required|integer',
$contact = (new CreateContact)->execute([
'account_id' => auth()->user()->account->id,
'first_name' => $request->get('first_name'),
'last_name' => $request->input('last_name', null),
'nickname' => $request->input('nickname', null),
'gender_id' => $request->get('gender'),
]);

if ($validator->fails()) {
return back()
->withInput()
->withErrors($validator);
}

$contact = new Contact;
$contact->account_id = $request->user()->account_id;
$contact->gender_id = $request->input('gender');

$contact->first_name = $request->input('first_name');
$contact->last_name = $request->input('last_name', null);
$contact->nickname = $request->input('nickname', null);

$contact->setAvatarColor();
$contact->save();

// Did the user press "Save" or "Submit and add another person"
if (! is_null($request->get('save'))) {
return redirect()->route('people.show', $contact);
Expand All @@ -204,7 +189,7 @@ public function store(Request $request)
}

/**
* Display the specified resource.
* Display the contact profile.
*
* @param Contact $contact
* @return \Illuminate\Http\Response
Expand Down Expand Up @@ -306,40 +291,53 @@ public function edit(Contact $contact)
}

/**
* Update the identity and address of the People object.
* Update the contact.
*
* @param Request $request
* @param Contact $contact
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Contact $contact)
{
$validator = Validator::make($request->all(), [
'firstname' => 'required|max:50',
'lastname' => 'max:100',
'nickname' => 'max:100',
'description' => 'max:240',
'gender' => 'required',
'file' => 'max:10240',
'birthdate' => 'required|string',
'birthdayDate' => 'date_format:Y-m-d',
]);

if ($validator->fails()) {
return back()
->withInput()
->withErrors($validator);
// process birthday dates
// TODO: remove this part entirely when we redo this whole SpecialDate
// thing
if ($request->get('birthdate') == 'exact') {
$birthdate = $request->input('birthdayDate');
$birthdate = DateHelper::parseDate($birthdate);
$day = $birthdate->day;
$month = $birthdate->month;
$year = $birthdate->year;
} else {
$day = $request->get('day');
$month = $request->get('month');
$year = $request->get('year');
}

if (! $contact->setName($request->input('firstname'), $request->input('lastname'))) {
return back()
->withInput()
->withErrors('There has been a problem with saving the name.');
}
$data = [
'account_id' => auth()->user()->account->id,
'contact_id' => $contact->id,
'first_name' => $request->get('firstname'),
'last_name' => $request->input('lastname', null),
'nickname' => $request->input('nickname', null),
'gender_id' => $request->get('gender'),
'description' => $request->input('description', null),
'is_birthdate_known' => ($request->get('birthdate') == 'unknown' ? false : true),
'birthdate_day' => $day,
'birthdate_month' => $month,
'birthdate_year' => $year,
'birthdate_is_age_based' => ($request->get('birthdate') == 'approximate' ? true : false),
'birthdate_age' => $request->get('age'),
'birthdate_add_reminder' => ($request->get('addReminder') != '' ? true : false),
'is_deceased' => ($request->get('is_deceased') != '' ? true : false),
'is_deceased_date_known' => ($request->get('is_deceased_date_known') != '' ? true : false),
'deceased_date_day' => $request->get('deceased_date_day'),
'deceased_date_month' => $request->get('deceased_date_month'),
'deceased_date_year' => $request->get('deceased_date_year'),
'deceased_date_add_reminder' => ($request->get('add_reminder_deceased') != '' ? true : false),
];

$contact->gender_id = $request->input('gender');
$contact->description = $request->input('description');
$contact->nickname = $request->input('nickname', null);
$contact = (new UpdateContact)->execute($data);

if ($request->file('avatar') != '') {
if ($contact->has_avatar) {
Expand All @@ -351,78 +349,20 @@ public function update(Request $request, Contact $contact)
->withErrors(trans('app.error_save'));
}
}

$contact->has_avatar = true;
$contact->avatar_location = config('filesystems.default');
$contact->avatar_file_name = $request->avatar->storePublicly('avatars', $contact->avatar_location);
}

// Is the person deceased?
$contact->removeSpecialDate('deceased_date');
$contact->is_dead = false;

if ($request->input('markPersonDeceased') != '') {
$contact->is_dead = true;

if ($request->input('checkboxDatePersonDeceased') != '') {
$specialDate = $contact->setSpecialDate('deceased_date', $request->input('deceased_date_year'), $request->input('deceased_date_month'), $request->input('deceased_date_day'));

if ($request->input('addReminderDeceased') != '') {
$specialDate->setReminder('year', 1, trans('people.deceased_reminder_title', ['name' => $contact->first_name]));
}
}
}

$contact->save();

// Handling the case of the birthday
$contact->removeSpecialDate('birthdate');
switch ($request->input('birthdate')) {
case 'unknown':
break;
case 'approximate':
$specialDate = $contact->setSpecialDateFromAge('birthdate', $request->input('age'));
break;
case 'almost':
$specialDate = $contact->setSpecialDate(
'birthdate',
0,
$request->input('month'),
$request->input('day')
);

if ($request->input('addReminder') != '') {
$specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $contact->first_name]));
}

break;
case 'exact':
$birthdate = $request->input('birthdayDate');
$birthdate = DateHelper::parseDate($birthdate);
$specialDate = $contact->setSpecialDate(
'birthdate',
$birthdate->year,
$birthdate->month,
$birthdate->day
);

if ($request->input('addReminder') != '') {
$newReminder = $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $contact->first_name]));
}

break;
$contact->save();
}

dispatch(new ResizeAvatars($contact));

$contact->updateGravatar();

return redirect()->route('people.show', $contact)
->with('success', trans('people.information_edit_success'));
}

/**
* Delete the specified resource.
* Delete the contact.
*
* @param Request $request
* @param Contact $contact
Expand All @@ -434,14 +374,12 @@ public function destroy(Request $request, Contact $contact)
return redirect()->route('people.index');
}

Relationship::where('account_id', auth()->user()->account_id)
->where('contact_is', $contact->id)
->delete();
Relationship::where('account_id', auth()->user()->account_id)
->where('of_contact', $contact->id)
->delete();
$data = [
'account_id' => auth()->user()->account->id,
'contact_id' => $contact->id,
];

$contact->deleteEverything();
(new DestroyContact)->execute($data);

return redirect()->route('people.index')
->with('success', trans('people.people_delete_success'));
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ public function getRelationshipNatureWith(self $otherContact)
}

/**
* Delete the contact and all the related object.
* Delete all related objects.
*
* @return bool
*/
Expand Down
63 changes: 63 additions & 0 deletions app/Services/Contact/Contact/CreateContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Services\Contact\Contact;

use App\Helpers\RandomHelper;
use App\Services\BaseService;
use App\Models\Contact\Contact;

class CreateContact extends BaseService
{
private $contact;

/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'first_name' => 'required|string|max:255',
'middle_name' => 'nullable|string|max:255',
'last_name' => 'nullable|string|max:255',
'nickname' => 'nullable|string|max:255',
'gender_id' => 'required|integer|exists:genders,id',
'description' => 'nullable|string|max:255',
'is_partial' => 'nullable|boolean',
];
}

/**
* Create a contact.
*
* @param array $data
* @return Contact
*/
public function execute(array $data) : Contact
{
$this->validate($data);

$this->contact = Contact::create($data);

$this->generateUUID();

$this->contact->setAvatarColor();

$this->contact->save();

return $this->contact;
}

/**
* Generates a UUID for this contact.
*
* @return void
*/
private function generateUUID()
{
$this->contact->uuid = RandomHelper::uuid();
$this->contact->save();
}
}
Loading

0 comments on commit d242e0f

Please sign in to comment.