Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: services to set and clear personal description #3573

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions app/Services/Contact/Description/ClearPersonalDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Services\Contact\Description;

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

class ClearPersonalDescription extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'contact_id' => 'required|integer|exists:contacts,id',
'author_id' => 'required|integer|exists:users,id',
];
}

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

$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->description = null;
$contact->save();

return $contact;
}
}
46 changes: 46 additions & 0 deletions app/Services/Contact/Description/SetPersonalDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Services\Contact\Description;

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

class SetPersonalDescription extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'contact_id' => 'required|integer|exists:contacts,id',
'author_id' => 'required|integer|exists:users,id',
'description' => 'required|string|max:255',
];
}

/**
* Set a contact's description.
* The description should be saved as unparsed markdown content, and fetched
* as unparsed markdown content. The UI is responsible for parsing and
* displaying the proper content.
*
* @param array $data
* @return Contact
*/
public function execute(array $data): Contact
{
$this->validate($data);

$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);

$contact->description = $data['description'];
$contact->save();

return $contact;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Tests\Unit\Services\Contact\Description;

use Tests\TestCase;
use App\Models\User\User;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Queue;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Contact\Description\ClearPersonalDescription;

class ClearPersonalDescriptionTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_clears_a_personal_description(): void
{
Queue::fake();

$contact = factory(Contact::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $contact->account_id,
]);

$request = [
'account_id' => $contact->account_id,
'author_id' => $user->id,
'contact_id' => $contact->id,
];

$michael = (new ClearPersonalDescription)->execute($request);

$this->assertDatabaseHas('contacts', [
'account_id' => $contact->account_id,
'id' => $contact->id,
'description' => null,
]);

$this->assertInstanceOf(
Contact::class,
$contact
);
}

/** @test */
public function it_fails_if_wrong_parameters_are_given(): void
{
$request = [
'first_name' => 'Dwight',
];

$this->expectException(ValidationException::class);
(new ClearPersonalDescription)->execute($request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Tests\Unit\Services\Contact\Description;

use Tests\TestCase;
use App\Models\User\User;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Queue;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Contact\Description\SetPersonalDescription;

class SetPersonalDescriptionTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_sets_a_personal_description(): void
{
Queue::fake();

$contact = factory(Contact::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $contact->account_id,
]);

$request = [
'account_id' => $contact->account_id,
'author_id' => $user->id,
'contact_id' => $contact->id,
'description' => 'This is just great',
];

$contact = (new SetPersonalDescription)->execute($request);

$this->assertDatabaseHas('contacts', [
'account_id' => $contact->account_id,
'id' => $contact->id,
'description' => 'This is just great',
]);

$this->assertInstanceOf(
Contact::class,
$contact
);
}

/** @test */
public function it_fails_if_wrong_parameters_are_given(): void
{
$request = [
'first_name' => 'Dwight',
];

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