Skip to content

Commit

Permalink
feat: update last called attribute (#5614)
Browse files Browse the repository at this point in the history
  • Loading branch information
samfelgar authored Oct 26, 2021
1 parent 38429a2 commit 83e1d68
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 5 deletions.
20 changes: 20 additions & 0 deletions app/Http/Controllers/Contacts/CallsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Contacts;

use App\Helpers\DateHelper;
use App\Models\Contact\Call;
use Illuminate\Http\Request;
use App\Models\Contact\Contact;
Expand Down Expand Up @@ -30,6 +31,25 @@ public function index(Request $request, Contact $contact)
return CallResource::collection($calls);
}

/**
* Display the timestamp of the last phone contact.
*
* @param Contact $contact
* @return JsonResponse
*/
public function lastCalled(Contact $contact): JsonResponse
{
$lastTalkedTo = $contact->last_talked_to;

if ($lastTalkedTo !== null) {
$lastTalkedTo = DateHelper::getShortDate($contact->last_talked_to);
}

return $this->respond([
'last_talked_to' => $lastTalkedTo,
]);
}

/**
* Store a call.
*
Expand Down
5 changes: 5 additions & 0 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ Vue.component(
require('./components/people/StayInTouch.vue').default
);

Vue.component(
'LastCalled',
require('./components/people/calls/LastCalled.vue').default
);

Vue.component(
'PhoneCallList',
require('./components/people/calls/PhoneCallList.vue').default
Expand Down
47 changes: 47 additions & 0 deletions resources/js/components/people/calls/LastCalled.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<template>
<span v-cy-name="'last-talked-to'">{{ lastCalledMessage }}</span>
</template>

<script>
export default {
props: {
hash: {
type: String,
default: '',
},
initialValue: {
type: String,
default: '',
}
},
data() {
return {
lastCalled: '',
};
},
computed: {
lastCalledMessage() {
if (!this.initialValue && !this.lastCalled) {
return this.$t('people.last_called_empty');
}
if (!this.lastCalled) {
return this.$t('people.last_talked_to', {date: this.initialValue});
}
return this.$t('people.last_talked_to', {date: this.lastCalled});
}
},
methods: {
getLastCalled() {
axios.get('people/' + this.hash + '/calls/last')
.then(response => {
this.lastCalled = response.data.last_talked_to;
});
}
}
};
</script>
7 changes: 7 additions & 0 deletions resources/js/components/people/calls/PhoneCallList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export default {
this.resetFields();
this.displayLogCall = false;
this.chosenEmotions = [];
this.updateLastCalled();
this.$notify({
group: 'main',
Expand All @@ -361,6 +362,7 @@ export default {
this.getCalls();
this.editCallId = 0;
this.chosenEmotions = [];
this.updateLastCalled();
this.$notify({
group: 'main',
Expand All @@ -371,6 +373,10 @@ export default {
});
},
updateLastCalled() {
this.$parent.$refs.lastCalledAttribute.getLastCalled();
},
showEditBox(call) {
this.editCallId = call.id;
this.editCall.content = call.content;
Expand All @@ -390,6 +396,7 @@ export default {
axios.delete('people/' + this.hash + '/calls/' + this.destroyCallId)
.then(response => {
this.calls.splice(this.calls.indexOf(call), 1);
this.updateLastCalled();
});
},
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/people.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@

// Last called
'last_called' => 'Last called: :date',
'last_talked_to' => 'Last called: {date}',
'last_called_empty' => 'Last called: unknown',
'last_activity_date' => 'Last activity together: :date',
'last_activity_date_empty' => 'Last activity together: unknown',
Expand Down
10 changes: 5 additions & 5 deletions resources/views/people/_header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@
@if (! $contact->isMe())
<li class="mb2 mb0-ns dn di-ns tc {{ htmldir() == 'ltr' ? 'mr3-ns' : 'ml3-ns' }}">
<span class="{{ htmldir() == 'ltr' ? 'mr1' : 'ml1' }}">@include('partials.icons.header_call')</span>
@if (is_null($contact->last_talked_to))
{{ trans('people.last_called_empty') }}
@else
{{ trans('people.last_called', ['date' => \App\Helpers\DateHelper::getShortDate($contact->last_talked_to)]) }}
@endif
<last-called hash="{{ $contact->hashID() }}"
initial-value="{{ is_null($contact->last_talked_to) ? null : \App\Helpers\DateHelper::getShortDate($contact->last_talked_to) }}"
ref="lastCalledAttribute"
>
</last-called>
</li>
@endif

Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Contacts\CallsController;

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -156,6 +157,7 @@
Route::resource('people/{contact}/debts', 'Contacts\\DebtController')->except(['index', 'show']);

// Phone calls
Route::get('people/{contact}/calls/last', [CallsController::class, 'lastCalled']);
Route::resource('people/{contact}/calls', 'Contacts\\CallsController')->except(['show']);

// Conversations
Expand Down
50 changes: 50 additions & 0 deletions tests/Feature/CallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Tests\Feature;

use Tests\FeatureTestCase;
use App\Helpers\DateHelper;
use App\Models\Contact\Call;
use App\Models\Contact\Contact;
use App\Services\Contact\Call\CreateCall;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class CallsTest extends FeatureTestCase
Expand Down Expand Up @@ -57,6 +59,54 @@ public function test_it_gets_the_list_of_calls()
);
}

/** @test */
public function it_gets_last_talked_to()
{
$user = $this->signin();

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

$referenceDate = now();

app(CreateCall::class)->execute([
'account_id' => $user->account_id,
'contact_id' => $contact->id,
'called_at' => $referenceDate->format('Y-m-d'),
]);

$response = $this->json('GET', "/people/{$contact->hashId()}/calls/last");

$response->assertStatus(200);

$response->assertJsonStructure([
'last_talked_to',
]);

$this->assertEquals($response->json('last_talked_to'), DateHelper::getShortDate($referenceDate));
}

/** @test */
public function it_gets_a_empty_last_talked_to()
{
$user = $this->signin();

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

$response = $this->json('GET', "/people/{$contact->hashId()}/calls/last");

$response->assertStatus(200);

$response->assertJsonStructure([
'last_talked_to',
]);

$this->assertNull($response->json('last_talked_to'));
}

public function test_dashboard_calls()
{
$user = $this->signin();
Expand Down
3 changes: 3 additions & 0 deletions tests/cypress/integration/contacts/calls_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ describe('Calls', function () {
cy.url().should('include', '/people/h:');
cy.get('[cy-name=calls-blank-state]').should('exist');
cy.get('[cy-name=log-call-form]').should('not.be.visible');
cy.get('[cy-name=last-talked-to]').should('contain', 'unknown');

// add a call
cy.get('[cy-name=add-call-button]').click();
cy.get('[cy-name=log-call-form]').should('be.visible');
cy.get('[cy-name=save-call-button]').click();
cy.get('[cy-name=calls-blank-state]').should('not.exist');
cy.get('[cy-name=last-talked-to]').should('not.contain', 'unknown');

cy.get('[cy-name=calls-body]').should('be.visible')
.invoke('attr', 'cy-items').then(function (items) {
Expand All @@ -30,6 +32,7 @@ describe('Calls', function () {
cy.get('[cy-name=delete-call-confirm-button-'+item+']').should('be.visible');
cy.get('[cy-name=delete-call-confirm-button-'+item+']').click();
cy.get('[cy-name=calls-blank-state]').should('exist');
cy.get('[cy-name=last-talked-to]').should('contain', 'unknown');
});
});
});

0 comments on commit 83e1d68

Please sign in to comment.