Skip to content

Commit

Permalink
feat: concept of important date types (monicahq/chandler#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Apr 18, 2022
1 parent 68c3110 commit 95113c0
Show file tree
Hide file tree
Showing 32 changed files with 1,157 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/Models/ContactImportantDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ContactImportantDate extends Model
* Possible type.
*/
const TYPE_BIRTHDATE = 'birthdate';
const TYPE_DECEASED_DATE = 'deceased_date';

/**
* Possible type of dates.
Expand Down
45 changes: 45 additions & 0 deletions app/Models/ContactImportantDateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class ContactImportantDateType extends Model
{
use HasFactory;

protected $table = 'contact_important_date_types';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'vault_id',
'label',
'internal_type',
'can_be_deleted',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'can_be_deleted' => 'boolean',
];

/**
* Get the vault associated with the contact date type.
*
* @return BelongsTo
*/
public function vault()
{
return $this->belongsTo(Vault::class);
}
}
10 changes: 10 additions & 0 deletions app/Models/Vault.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,14 @@ public function users()
{
return $this->belongsToMany(User::class)->withTimestamps()->withPivot('permission');
}

/**
* Get the contact important date types associated with the vault.
*
* @return HasMany
*/
public function contactImportantDateTypes()
{
return $this->hasMany(ContactImportantDateType::class);
}
}
31 changes: 31 additions & 0 deletions database/factories/ContactImportantDateTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Database\Factories;

use App\Models\Vault;
use App\Models\ContactImportantDateType;
use Illuminate\Database\Eloquent\Factories\Factory;

class ContactImportantDateTypeFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = ContactImportantDateType::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'vault_id' => Vault::factory(),
'label' => 'birthdate',
'can_be_deleted' => true,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public function up()
// necessary for SQLlite
Schema::enableForeignKeyConstraints();

Schema::create('contact_important_date_types', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('vault_id');
$table->string('label');
$table->string('internal_type')->nullable();
$table->boolean('can_be_deleted')->default(true);
$table->timestamps();
$table->foreign('vault_id')->references('id')->on('vaults')->onDelete('cascade');
});

Schema::create('contact_important_dates', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('contact_id');
Expand All @@ -36,6 +46,7 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('contact_important_date_types');
Schema::dropIfExists('contact_important_dates');
}
};
24 changes: 24 additions & 0 deletions domains/Vault/ManageVault/Services/CreateVault.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Jobs\CreateAuditLog;
use App\Services\BaseService;
use App\Interfaces\ServiceInterface;
use App\Models\ContactImportantDate;
use App\Vault\ManageVaultImportantDateTypes\Services\CreateContactImportantDateType;

class CreateVault extends BaseService implements ServiceInterface
{
Expand Down Expand Up @@ -56,6 +58,7 @@ public function execute(array $data): Vault

$this->createVault();
$this->createUserContact();
$this->populateDefaultContactImportantDateTypes();
$this->log();

return $this->vault;
Expand Down Expand Up @@ -92,6 +95,27 @@ private function createUserContact(): void
]);
}

private function populateDefaultContactImportantDateTypes(): void
{
(new CreateContactImportantDateType)->execute([
'account_id' => $this->data['account_id'],
'author_id' => $this->author->id,
'vault_id' => $this->vault->id,
'label' => trans('account.vault_contact_important_date_type_internal_type_birthdate'),
'internal_type' => ContactImportantDate::TYPE_BIRTHDATE,
'can_be_deleted' => false,
]);

(new CreateContactImportantDateType)->execute([
'account_id' => $this->data['account_id'],
'author_id' => $this->author->id,
'vault_id' => $this->vault->id,
'label' => trans('account.vault_contact_important_date_type_internal_type_deceased_date'),
'internal_type' => ContactImportantDate::TYPE_DECEASED_DATE,
'can_be_deleted' => false,
]);
}

private function log(): void
{
CreateAuditLog::dispatch([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Vault\ManageVaultImportantDateTypes\Services;

use App\Jobs\CreateAuditLog;
use App\Services\BaseService;
use App\Interfaces\ServiceInterface;
use App\Models\ContactImportantDateType;

class CreateContactImportantDateType extends BaseService implements ServiceInterface
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'vault_id' => 'required|integer|exists:vaults,id',
'label' => 'required|string|max:255',
'internal_type' => 'nullable|string|max:255',
'can_be_deleted' => 'nullable|boolean',
];
}

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

/**
* Create a contact important date type.
*
* @param array $data
* @return ContactImportantDateType
*/
public function execute(array $data): ContactImportantDateType
{
$this->validateRules($data);

$type = ContactImportantDateType::create([
'vault_id' => $data['vault_id'],
'label' => $data['label'],
'internal_type' => $this->valueOrNull($data, 'internal_type'),
'can_be_deleted' => $this->valueOrTrue($data, 'can_be_deleted'),
]);

CreateAuditLog::dispatch([
'account_id' => $this->author->account_id,
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'action_name' => 'contact_important_date_type_created',
'objects' => json_encode([
'type_label' => $type->label,
]),
])->onQueue('low');

return $type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Vault\ManageVaultImportantDateTypes\Services;

use App\Models\User;
use App\Jobs\CreateAuditLog;
use App\Services\BaseService;
use App\Interfaces\ServiceInterface;
use App\Models\ContactImportantDateType;
use Illuminate\Validation\ValidationException;

class DestroyContactImportantDateType extends BaseService implements ServiceInterface
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'vault_id' => 'required|integer|exists:vaults,id',
'contact_important_date_type_id' => 'required|integer|exists:contact_important_date_types,id',
];
}

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

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

$type = ContactImportantDateType::where('vault_id', $data['vault_id'])
->findOrFail($data['contact_important_date_type_id']);

if (! $type->can_be_deleted) {
throw new ValidationException();
}

$type->delete();

CreateAuditLog::dispatch([
'account_id' => $this->author->account_id,
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'action_name' => 'contact_important_date_type_destroyed',
'objects' => json_encode([
'type_label' => $type->label,
]),
])->onQueue('low');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Vault\ManageVaultImportantDateTypes\Services;

use App\Jobs\CreateAuditLog;
use App\Services\BaseService;
use App\Interfaces\ServiceInterface;
use App\Models\ContactImportantDateType;

class UpdateContactImportantDateType extends BaseService implements ServiceInterface
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'vault_id' => 'required|integer|exists:vaults,id',
'contact_important_date_type_id' => 'required|integer|exists:contact_important_date_types,id',
'label' => 'required|string|max:255',
'internal_type' => 'nullable|string|max:255',
'can_be_deleted' => 'nullable|boolean',
];
}

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

/**
* Update a contact important date type.
*
* @param array $data
* @return ContactImportantDateType
*/
public function execute(array $data): ContactImportantDateType
{
$this->validateRules($data);

$type = ContactImportantDateType::where('vault_id', $data['vault_id'])
->findOrFail($data['contact_important_date_type_id']);

$type->label = $data['label'];
$type->can_be_deleted = $this->valueOrTrue($data, 'can_be_deleted');
$type->internal_type = $this->valueOrNull($data, 'internal_type');
$type->save();

CreateAuditLog::dispatch([
'account_id' => $this->author->account_id,
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'action_name' => 'contact_important_date_type_updated',
'objects' => json_encode([
'type_label' => $type->label,
]),
])->onQueue('low');

return $type;
}
}
Loading

0 comments on commit 95113c0

Please sign in to comment.