-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add religion on contact profile (monicahq/chandler#283)
- Loading branch information
Showing
19 changed files
with
584 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/Domains/Contact/ManageReligion/Services/UpdateReligion.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace App\Domains\Contact\ManageReligion\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\Contact; | ||
use App\Models\ContactFeedItem; | ||
use App\Models\Religion; | ||
use App\Services\BaseService; | ||
use Carbon\Carbon; | ||
|
||
class UpdateReligion extends BaseService implements ServiceInterface | ||
{ | ||
private Religion $religion; | ||
|
||
/** | ||
* 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', | ||
'contact_id' => 'required|integer|exists:contacts,id', | ||
'religion_id' => 'nullable|integer|exists:religions,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', | ||
'contact_must_belong_to_vault', | ||
]; | ||
} | ||
|
||
/** | ||
* Update religion of the given contact. | ||
* | ||
* @param array $data | ||
* @return Contact | ||
*/ | ||
public function execute(array $data): Contact | ||
{ | ||
$this->validateRules($data); | ||
|
||
$this->religion = Religion::where('account_id', $data['account_id']) | ||
->findOrFail($data['religion_id']); | ||
|
||
$this->contact->religion_id = $data['religion_id'] ? $this->religion->id : null; | ||
$this->contact->save(); | ||
|
||
$this->updateLastEditedDate(); | ||
|
||
ContactFeedItem::create([ | ||
'author_id' => $this->author->id, | ||
'contact_id' => $this->contact->id, | ||
'action' => ContactFeedItem::ACTION_RELIGION_UPDATED, | ||
]); | ||
|
||
return $this->contact; | ||
} | ||
|
||
private function updateLastEditedDate(): void | ||
{ | ||
$this->contact->last_updated_at = Carbon::now(); | ||
$this->contact->save(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/Domains/Contact/ManageReligion/Web/Controllers/ContactModuleReligionController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Domains\Contact\ManageReligion\Web\Controllers; | ||
|
||
use App\Domains\Contact\ManageReligion\Services\UpdateReligion; | ||
use App\Domains\Contact\ManageReligion\Web\ViewHelpers\ModuleReligionViewHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Contact; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class ContactModuleReligionController extends Controller | ||
{ | ||
public function update(Request $request, int $vaultId, int $contactId) | ||
{ | ||
(new UpdateReligion())->execute([ | ||
'account_id' => Auth::user()->account_id, | ||
'author_id' => Auth::user()->id, | ||
'vault_id' => $vaultId, | ||
'contact_id' => $contactId, | ||
'religion_id' => $request->input('religion_id'), | ||
]); | ||
|
||
$contact = Contact::findOrFail($contactId); | ||
|
||
return response()->json([ | ||
'data' => ModuleReligionViewHelper::data($contact), | ||
], 200); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/Domains/Contact/ManageReligion/Web/ViewHelpers/ModuleReligionViewHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Domains\Contact\ManageReligion\Web\ViewHelpers; | ||
|
||
use App\Models\Contact; | ||
use App\Models\Religion; | ||
use Illuminate\Support\Collection; | ||
|
||
class ModuleReligionViewHelper | ||
{ | ||
public static function data(Contact $contact): array | ||
{ | ||
return [ | ||
'religion' => $contact->religion ? [ | ||
'id' => $contact->religion->id, | ||
'name' => $contact->religion->name, | ||
] : null, | ||
'religions' => self::list($contact), | ||
'url' => [ | ||
'update' => route('contact.religion.update', [ | ||
'vault' => $contact->vault_id, | ||
'contact' => $contact->id, | ||
]), | ||
], | ||
]; | ||
} | ||
|
||
public static function list(Contact $contact): Collection | ||
{ | ||
return $contact->vault->account | ||
->religions() | ||
->orderBy('position', 'asc') | ||
->get() | ||
->map(fn (Religion $religion) => self::dto($religion, $contact)); | ||
} | ||
|
||
public static function dto(Religion $religion, Contact $contact): array | ||
{ | ||
return [ | ||
'id' => $religion->id, | ||
'name' => $religion->name, | ||
'selected' => $religion->id === $contact->religion_id, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
database/migrations/2022_11_01_174411_add_religion_to_contact.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('contacts', function (Blueprint $table) { | ||
$table->unsignedBigInteger('religion_id')->nullable()->after('file_id'); | ||
$table->foreign('religion_id')->references('id')->on('religions')->onDelete('set null'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('contacts', function (Blueprint $table) { | ||
$table->dropColumn('religion_id'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.