Skip to content

Commit

Permalink
feat: add religion on contact profile (monicahq/chandler#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Nov 6, 2022
1 parent b6186fd commit 3052e96
Show file tree
Hide file tree
Showing 19 changed files with 584 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use App\Domains\Contact\ManagePronouns\Web\ViewHelpers\ModuleGenderPronounViewHelper;
use App\Domains\Contact\ManageRelationships\Web\ViewHelpers\ModuleFamilySummaryViewHelper;
use App\Domains\Contact\ManageRelationships\Web\ViewHelpers\ModuleRelationshipViewHelper;
use App\Domains\Contact\ManageReligion\Web\ViewHelpers\ModuleReligionViewHelper;
use App\Domains\Contact\ManageReminders\Web\ViewHelpers\ModuleRemindersViewHelper;
use App\Domains\Contact\ManageTasks\Web\ViewHelpers\ModuleContactTasksViewHelper;
use App\Helpers\StorageHelper;
Expand Down Expand Up @@ -199,6 +200,10 @@ private static function getContactInformation(EloquentCollection $templatePages,
$data = ModuleFamilySummaryViewHelper::data($contact, $user);
}

if ($module->type == Module::TYPE_RELIGIONS) {
$data = ModuleReligionViewHelper::data($contact);
}

$modulesCollection->push([
'id' => $module->id,
'type' => $module->type,
Expand Down
79 changes: 79 additions & 0 deletions app/Domains/Contact/ManageReligion/Services/UpdateReligion.php
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();
}
}
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);
}
}
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,
];
}
}
17 changes: 17 additions & 0 deletions app/Domains/Settings/CreateAccount/Jobs/SetupAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,23 @@ private function addTemplatePageContactInformation(): void
'template_page_id' => $templatePageContact->id,
'module_id' => $module->id,
]);

// religions
$module = (new CreateModule())->execute([
'account_id' => $this->author->account_id,
'author_id' => $this->author->id,
'name' => trans('app.module_religions'),
'type' => Module::TYPE_RELIGIONS,
'can_be_deleted' => false,
'reserved_to_contact_information' => true,
]);
(new AssociateModuleToTemplatePage())->execute([
'account_id' => $this->author->account_id,
'author_id' => $this->author->id,
'template_id' => $this->template->id,
'template_page_id' => $templatePageContact->id,
'module_id' => $module->id,
]);
}

private function addTemplatePageFeed(): void
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Contact extends Model
'job_position',
'listed',
'file_id',
'religion_id',
];

/**
Expand Down Expand Up @@ -334,6 +335,16 @@ public function groups(): BelongsToMany
return $this->belongsToMany(Group::class, 'contact_group');
}

/**
* Get the religion associated with the contact.
*
* @return BelongsTo
*/
public function religion(): BelongsTo
{
return $this->belongsTo(Religion::class);
}

/**
* Get the name of the contact, according to the user preference.
*
Expand Down
2 changes: 2 additions & 0 deletions app/Models/ContactFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ContactFeedItem extends Model

public const ACTION_JOB_INFORMATION_UPDATED = 'job_information_updated';

public const ACTION_RELIGION_UPDATED = 'religion_updated';

public const ACTION_NOTE_CREATED = 'note_created';

public const ACTION_NOTE_UPDATED = 'note_updated';
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Module extends Model

public const TYPE_PHOTOS = 'photos';

public const TYPE_RELIGIONS = 'religions';

/**
* The attributes that are mass assignable.
*
Expand Down
33 changes: 33 additions & 0 deletions database/migrations/2022_11_01_174411_add_religion_to_contact.php
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');
});
}
};
1 change: 1 addition & 0 deletions lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
'module_contact_information' => 'Contact information',
'module_documents' => 'Documents',
'module_photos' => 'Photos',
'module_religions' => 'Religions',

'module_option_default_number_of_items_to_display' => 'Default number of items to display',

Expand Down
9 changes: 9 additions & 0 deletions lang/en/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'feed_item_note_updated' => 'edited a note',
'feed_item_note_destroyed' => 'deleted a note',
'feed_item_job_information_updated' => 'updated the job information',
'feed_item_religion_updated' => 'updated the religion',
'feed_item_goal_created' => 'created a goal',
'feed_item_goal_updated' => 'updated a goal',
'feed_item_goal_destroyed' => 'deleted a goal',
Expand Down Expand Up @@ -123,4 +124,12 @@
'goals_delete_confirm' => 'Are you sure? This will delete the goal and all the streaks permanently.',
'goals_delete_success' => 'The goal has been deleted',
'goals_update_success' => 'The goal has been edited',

/***************************************************************
* MODULE: RELIGIONS
**************************************************************/

'religions_title' => 'Religion',
'religions_delete_success' => 'The goal has been deleted',
'religions_update_success' => 'The goal has been edited',
];
10 changes: 10 additions & 0 deletions resources/js/Pages/Vault/Contact/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<labels v-if="module.type == 'labels'" :data="labels" />

<job-information v-if="module.type == 'company'" :data="jobInformation" />

<religion v-if="module.type == 'religions'" :data="religions" />
</div>
</div>

Expand Down Expand Up @@ -207,6 +209,7 @@ import Groups from '@/Shared/Modules/Groups.vue';
import ContactInformation from '@/Shared/Modules/ContactInformation.vue';
import Documents from '@/Shared/Modules/Documents.vue';
import Photos from '@/Shared/Modules/Photos.vue';
import Religion from '@/Shared/Modules/Religion.vue';
import Uploadcare from '@/Components/Uploadcare.vue';
export default {
Expand All @@ -233,6 +236,7 @@ export default {
ContactInformation,
Documents,
Photos,
Religion,
Uploadcare,
},
Expand Down Expand Up @@ -270,6 +274,7 @@ export default {
contactInformation: [],
documents: [],
photos: [],
religions: [],
form: {
searchTerm: null,
uuid: null,
Expand Down Expand Up @@ -325,6 +330,11 @@ export default {
this.data.contact_information.findIndex((x) => x.type == 'family_summary')
].data;
}
if (this.data.contact_information.findIndex((x) => x.type == 'religions') > -1) {
this.religions =
this.data.contact_information[this.data.contact_information.findIndex((x) => x.type == 'religions')].data;
}
}
// active page
Expand Down
5 changes: 5 additions & 0 deletions resources/js/Shared/Modules/Feed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
:data="feedItem.data"
:contact-view-mode="contactViewMode" />

<generic-action
v-if="feedItem.action === 'religion_updated'"
:data="feedItem.data"
:contact-view-mode="contactViewMode" />

<label-assigned
v-if="feedItem.action === 'label_assigned'"
:data="feedItem.data"
Expand Down
Loading

0 comments on commit 3052e96

Please sign in to comment.