diff --git a/app/Models/ContactFeedItem.php b/app/Models/ContactFeedItem.php index 663cc343c..95ebb3738 100644 --- a/app/Models/ContactFeedItem.php +++ b/app/Models/ContactFeedItem.php @@ -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'; diff --git a/app/Models/Pet.php b/app/Models/Pet.php index 5f181a2e2..c9df7952a 100644 --- a/app/Models/Pet.php +++ b/app/Models/Pet.php @@ -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 { @@ -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'); + } } diff --git a/domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedPet.php b/domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedPet.php new file mode 100644 index 000000000..f38c9388b --- /dev/null +++ b/domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedPet.php @@ -0,0 +1,38 @@ +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, + ]), + ], + ]; + } +} diff --git a/domains/Contact/ManageContactFeed/Web/ViewHelpers/ModuleFeedViewHelper.php b/domains/Contact/ManageContactFeed/Web/ViewHelpers/ModuleFeedViewHelper.php index 15166b73f..168f644a5 100644 --- a/domains/Contact/ManageContactFeed/Web/ViewHelpers/ModuleFeedViewHelper.php +++ b/domains/Contact/ManageContactFeed/Web/ViewHelpers/ModuleFeedViewHelper.php @@ -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; @@ -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); } diff --git a/domains/Contact/ManagePets/Services/CreatePet.php b/domains/Contact/ManagePets/Services/CreatePet.php index c097e2de6..1a41706b7 100644 --- a/domains/Contact/ManagePets/Services/CreatePet.php +++ b/domains/Contact/ManagePets/Services/CreatePet.php @@ -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; @@ -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); + } } diff --git a/domains/Contact/ManagePets/Services/DestroyPet.php b/domains/Contact/ManagePets/Services/DestroyPet.php index a8749914a..1b23d8aff 100644 --- a/domains/Contact/ManagePets/Services/DestroyPet.php +++ b/domains/Contact/ManagePets/Services/DestroyPet.php @@ -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; @@ -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, + ]); } } diff --git a/domains/Contact/ManagePets/Services/UpdatePet.php b/domains/Contact/ManagePets/Services/UpdatePet.php index 467e3d369..693f26a39 100644 --- a/domains/Contact/ManagePets/Services/UpdatePet.php +++ b/domains/Contact/ManagePets/Services/UpdatePet.php @@ -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; @@ -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); + } } diff --git a/lang/en/contact.php b/lang/en/contact.php index 203e70c27..a902c3663 100644 --- a/lang/en/contact.php +++ b/lang/en/contact.php @@ -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', diff --git a/resources/js/Shared/Modules/Feed.vue b/resources/js/Shared/Modules/Feed.vue index 2b3676af5..ce3852547 100644 --- a/resources/js/Shared/Modules/Feed.vue +++ b/resources/js/Shared/Modules/Feed.vue @@ -95,6 +95,15 @@ " :data="feedItem.data" :contact-view-mode="contactViewMode" /> + + @@ -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: { @@ -129,6 +139,7 @@ export default { LabelAssigned, Addresses, ContactInformation, + Pet, }, props: { diff --git a/resources/js/Shared/Modules/FeedItems/Pet.vue b/resources/js/Shared/Modules/FeedItems/Pet.vue new file mode 100644 index 000000000..f11f13212 --- /dev/null +++ b/resources/js/Shared/Modules/FeedItems/Pet.vue @@ -0,0 +1,52 @@ + + + + + + + + {{ + data.contact.name + }} + + + + + + + {{ data.pet.object.pet_category.name }} + {{ data.pet.object.name }} + + + + + {{ data.pet.description }} + + + + + + diff --git a/tests/Unit/Domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedPetTest.php b/tests/Unit/Domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedPetTest.php new file mode 100644 index 000000000..efc01af05 --- /dev/null +++ b/tests/Unit/Domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedPetTest.php @@ -0,0 +1,65 @@ +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 + ); + } +} diff --git a/tests/Unit/Domains/Contact/ManagePets/Services/CreatePetTest.php b/tests/Unit/Domains/Contact/ManagePets/Services/CreatePetTest.php index 016c5a299..385cd483e 100644 --- a/tests/Unit/Domains/Contact/ManagePets/Services/CreatePetTest.php +++ b/tests/Unit/Domains/Contact/ManagePets/Services/CreatePetTest.php @@ -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; @@ -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, + ]); } } diff --git a/tests/Unit/Domains/Contact/ManagePets/Services/DestroyPetTest.php b/tests/Unit/Domains/Contact/ManagePets/Services/DestroyPetTest.php index 47ef7d013..34be004e7 100644 --- a/tests/Unit/Domains/Contact/ManagePets/Services/DestroyPetTest.php +++ b/tests/Unit/Domains/Contact/ManagePets/Services/DestroyPetTest.php @@ -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; @@ -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, + ]); } } diff --git a/tests/Unit/Domains/Contact/ManagePets/Services/UpdatePetTest.php b/tests/Unit/Domains/Contact/ManagePets/Services/UpdatePetTest.php index cf42db222..618ebe77a 100644 --- a/tests/Unit/Domains/Contact/ManagePets/Services/UpdatePetTest.php +++ b/tests/Unit/Domains/Contact/ManagePets/Services/UpdatePetTest.php @@ -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; @@ -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, + ]); } }