From 59064fbf25895276107ecf90ca54b4faafefbba8 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sun, 3 Mar 2019 21:27:37 +0100 Subject: [PATCH] fix: set deceased information with removing date and reminder (#2416) --- CHANGELOG | 1 + .../Contact/UpdateDeceasedInformation.php | 41 ++++++++++++++++++- .../Contact/Reminder/CreateReminder.php | 2 +- .../Contact/Reminder/UpdateReminder.php | 2 +- database/seeds/FakeContentTableSeeder.php | 18 ++++---- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 6bb508bbdc8..250aa9275e0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -10,6 +10,7 @@ Enhancements: Fixes: +* Fix setting deceased information with removing date and reminder * Fix contact information update * Fix adding people on activity create and update * Fix setting a relationship without selecting any birthdate option diff --git a/app/Services/Contact/Contact/UpdateDeceasedInformation.php b/app/Services/Contact/Contact/UpdateDeceasedInformation.php index 7bb66f9103a..045d5d220ea 100644 --- a/app/Services/Contact/Contact/UpdateDeceasedInformation.php +++ b/app/Services/Contact/Contact/UpdateDeceasedInformation.php @@ -6,6 +6,7 @@ use App\Models\Contact\Contact; use App\Models\Instance\SpecialDate; use App\Services\Contact\Reminder\CreateReminder; +use App\Services\Contact\Reminder\DestroyReminder; class UpdateDeceasedInformation extends BaseService { @@ -41,11 +42,49 @@ public function execute(array $data) $contact = Contact::where('account_id', $data['account_id']) ->findOrFail($data['contact_id']); + $this->clearRelatedReminder($contact); + + $this->clearRelatedSpecialDate($contact); + $this->manageDeceasedDate($data, $contact); return $contact; } + /** + * Delete related reminder. + * + * @param Contact $contact + * @return void + */ + private function clearRelatedReminder(Contact $contact) + { + if (is_null($contact->deceased_reminder_id)) { + return; + } + + app(DestroyReminder::class)->execute([ + 'account_id' => $contact->account_id, + 'reminder_id' => $contact->deceased_reminder_id, + ]); + } + + /** + * Delete related special date. + * + * @param Contact $contact + * @return void + */ + private function clearRelatedSpecialDate(Contact $contact) + { + if (is_null($contact->deceased_special_date_id)) { + return; + } + + $specialDate = SpecialDate::find($contact->deceased_special_date_id); + $specialDate->delete(); + } + /** * Update deceased date information depending on the type of information. * @@ -74,7 +113,7 @@ private function manageDeceasedDate(array $data, Contact $contact): void } /** - * Case where we have a year, month and day for the birthday. + * Case where we have a year, month and day for the date. * * @param array $data * @param Contact $contact diff --git a/app/Services/Contact/Reminder/CreateReminder.php b/app/Services/Contact/Reminder/CreateReminder.php index 4f8daeff4ad..2cb5d6dd568 100644 --- a/app/Services/Contact/Reminder/CreateReminder.php +++ b/app/Services/Contact/Reminder/CreateReminder.php @@ -19,7 +19,7 @@ public function rules() return [ 'account_id' => 'required|integer|exists:accounts,id', 'contact_id' => 'required|integer|exists:contacts,id', - 'initial_date' => 'required|date|date_format:Y-m-d', + 'initial_date' => 'required|date_format:Y-m-d', 'frequency_type' => [ 'required', Rule::in(Reminder::$frequencyTypes), diff --git a/app/Services/Contact/Reminder/UpdateReminder.php b/app/Services/Contact/Reminder/UpdateReminder.php index 5e89e663885..22dff782f3c 100644 --- a/app/Services/Contact/Reminder/UpdateReminder.php +++ b/app/Services/Contact/Reminder/UpdateReminder.php @@ -19,7 +19,7 @@ public function rules() 'account_id' => 'required|integer|exists:accounts,id', 'contact_id' => 'required|integer|exists:contacts,id', 'reminder_id' => 'required|integer|exists:reminders,id', - 'initial_date' => 'required|date|date_format:Y-m-d', + 'initial_date' => 'required|date_format:Y-m-d', 'frequency_type' => [ 'required', Rule::in(Reminder::$frequencyTypes), diff --git a/database/seeds/FakeContentTableSeeder.php b/database/seeds/FakeContentTableSeeder.php index 68aa2472485..d1f7db15f4e 100644 --- a/database/seeds/FakeContentTableSeeder.php +++ b/database/seeds/FakeContentTableSeeder.php @@ -159,12 +159,12 @@ public function populateDeceasedDate() app(UpdateDeceasedInformation::class)->execute([ 'account_id' => $this->contact->account_id, 'contact_id' => $this->contact->id, - 'is_deceased' => (rand(1, 2) == 1) ? true : false, - 'is_date_known' => (rand(1, 2) == 1) ? true : false, + 'is_deceased' => rand(1, 2) == 1, + 'is_date_known' => rand(1, 2) == 1, 'day' => (int) $birthdate->format('d'), 'month' => (int) $birthdate->format('m'), 'year' => (int) $birthdate->format('Y'), - 'add_reminder' => (rand(1, 2) == 1) ? true : false, + 'add_reminder' => rand(1, 2) == 1, ]); } } @@ -177,13 +177,13 @@ public function populateBirthday() app(UpdateBirthdayInformation::class)->execute([ 'account_id' => $this->contact->account_id, 'contact_id' => $this->contact->id, - 'is_date_known' => (rand(1, 2) == 1) ? true : false, + 'is_date_known' => rand(1, 2) == 1, 'day' => (int) $birthdate->format('d'), 'month' => (int) $birthdate->format('m'), 'year' => (int) $birthdate->format('Y'), - 'is_age_based' => (rand(1, 2) == 1) ? true : false, + 'is_age_based' => rand(1, 2) == 1, 'age' => rand(1, 99), - 'add_reminder' => (rand(1, 2) == 1) ? true : false, + 'add_reminder' => rand(1, 2) == 1, ]); } } @@ -255,13 +255,13 @@ public function populateRelationships() app(UpdateBirthdayInformation::class)->execute([ 'account_id' => $this->contact->account_id, 'contact_id' => $relatedContact->id, - 'is_date_known' => (rand(1, 2) == 1) ? true : false, + 'is_date_known' => rand(1, 2) == 1, 'day' => (int) $relatedContactBirthDate->format('d'), 'month' => (int) $relatedContactBirthDate->format('m'), 'year' => (int) $relatedContactBirthDate->format('Y'), - 'is_age_based' => (rand(1, 2) == 1) ? true : false, + 'is_age_based' => rand(1, 2) == 1, 'age' => rand(1, 99), - 'add_reminder' => (rand(1, 2) == 1) ? true : false, + 'add_reminder' => rand(1, 2) == 1, ]); // set relationship