Skip to content

Commit

Permalink
fix: fix edit reminder (monicahq/chandler#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Jul 20, 2022
1 parent 51d568b commit 0759d58
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ public function execute(array $data): ContactReminder
$this->validateRules($data);
$this->data = $data;

$this->createContactReminer();
$this->updateLastEditedDate();
$this->createContactReminder();
$this->updateLastUpdatedDate();
$this->scheduledReminderForAllUsersInVault();
$this->log();

return $this->reminder;
}

private function createContactReminer(): void
private function createContactReminder(): void
{
$this->reminder = ContactReminder::create([
'contact_id' => $this->data['contact_id'],
Expand All @@ -82,7 +82,7 @@ private function createContactReminer(): void
]);
}

private function updateLastEditedDate(): void
private function updateLastUpdatedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use App\Models\ContactReminder;
use App\Services\BaseService;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class UpdateReminder extends BaseService implements ServiceInterface
class UpdateContactReminder extends BaseService implements ServiceInterface
{
private ContactReminder $reminder;
private array $data;

/**
* Get the validation rules that apply to the service.
Expand Down Expand Up @@ -58,25 +60,58 @@ public function permissions(): array
*/
public function execute(array $data): ContactReminder
{
$this->validateRules($data);
$this->data = $data;
$this->validate();

$this->reminder = ContactReminder::where('contact_id', $data['contact_id'])
->findOrFail($data['contact_reminder_id']);
$this->update();
$this->deleteOldScheduledReminders();
$this->updateLastUpdatedDate();
$this->scheduledReminderForAllUsersInVault();
$this->log();

return $this->reminder;
}

private function validate(): void
{
$this->validateRules($this->data);

$this->reminder->label = $data['label'];
$this->reminder->day = $data['day'];
$this->reminder->month = $data['month'];
$this->reminder->year = $data['year'];
$this->reminder->type = $data['type'];
$this->reminder->frequency_number = $this->valueOrNull($data, 'frequency_number');
$this->reminder = ContactReminder::where('contact_id', $this->data['contact_id'])
->findOrFail($this->data['contact_reminder_id']);
}

private function update(): void
{
$this->reminder->label = $this->data['label'];
$this->reminder->day = $this->data['day'];
$this->reminder->month = $this->data['month'];
$this->reminder->year = $this->data['year'];
$this->reminder->type = $this->data['type'];
$this->reminder->frequency_number = $this->valueOrNull($this->data, 'frequency_number');
$this->reminder->save();
}

$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
private function deleteOldScheduledReminders(): void
{
DB::table('contact_reminder_scheduled')->where('contact_reminder_id', $this->reminder->id)->delete();
}

$this->log();
private function scheduledReminderForAllUsersInVault(): void
{
$users = $this->vault->users()->get();

return $this->reminder;
foreach ($users as $user) {
(new ScheduleContactReminderForUser())->execute([
'contact_reminder_id' => $this->reminder->id,
'user_id' => $user->id,
]);
}
}

private function updateLastUpdatedDate(): void
{
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function log(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Contact\ManageReminders\Services\CreateContactReminder;
use App\Contact\ManageReminders\Services\DestroyReminder;
use App\Contact\ManageReminders\Services\UpdateReminder;
use App\Contact\ManageReminders\Services\UpdateContactReminder;
use App\Contact\ManageReminders\Web\ViewHelpers\ModuleRemindersViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Contact;
Expand Down Expand Up @@ -92,7 +92,7 @@ public function update(Request $request, int $vaultId, int $contactId, int $remi
'frequency_number' => $frequencyNumber,
];

$reminder = (new UpdateReminder())->execute($data);
$reminder = (new UpdateContactReminder())->execute($data);
$contact = Contact::find($contactId);

return response()->json([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace Tests\Unit\Domains\Contact\ManageReminders\Services;

use App\Contact\ManageReminders\Services\UpdateReminder;
use App\Contact\ManageReminders\Services\UpdateContactReminder;
use App\Exceptions\NotEnoughPermissionException;
use App\Jobs\CreateAuditLog;
use App\Jobs\CreateContactLog;
use App\Models\Account;
use App\Models\Contact;
use App\Models\ContactInformationType;
use App\Models\ContactReminder;
use App\Models\User;
use App\Models\UserNotificationChannel;
use App\Models\Vault;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Queue;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;

class UpdateReminderTest extends TestCase
class UpdateContactReminderTest extends TestCase
{
use DatabaseTransactions;

Expand All @@ -44,7 +44,7 @@ public function it_fails_if_wrong_parameters_are_given(): void
];

$this->expectException(ValidationException::class);
(new UpdateReminder())->execute($request);
(new UpdateContactReminder())->execute($request);
}

/** @test */
Expand Down Expand Up @@ -105,7 +105,6 @@ public function it_fails_if_reminder_is_not_in_the_contact(): void
$vault = $this->createVault($regis->account);
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault);
$contact = Contact::factory()->create(['vault_id' => $vault->id]);
$reminder = ContactInformationType::factory()->create();
$reminder = ContactReminder::factory()->create();

$this->executeService($regis, $regis->account, $vault, $contact, $reminder);
Expand All @@ -115,6 +114,11 @@ private function executeService(User $author, Account $account, Vault $vault, Co
{
Queue::fake();

$channel = UserNotificationChannel::factory()->create([
'user_id' => $author->id,
'preferred_time' => '18:00',
]);

$request = [
'account_id' => $account->id,
'vault_id' => $vault->id,
Expand All @@ -129,7 +133,7 @@ private function executeService(User $author, Account $account, Vault $vault, Co
'frequency_number' => null,
];

$reminder = (new UpdateReminder())->execute($request);
$reminder = (new UpdateContactReminder())->execute($request);

$this->assertDatabaseHas('contact_reminders', [
'id' => $reminder->id,
Expand All @@ -142,6 +146,10 @@ private function executeService(User $author, Account $account, Vault $vault, Co
'frequency_number' => null,
]);

$this->assertDatabaseHas('contact_reminder_scheduled', [
'contact_reminder_id' => $reminder->id,
]);

Queue::assertPushed(CreateAuditLog::class, function ($job) {
return $job->auditLog['action_name'] === 'contact_reminder_updated';
});
Expand Down

0 comments on commit 0759d58

Please sign in to comment.