Skip to content

Commit

Permalink
feat: manage emotion on calls (monicahq/chandler#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored May 17, 2022
1 parent 37f0003 commit 1378196
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/Models/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Call extends Model
'contact_id',
'call_reason_id',
'author_id',
'emotion_id',
'author_name',
'called_at',
'duration',
Expand Down Expand Up @@ -85,4 +86,14 @@ public function callReason()
{
return $this->belongsTo(CallReason::class, 'call_reason_id');
}

/**
* Get the emotion associated with the call.
*
* @return BelongsTo
*/
public function emotion()
{
return $this->belongsTo(Emotion::class);
}
}
2 changes: 2 additions & 0 deletions database/migrations/2022_05_16_193917_create_calls_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function up()
$table->unsignedBigInteger('contact_id');
$table->unsignedBigInteger('call_reason_id')->nullable();
$table->unsignedBigInteger('author_id')->nullable();
$table->unsignedBigInteger('emotion_id')->nullable();
$table->string('author_name');
$table->datetime('called_at');
$table->integer('duration')->nullable();
Expand All @@ -32,6 +33,7 @@ public function up()
$table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');
$table->foreign('call_reason_id')->references('id')->on('call_reasons')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('emotion_id')->references('id')->on('emotions')->onDelete('set null');
});
}

Expand Down
8 changes: 8 additions & 0 deletions domains/Contact/ManageCalls/Services/CreateCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Interfaces\ServiceInterface;
use App\Models\Call;
use App\Models\CallReason;
use App\Models\Emotion;
use App\Services\BaseService;
use Carbon\Carbon;

Expand Down Expand Up @@ -71,6 +72,12 @@ public function execute(array $data): Call
private function validate(): void
{
$this->validateRules($this->data);

if ($this->valueOrNull($this->data, 'emotion_id')) {
Emotion::where('account_id', $this->data['account_id'])
->where('id', $this->data['emotion_id'])
->firstOrFail();
}
}

private function createCall(): void
Expand All @@ -82,6 +89,7 @@ private function createCall(): void
'called_at' => $this->data['called_at'],
'duration' => $this->valueOrNull($this->data, 'duration'),
'call_reason_id' => $this->valueOrNull($this->data, 'call_reason_id'),
'emotion_id' => $this->valueOrNull($this->data, 'emotion_id'),
'description' => $this->valueOrNull($this->data, 'description'),
'type' => $this->data['type'],
'answered' => $this->valueOrTrue($this->data, 'answered'),
Expand Down
8 changes: 8 additions & 0 deletions domains/Contact/ManageCalls/Services/UpdateCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Interfaces\ServiceInterface;
use App\Models\Call;
use App\Models\Emotion;
use App\Services\BaseService;
use Carbon\Carbon;

Expand Down Expand Up @@ -65,6 +66,7 @@ public function execute(array $data): Call
$call->called_at = $data['called_at'];
$call->duration = $this->valueOrNull($data, 'duration');
$call->call_reason_id = $this->valueOrNull($data, 'call_reason_id');
$call->emotion_id = $this->valueOrNull($data, 'emotion_id');
$call->type = $data['type'];
$call->answered = $this->valueOrTrue($data, 'answered');
$call->who_initiated = $data['who_initiated'];
Expand All @@ -79,5 +81,11 @@ public function execute(array $data): Call
private function validate(): void
{
$this->validateRules($this->data);

if ($this->valueOrNull($this->data, 'emotion_id')) {
Emotion::where('account_id', $this->data['account_id'])
->where('id', $this->data['emotion_id'])
->firstOrFail();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function store(Request $request, int $vaultId, int $contactId)
'vault_id' => $vaultId,
'contact_id' => $contactId,
'call_reason_id' => $request->input('call_reason_id') == 0 ? null : $request->input('call_reason_id'),
'emotion_id' => $request->input('emotion_id') == 0 ? null : $request->input('emotion_id'),
'called_at' => $carbonDate->format('Y-m-d'),
'duration' => $request->input('duration'),
'description' => $request->input('description'),
Expand Down Expand Up @@ -98,6 +99,7 @@ public function update(Request $request, int $vaultId, int $contactId, int $call
'contact_id' => $contactId,
'call_id' => $callId,
'call_reason_id' => $request->input('call_reason_id') == 0 ? null : $request->input('call_reason_id'),
'emotion_id' => $request->input('emotion_id') == 0 ? null : $request->input('emotion_id'),
'called_at' => $carbonDate->format('Y-m-d'),
'duration' => $request->input('duration'),
'description' => $request->input('description'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,19 @@ public static function data(Contact $contact, User $user): array
]);
}

$emotions = $contact->vault->account->emotions()->get();
$emotionsCollection = $emotions->map(function ($emotion) {
return [
'id' => $emotion->id,
'name' => $emotion->name,
'type' => $emotion->type,
];
});

return [
'contact_name' => $contact->getName($user),
'calls' => $callsCollection,
'emotions' => $emotionsCollection,
'call_reason_types' => $callReasonTypesCollection,
'url' => [
'store' => route('contact.call.store', [
Expand All @@ -64,6 +74,11 @@ public static function dto(Contact $contact, Call $call, User $user): array
'who_initiated' => $call->who_initiated,
'type' => $call->type,
'answered' => $call->answered,
'emotion' => $call->emotion ? [
'id' => $call->emotion->id,
'name' => $call->emotion->name,
'type' => $call->emotion->type,
] : null,
'reason' => $call->callReason ? [
'id' => $call->callReason->id,
'label' => $call->callReason->label,
Expand Down
55 changes: 55 additions & 0 deletions resources/js/Shared/Modules/Calls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@
</select>
</div>

<!-- emotion -->
<div v-if="emotionFieldShown" class="border-b border-gray-200 p-5">
<p class="mb-2">How did you feel?</p>
<div v-for="emotion in data.emotions" :key="emotion.id" class="mb-2 flex items-center">
<input
:value="emotion.id"
v-model="form.emotion_id"
:id="emotion.type"
name="emotion"
type="radio"
class="h-4 w-4 border-gray-300 text-indigo-600 focus:ring-indigo-500" />
<label :for="emotion.type" class="ml-2 block cursor-pointer font-medium text-gray-700">
{{ emotion.name }}
</label>
</div>
</div>

<!-- options -->
<div class="border-b border-gray-200 p-5">
<!-- cta to add a description -->
Expand Down Expand Up @@ -274,6 +291,11 @@

<!-- reason, if defined -->
<span v-if="call.reason">{{ call.reason.label }}</span>

<!-- emotion -->
<div v-if="call.emotion" class="text-xs text-gray-600">
{{ call.emotion.name }}
</div>
</div>

<hover-menu :show-edit="true" :show-delete="true" @edit="showUpdateCallModal(call)" @delete="destroy(call)" />
Expand Down Expand Up @@ -418,6 +440,23 @@
</select>
</div>

<!-- emotion -->
<div v-if="emotionFieldShown" class="border-b border-gray-200 p-5">
<p class="mb-2">How did you feel?</p>
<div v-for="emotion in data.emotions" :key="emotion.id" class="mb-2 flex items-center">
<input
:value="emotion.id"
v-model="form.emotion_id"
:id="emotion.type"
name="emotion"
type="radio"
class="h-4 w-4 border-gray-300 text-indigo-600 focus:ring-indigo-500" />
<label :for="emotion.type" class="ml-2 block cursor-pointer font-medium text-gray-700">
{{ emotion.name }}
</label>
</div>
</div>

<!-- options -->
<div class="border-b border-gray-200 p-5">
<!-- cta to add a description -->
Expand Down Expand Up @@ -494,10 +533,12 @@ export default {
editedCallId: 0,
descriptionFieldShown: false,
reasonFieldShown: false,
emotionFieldShown: false,
form: {
called_at: '',
call_reason_id: 0,
description: '',
emotion_id: 0,
type: '',
errors: [],
},
Expand All @@ -521,6 +562,11 @@ export default {
this.form.call_reason_id = '';
},
showEmotionField() {
this.emotionFieldShown = true;
this.form.emotion_id = 1;
},
showCreateCallModal() {
this.form.errors = [];
this.descriptionFieldShown = false;
Expand Down Expand Up @@ -558,6 +604,15 @@ export default {
this.form.call_reason_id = 0;
this.reasonFieldShown = false;
}
if (call.emotion) {
this.form.emotion_id = call.emotion.id;
this.emotionFieldShown = true;
} else {
this.form.emotion_id = 0;
this.emotionFieldShown = false;
}
this.form.called_at = call.called_at;
this.editedCallId = call.id;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contact\ManageCalls\Web\ViewHelpers\ModuleCallsViewHelper;
use App\Models\Call;
use App\Models\Contact;
use App\Models\Emotion;
use App\Models\User;
use Carbon\Carbon;
use function env;
Expand All @@ -24,19 +25,34 @@ public function it_gets_the_data_needed_for_the_view(): void
$call = Call::factory()->create([
'contact_id' => $contact->id,
]);
$emotion = Emotion::factory()->create([
'account_id' => $contact->vault->account_id,
]);

$array = ModuleCallsViewHelper::data($contact, $user);

$this->assertEquals(
4,
5,
count($array)
);

$this->assertArrayHasKey('contact_name', $array);
$this->assertArrayHasKey('emotions', $array);
$this->assertArrayHasKey('calls', $array);
$this->assertArrayHasKey('call_reason_types', $array);
$this->assertArrayHasKey('url', $array);

$this->assertEquals(
[
0 => [
'id' => $emotion->id,
'name' => $emotion->name,
'type' => $emotion->type,
],
],
$array['emotions']->toArray()
);

$this->assertEquals(
$contact->getName($user),
$array['contact_name']
Expand Down Expand Up @@ -73,6 +89,7 @@ public function it_gets_the_data_transfer_object(): void
'who_initiated' => 'me',
'type' => 'audio',
'answered' => true,
'emotion' => null,
'reason' => [
'id' => $call->callReason->id,
'label' => $call->callReason->label,
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Models/CallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit\Models;

use App\Models\Call;
use App\Models\Emotion;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

Expand Down Expand Up @@ -33,4 +34,15 @@ public function it_has_one_call_reason()

$this->assertTrue($call->callReason()->exists());
}

/** @test */
public function it_has_one_emotion()
{
$emotion = Emotion::factory()->create();
$call = Call::factory()->create([
'emotion_id' => $emotion->id,
]);

$this->assertTrue($call->emotion()->exists());
}
}

0 comments on commit 1378196

Please sign in to comment.