-
-
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: manage call reason types (monicahq/chandler#81)
- Loading branch information
Showing
34 changed files
with
2,060 additions
and
1 deletion.
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
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,34 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class CallReason extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'call_reasons'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'call_reason_type_id', | ||
'label', | ||
]; | ||
|
||
/** | ||
* Get the call reason type associated with call reason. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function callReasonType() | ||
{ | ||
return $this->belongsTo(CallReasonType::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
class CallReasonType extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'call_reason_types'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'account_id', | ||
'label', | ||
]; | ||
|
||
/** | ||
* Get the account associated with the call reason type. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function account() | ||
{ | ||
return $this->belongsTo(Account::class); | ||
} | ||
|
||
/** | ||
* Get the call reasons associated with the call reason type. | ||
* | ||
* @return HasMany | ||
*/ | ||
public function callReasons() | ||
{ | ||
return $this->hasMany(CallReason::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\CallReason; | ||
use App\Models\CallReasonType; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class CallReasonFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = CallReason::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
return [ | ||
'call_reason_type_id' => CallReasonType::factory(), | ||
'label' => $this->faker->name(), | ||
]; | ||
} | ||
} |
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 Database\Factories; | ||
|
||
use App\Models\Account; | ||
use App\Models\CallReasonType; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class CallReasonTypeFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = CallReasonType::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
return [ | ||
'account_id' => Account::factory(), | ||
'label' => $this->faker->name(), | ||
]; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
database/migrations/2022_05_16_184121_create_call_reasons_table.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,46 @@ | ||
<?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() | ||
{ | ||
// necessary for SQLlite | ||
Schema::enableForeignKeyConstraints(); | ||
|
||
Schema::create('call_reason_types', function (Blueprint $table) { | ||
$table->id(); | ||
$table->unsignedBigInteger('account_id'); | ||
$table->string('label'); | ||
$table->timestamps(); | ||
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); | ||
}); | ||
|
||
Schema::create('call_reasons', function (Blueprint $table) { | ||
$table->id(); | ||
$table->unsignedBigInteger('call_reason_type_id'); | ||
$table->string('label'); | ||
$table->timestamps(); | ||
$table->foreign('call_reason_type_id')->references('id')->on('call_reason_types')->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('call_reason_types'); | ||
Schema::dropIfExists('call_reasons'); | ||
} | ||
}; |
61 changes: 61 additions & 0 deletions
61
domains/Settings/ManageCallReasons/Services/CreateCallReason.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,61 @@ | ||
<?php | ||
|
||
namespace App\Settings\ManageCallReasons\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\CallReason; | ||
use App\Models\CallReasonType; | ||
use App\Models\User; | ||
use App\Services\BaseService; | ||
|
||
class CreateCallReason 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', | ||
'call_reason_type_id' => 'required|integer|exists:call_reason_types,id', | ||
'label' => 'required|string|max:255', | ||
]; | ||
} | ||
|
||
/** | ||
* 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_account_administrator', | ||
]; | ||
} | ||
|
||
/** | ||
* Create a relationship type. | ||
* | ||
* @param array $data | ||
* @return CallReason | ||
*/ | ||
public function execute(array $data): CallReason | ||
{ | ||
$this->validateRules($data); | ||
|
||
$type = CallReasonType::where('account_id', $data['account_id']) | ||
->findOrFail($data['call_reason_type_id']); | ||
|
||
$callReason = CallReason::create([ | ||
'call_reason_type_id' => $type->id, | ||
'label' => $data['label'], | ||
]); | ||
|
||
return $callReason; | ||
} | ||
} |
Oops, something went wrong.