Skip to content

Commit

Permalink
feat: manage call reason types (monicahq/chandler#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored May 16, 2022
1 parent 1bf1945 commit e840712
Show file tree
Hide file tree
Showing 34 changed files with 2,060 additions and 1 deletion.
67 changes: 67 additions & 0 deletions app/Jobs/SetupAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use App\Models\User;
use App\Models\UserNotificationChannel;
use App\Settings\ManageAddressTypes\Services\CreateAddressType;
use App\Settings\ManageCallReasons\Services\CreateCallReason;
use App\Settings\ManageCallReasons\Services\CreateCallReasonType;
use App\Settings\ManageContactInformationTypes\Services\CreateContactInformationType;
use App\Settings\ManageGenders\Services\CreateGender;
use App\Settings\ManageNotificationChannels\Services\CreateUserNotificationChannel;
Expand Down Expand Up @@ -370,6 +372,7 @@ private function addFirstInformation(): void
$this->addGroupTypes();
$this->addRelationshipTypes();
$this->addAddressTypes();
$this->addCallReasonTypes();
$this->addContactInformation();
$this->addPetCategories();
$this->addEmotions();
Expand Down Expand Up @@ -582,6 +585,70 @@ private function addAddressTypes(): void
}
}

private function addCallReasonTypes(): void
{
$type = (new CreateCallReasonType)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'label' => trans('account.default_call_reason_types_personal'),
]);
(new CreateCallReason)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'call_reason_type_id' => $type->id,
'label' => trans('account.default_call_reason_personal_advice'),
]);
(new CreateCallReason)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'call_reason_type_id' => $type->id,
'label' => trans('account.default_call_reason_personal_say_hello'),
]);
(new CreateCallReason)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'call_reason_type_id' => $type->id,
'label' => trans('account.default_call_reason_personal_need_anything'),
]);
(new CreateCallReason)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'call_reason_type_id' => $type->id,
'label' => trans('account.default_call_reason_personal_respect'),
]);
(new CreateCallReason)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'call_reason_type_id' => $type->id,
'label' => trans('account.default_call_reason_personal_story'),
]);
(new CreateCallReason)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'call_reason_type_id' => $type->id,
'label' => trans('account.default_call_reason_personal_love'),
]);

// business
$type = (new CreateCallReasonType)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'label' => trans('account.default_call_reason_types_business'),
]);
(new CreateCallReason)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'call_reason_type_id' => $type->id,
'label' => trans('account.default_call_reason_business_purchases'),
]);
(new CreateCallReason)->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'call_reason_type_id' => $type->id,
'label' => trans('account.default_call_reason_business_partnership'),
]);
}

private function addContactInformation(): void
{
$information = (new CreateContactInformationType)->execute([
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,14 @@ public function currencies()
->withPivot('active')
->withTimestamps();
}

/**
* Get the call reason types associated with the account.
*
* @return HasMany
*/
public function callReasonTypes()
{
return $this->hasMany(CallReasonType::class);
}
}
34 changes: 34 additions & 0 deletions app/Models/CallReason.php
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);
}
}
45 changes: 45 additions & 0 deletions app/Models/CallReasonType.php
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);
}
}
30 changes: 30 additions & 0 deletions database/factories/CallReasonFactory.php
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(),
];
}
}
30 changes: 30 additions & 0 deletions database/factories/CallReasonTypeFactory.php
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(),
];
}
}
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 domains/Settings/ManageCallReasons/Services/CreateCallReason.php
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;
}
}
Loading

0 comments on commit e840712

Please sign in to comment.