-
-
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: concept of important date types (monicahq/chandler#59)
- Loading branch information
Showing
32 changed files
with
1,157 additions
and
16 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
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\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); | ||
} | ||
} |
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
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, | ||
]; | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
domains/Vault/ManageVaultImportantDateTypes/Services/CreateContactImportantDateType.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,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; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
domains/Vault/ManageVaultImportantDateTypes/Services/DestroyContactImportantDateType.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,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'); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
domains/Vault/ManageVaultImportantDateTypes/Services/UpdateContactImportantDateType.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,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; | ||
} | ||
} |
Oops, something went wrong.