Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
feat: pet actions appear in feed (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Sep 10, 2022
1 parent 8721225 commit 086e913
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/Models/ContactFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class ContactFeedItem extends Model

public const ACTION_NOTE_DESTROYED = 'note_destroyed';

public const ACTION_PET_CREATED = 'pet_created';

public const ACTION_PET_UPDATED = 'pet_updated';

public const ACTION_PET_DESTROYED = 'pet_destroyed';

public const ACTION_GOAL_CREATED = 'goal_created';

public const ACTION_GOAL_UPDATED = 'goal_updated';
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Pet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;

class Pet extends Model
{
Expand Down Expand Up @@ -42,4 +43,14 @@ public function contact(): BelongsTo
{
return $this->belongsTo(Contact::class);
}

/**
* Get the pet's feed item.
*
* @return MorphOne
*/
public function feedItem(): MorphOne
{
return $this->morphOne(ContactFeedItem::class, 'feedable');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Contact\ManageContactFeed\Web\ViewHelpers\Actions;

use App\Models\ContactFeedItem;

class ActionFeedPet
{
public static function data(ContactFeedItem $item): array
{
$contact = $item->contact;
$pet = $item->feedable;

return [
'pet' => [
'object' => $pet ? [
'id' => $pet->id,
'name' => $pet->name,
'pet_category' => [
'id' => $pet->petCategory->id,
'name' => $pet->petCategory->name,
],
] : null,
'description' => $item->description,
],
'contact' => [
'id' => $contact->id,
'name' => $contact->name,
'age' => $contact->age,
'avatar' => $contact->avatar,
'url' => route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedContactInformation;
use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedGenericContactInformation;
use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedLabelAssigned;
use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedPet;
use App\Helpers\DateHelper;
use App\Helpers\UserHelper;
use App\Models\ContactFeedItem;
Expand Down Expand Up @@ -81,6 +82,11 @@ private static function getData(ContactFeedItem $item, User $user)
case 'contact_information_destroyed':
return ActionFeedContactInformation::data($item);

case 'pet_created':
case 'pet_updated':
case 'pet_destroyed':
return ActionFeedPet::data($item);

default:
return ActionFeedGenericContactInformation::data($item);
}
Expand Down
15 changes: 15 additions & 0 deletions domains/Contact/ManagePets/Services/CreatePet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Contact\ManagePets\Services;

use App\Interfaces\ServiceInterface;
use App\Models\ContactFeedItem;
use App\Models\Pet;
use App\Models\PetCategory;
use App\Services\BaseService;
Expand Down Expand Up @@ -66,6 +67,20 @@ public function execute(array $data): Pet
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();

$this->createFeedItem();

return $this->pet;
}

private function createFeedItem(): void
{
$feedItem = ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_PET_CREATED,
'description' => $this->pet->petCategory->name,
]);

$this->pet->feedItem()->save($feedItem);
}
}
13 changes: 13 additions & 0 deletions domains/Contact/ManagePets/Services/DestroyPet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Contact\ManagePets\Services;

use App\Interfaces\ServiceInterface;
use App\Models\ContactFeedItem;
use App\Models\Pet;
use App\Services\BaseService;
use Carbon\Carbon;
Expand Down Expand Up @@ -58,5 +59,17 @@ public function execute(array $data): void

$this->contact->last_updated_at = Carbon::now();
$this->contact->save();

$this->createFeedItem();
}

private function createFeedItem(): void
{
ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_PET_DESTROYED,
'description' => $this->pet->petCategory->name,
]);
}
}
15 changes: 15 additions & 0 deletions domains/Contact/ManagePets/Services/UpdatePet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Contact\ManagePets\Services;

use App\Interfaces\ServiceInterface;
use App\Models\ContactFeedItem;
use App\Models\Pet;
use App\Models\PetCategory;
use App\Services\BaseService;
Expand Down Expand Up @@ -69,6 +70,20 @@ public function execute(array $data): Pet
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();

$this->createFeedItem();

return $this->pet;
}

private function createFeedItem(): void
{
$feedItem = ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_PET_UPDATED,
'description' => $this->pet->petCategory->name,
]);

$this->pet->feedItem()->save($feedItem);
}
}
3 changes: 3 additions & 0 deletions lang/en/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
'feed_item_address_created' => 'added an address',
'feed_item_address_updated' => 'updated an address',
'feed_item_address_destroyed' => 'deleted an address',
'feed_item_pet_created' => 'added a pet',
'feed_item_pet_updated' => 'updated a pet',
'feed_item_pet_destroyed' => 'deleted a pet',
'feed_item_contact_information_created' => 'added a contact information',
'feed_item_contact_information_updated' => 'updated a contact information',
'feed_item_contact_information_destroyed' => 'deleted a contact information',
Expand Down
11 changes: 11 additions & 0 deletions resources/js/Shared/Modules/Feed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@
"
:data="feedItem.data"
:contact-view-mode="contactViewMode" />

<pet
v-if="
feedItem.action === 'pet_created' ||
feedItem.action === 'pet_updated' ||
feedItem.action === 'pet_destroyed'
"
:data="feedItem.data"
:contact-view-mode="contactViewMode" />
</div>

<!-- details -->
Expand All @@ -121,6 +130,7 @@ import GenericAction from '@/Shared/Modules/FeedItems/GenericAction.vue';
import LabelAssigned from '@/Shared/Modules/FeedItems/LabelAssigned.vue';
import Addresses from '@/Shared/Modules/FeedItems/Address.vue';
import ContactInformation from '@/Shared/Modules/FeedItems/ContactInformation.vue';
import Pet from '@/Shared/Modules/FeedItems/Pet.vue';
export default {
components: {
Expand All @@ -129,6 +139,7 @@ export default {
LabelAssigned,
Addresses,
ContactInformation,
Pet,
},
props: {
Expand Down
52 changes: 52 additions & 0 deletions resources/js/Shared/Modules/FeedItems/Pet.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<template>
<div class="rounded-lg border border-gray-300 dark:border-gray-700">
<!-- contact information -->
<div
v-if="!contactViewMode"
class="flex items-center border-b border-gray-300 px-3 py-2 text-sm dark:border-gray-700">
<avatar
:data="data.contact.avatar"
:classes="'rounded-full border-gray-200 dark:border-gray-800 border relative h-5 w-5 mr-2'" />

<div class="flex flex-col">
<inertia-link :href="data.contact.url" class="text-gray-800 hover:underline dark:text-gray-200">{{
data.contact.name
}}</inertia-link>
</div>
</div>

<div class="px-3 py-2">
<!-- the pet still exists in the system -->
<div v-if="data.pet.object" class="flex items-center">
<span class="mr-2 text-sm text-gray-500">{{ data.pet.object.pet_category.name }}</span>
<span class="mr-2">{{ data.pet.object.name }}</span>
</div>

<!-- the pet was deleted -->
<span v-else class="mr-2 mb-2">
<span>{{ data.pet.description }}</span>
</span>
</div>
</div>
</template>

<script>
import Avatar from '@/Shared/Avatar.vue';
export default {
components: {
Avatar,
},
props: {
data: {
type: Object,
default: null,
},
contactViewMode: {
type: Boolean,
default: true,
},
},
};
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Tests\Unit\Domains\Contact\ManageContactFeed\Web\ViewHelpers\Actions;

use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedPet;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Models\Pet;
use App\Models\PetCategory;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class ActionFeedPetTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_gets_the_data_needed_for_the_view(): void
{
$contact = Contact::factory()->create([
'first_name' => 'John',
'last_name' => 'Doe',
]);
$petCategory = PetCategory::factory()->create([
'name' => 'Dog',
]);
$pet = Pet::factory()->create([
'pet_category_id' => $petCategory->id,
'name' => 'Charles',
]);

$feedItem = ContactFeedItem::factory()->create([
'contact_id' => $contact->id,
'action' => ContactFeedItem::ACTION_PET_CREATED,
'description' => 'pet',
]);
$pet->feedItem()->save($feedItem);

$array = ActionFeedPet::data($feedItem);

$this->assertEquals(
[
'pet' => [
'object' => [
'id' => $pet->id,
'name' => 'Charles',
'pet_category' => [
'id' => $petCategory->id,
'name' => 'Dog',
],
],
'description' => 'pet',
],
'contact' => [
'id' => $contact->id,
'name' => 'John Doe',
'age' => null,
'avatar' => $contact->avatar,
'url' => env('APP_URL').'/vaults/'.$contact->vault_id.'/contacts/'.$contact->id,
],
],
$array
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exceptions\NotEnoughPermissionException;
use App\Models\Account;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Models\PetCategory;
use App\Models\User;
use App\Models\Vault;
Expand Down Expand Up @@ -117,5 +118,10 @@ private function executeService(User $author, Account $account, Vault $vault, Co
'pet_category_id' => $petCategory->id,
'name' => 'boubou',
]);

$this->assertDatabaseHas('contact_feed_items', [
'contact_id' => $contact->id,
'action' => ContactFeedItem::ACTION_PET_CREATED,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exceptions\NotEnoughPermissionException;
use App\Models\Account;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Models\Pet;
use App\Models\User;
use App\Models\Vault;
Expand Down Expand Up @@ -121,5 +122,10 @@ private function executeService(User $author, Account $account, Vault $vault, Co
$this->assertDatabaseMissing('pets', [
'id' => $pet->id,
]);

$this->assertDatabaseHas('contact_feed_items', [
'contact_id' => $contact->id,
'action' => ContactFeedItem::ACTION_PET_DESTROYED,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exceptions\NotEnoughPermissionException;
use App\Models\Account;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Models\ContactInformationType;
use App\Models\Pet;
use App\Models\PetCategory;
Expand Down Expand Up @@ -158,5 +159,10 @@ private function executeService(User $author, Account $account, Vault $vault, Co
'pet_category_id' => $petCategory->id,
'name' => 'boubou',
]);

$this->assertDatabaseHas('contact_feed_items', [
'contact_id' => $contact->id,
'action' => ContactFeedItem::ACTION_PET_UPDATED,
]);
}
}

0 comments on commit 086e913

Please sign in to comment.