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

feat: manage gifts #131

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e9a449e
Update 2022_06_09_173049_create_gifts_table.php
djaiss Jun 10, 2022
5ccbb0d
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Jun 27, 2022
527b889
wip
djaiss Jun 27, 2022
7a4961b
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Jun 28, 2022
83af8d5
wip
djaiss Jun 29, 2022
f75a7f3
Apply fixes from StyleCI
StyleCIBot Jun 29, 2022
1bd5d7f
Merg:wq branch 'main' into 2022-06-09-gifts-final
djaiss Jul 8, 2022
af1bf9e
Update CreateGift.php
djaiss Jul 8, 2022
1d0eb5d
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Jul 22, 2022
81248dc
Update composer.lock
djaiss Jul 22, 2022
c4e9993
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Jul 22, 2022
7345992
wip
djaiss Jul 22, 2022
9d9e6cd
wip
djaiss Jul 22, 2022
1ec5f30
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Nov 15, 2022
f2dd66b
chore: php linting with pint
djaiss Nov 15, 2022
d8f57a5
wip
djaiss Nov 18, 2022
8722ae3
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Dec 3, 2022
54bc139
Update 2022_06_09_173049_create_gifts_table.php
djaiss Dec 3, 2022
27a42c3
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Dec 4, 2022
56996fb
wip
djaiss Dec 4, 2022
fe3f5f3
wip
djaiss Dec 4, 2022
4a8631c
wip
djaiss Dec 4, 2022
d5f1fc5
Delete GiftUrlFactory.php
djaiss Dec 4, 2022
7fae1a1
Create GiftIndexViewHelper.php
djaiss Dec 4, 2022
d4c00d0
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Dec 4, 2022
f19f9d0
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Dec 6, 2022
37d3b82
Merge branch 'main' into 2022-06-09-gifts-final
djaiss Mar 23, 2023
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
125 changes: 125 additions & 0 deletions app/Domains/Contact/ManageGifts/Services/CreateGift.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace App\Domains\Contact\ManageGifts\Services;

use App\Interfaces\ServiceInterface;
use App\Models\Gift;
use App\Services\BaseService;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class CreateGift extends BaseService implements ServiceInterface
{
private Gift $gift;

private Collection $donatorsCollection;

private Collection $recipientsCollection;

private array $data;

/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'vault_id' => 'required|integer|exists:vaults,id',
'author_id' => 'required|integer|exists:users,id',
'gift_occasion_id' => 'nullable|integer|exists:gift_occasions,id',
'gift_state_id' => 'nullable|integer|exists:gift_states,id',
'currency_id' => 'nullable|integer|exists:currencies,id',
'name' => 'required|string|max:65535',
'description' => 'nullable|string|max:65535',
'budget' => 'nullable|integer',
'donators_ids' => 'required',
'recipients_ids' => 'required',
];
}

/**
* Get the permissions that apply to the user calling the service.
*
* @return array
*/
public function permissions(): array
{
return [
'author_must_belong_to_account',
'vault_must_belong_to_account',
'author_must_be_vault_editor',
];
}

/**
* Create a gift.
*
* @param array $data
* @return Gift
*/
public function execute(array $data): Gift
{
$this->data = $data;
$this->validate();
$this->create();

return $this->gift;
}

private function validate(): void
{
$this->validateRules($this->data);

$this->donatorsCollection = collect();
foreach ($this->data['donators_ids'] as $donatorId) {
$this->donatorsCollection->push(
$this->vault->contacts()->findOrFail($donatorId)
);
}

$this->recipientsCollection = collect();
foreach ($this->data['recipients_ids'] as $recipientId) {
$this->recipientsCollection->push(
$this->vault->contacts()->findOrFail($recipientId)
);
}

if ($this->valueOrNull($this->data, 'gift_occasion_id')) {
$this->account()->giftOccasions()->findOrFail($this->data['gift_occasion_id']);
}

if ($this->valueOrNull($this->data, 'gift_state_id')) {
$this->account()->giftStates()->findOrFail($this->data['gift_state_id']);
}
}

private function create(): void
{
$this->gift = Gift::create([
'vault_id' => $this->data['vault_id'],
'gift_occasion_id' => $this->valueOrNull($this->data, 'gift_occasion_id'),
'gift_state_id' => $this->valueOrNull($this->data, 'gift_state_id'),
'name' => $this->data['name'],
'description' => $this->valueOrNull($this->data, 'description'),
'budget' => $this->valueOrNull($this->data, 'budget'),
'currency_id' => $this->valueOrNull($this->data, 'currency_id'),
'shareable_link' => (string) Str::uuid(),
]);

foreach ($this->donatorsCollection as $donator) {
$donator->giftsAsDonator()->syncWithoutDetaching([$this->gift->id]);
$donator->last_updated_at = Carbon::now();
$donator->save();
}

foreach ($this->recipientsCollection as $recipient) {
$recipient->giftsAsRecipient()->syncWithoutDetaching([$this->gift->id]);
$recipient->last_updated_at = Carbon::now();
$recipient->save();
}
}
}
55 changes: 55 additions & 0 deletions app/Domains/Contact/ManageGifts/Services/DestroyGift.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Domains\Contact\ManageGifts\Services;

use App\Interfaces\ServiceInterface;
use App\Models\Gift;
use App\Services\BaseService;

class DestroyGift extends BaseService implements ServiceInterface
{
private Gift $gift;

/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'vault_id' => 'required|integer|exists:vaults,id',
'author_id' => 'required|integer|exists:users,id',
'gift_id' => 'required|integer|exists:gifts,id',
];
}

/**
* Get the permissions that apply to the user calling the service.
*
* @return array
*/
public function permissions(): array
{
return [
'author_must_belong_to_account',
'vault_must_belong_to_account',
'author_must_be_vault_editor',
];
}

/**
* Destroy a gift.
*
* @param array $data
*/
public function execute(array $data): void
{
$this->validateRules($data);

$this->gift = $this->vault->gifts()->findOrFail($data['gift_id']);

$this->gift->delete();
}
}
128 changes: 128 additions & 0 deletions app/Domains/Contact/ManageGifts/Services/UpdateGift.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace App\Domains\Contact\ManageGifts\Services;

use App\Interfaces\ServiceInterface;
use App\Models\Gift;
use App\Services\BaseService;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class UpdateGift extends BaseService implements ServiceInterface
{
private Gift $gift;

private Collection $donatorsCollection;

private Collection $recipientsCollection;

private array $data;

/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'vault_id' => 'required|integer|exists:vaults,id',
'author_id' => 'required|integer|exists:users,id',
'gift_id' => 'required|integer|exists:gifts,id',
'gift_occasion_id' => 'nullable|integer|exists:gift_occasions,id',
'gift_state_id' => 'nullable|integer|exists:gift_states,id',
'currency_id' => 'nullable|integer|exists:currencies,id',
'name' => 'required|string|max:65535',
'description' => 'nullable|string|max:65535',
'budget' => 'nullable|integer',
'donators_ids' => 'required',
'recipients_ids' => 'required',
];
}

/**
* Get the permissions that apply to the user calling the service.
*
* @return array
*/
public function permissions(): array
{
return [
'author_must_belong_to_account',
'vault_must_belong_to_account',
'author_must_be_vault_editor',
];
}

/**
* Update a gift.
*
* @param array $data
* @return Gift
*/
public function execute(array $data): Gift
{
$this->data = $data;
$this->validate();
$this->update();

return $this->gift;
}

private function validate(): void
{
$this->validateRules($this->data);

$this->gift = $this->vault->gifts()->findOrFail($this->data['gift_id']);

$this->donatorsCollection = collect();
foreach ($this->data['donators_ids'] as $donatorId) {
$this->donatorsCollection->push(
$this->vault->contacts()->findOrFail($donatorId)
);
}

$this->recipientsCollection = collect();
foreach ($this->data['recipients_ids'] as $recipientId) {
$this->recipientsCollection->push(
$this->vault->contacts()->findOrFail($recipientId)
);
}

if ($this->valueOrNull($this->data, 'gift_occasion_id')) {
$this->account()->giftOccasions()->findOrFail($this->data['gift_occasion_id']);
}

if ($this->valueOrNull($this->data, 'gift_state_id')) {
$this->account()->giftStates()->findOrFail($this->data['gift_state_id']);
}
}

private function update(): void
{
$this->gift->name = $this->data['name'];
$this->gift->description = $this->valueOrNull($this->data, 'description');
$this->gift->budget = $this->valueOrNull($this->data, 'budget');
$this->gift->currency_id = $this->valueOrNull($this->data, 'currency_id');
$this->gift->gift_occasion_id = $this->valueOrNull($this->data, 'gift_occasion_id');
$this->gift->gift_state_id = $this->valueOrNull($this->data, 'gift_state_id');
$this->gift->save();

DB::table('gift_donators')->where('gift_id', $this->gift->id)->delete();
DB::table('gift_recipients')->where('gift_id', $this->gift->id)->delete();

foreach ($this->donatorsCollection as $donator) {
$donator->giftsAsDonator()->syncWithoutDetaching([$this->gift->id]);
$donator->last_updated_at = Carbon::now();
$donator->save();
}

foreach ($this->recipientsCollection as $recipient) {
$recipient->giftsAsRecipient()->syncWithoutDetaching([$this->gift->id]);
$recipient->last_updated_at = Carbon::now();
$recipient->save();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Domains\Vault\ManageGifts\Web\ViewHelpers;

use App\Helpers\DateHelper;
use App\Models\Vault;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class GiftIndexViewHelper
{
public static function data(Vault $vault): Collection
{
$contacts = $vault->contacts()
->orderBy('last_name', 'asc')
->get();

$contactsCollection = collect();
foreach ($contacts as $contact) {
$tasks = DB::table('contact_tasks')
->where('completed', false)
->where('contact_id', $contact->id)
->orderBy('due_at', 'asc')
->get();

$tasksCollection = collect();
foreach ($tasks as $task) {
$tasksCollection->push([
'id' => $task->id,
'label' => $task->label,
'due_at' => $task->due_at ? DateHelper::format(Carbon::parse($task->due_at), $user) : null,
'due_at_late' => optional(Carbon::parse($task->due_at))->isPast() ?? false,
'url' => [
'toggle' => route('contact.task.toggle', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
'task' => $task->id,
]),
],
]);
}

if ($tasksCollection->count() <= 0) {
continue;
}

$contactsCollection->push([
'tasks' => $tasksCollection,
'contact' => [
'id' => $contact->id,
'name' => $contact->name,
'avatar' => $contact->avatar,
'url' => [
'show' => route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
],
],
]);
}

return $contactsCollection;
}
}
Loading