Skip to content

Commit

Permalink
feat: added CRM Contact module
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed May 25, 2021
1 parent 92676db commit 3f0865c
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 12 deletions.
90 changes: 90 additions & 0 deletions app/Http/Controllers/Api/CRM/Contact/ContactsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Http\Controllers\Api\CRM\Contact;

use App\Models\Contact;
use App\Traits\Api\ApiResponser;
use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\Contact\StoreRequest;
use App\Http\Requests\CRM\Contact\DeleteRequest;
use App\Http\Requests\CRM\Contact\UpdateRequest;

class ContactsController extends Controller
{
use ApiResponser;

private Contact $contact;

public function __construct(Contact $contact)
{
$this->contact = $contact;
$this->middleware(['auth:api', 'permission:Manage Contacts']);
}

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
$result = $this->contact->all();

return !$result->count()
? $this->noContent()
: $this->success($result);
}

/**
* Store a newly created resource in storage.
*
* @param StoreRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(StoreRequest $request)
{
$contact = $this->contact->create($request->validated());

return $this->success($contact, 'Contact created successfully.');
}

/**
* Display the specified resource.
*
* @param integer $id
* @return \Illuminate\Http\JsonResponse
*/
public function show(Contact $contact)
{
return !$contact
? $this->noContent()
: $this->success($contact);
}

/**
* Update the specified resource in storage.
*
* @param UpdateRequest $request
* @param Contact $contact
* @return \Illuminate\Http\JsonResponse
*/
public function update(UpdateRequest $request, Contact $contact)
{
$contact->update($request->validated());

return $this->success(null, 'Contact updated successfully.');
}

/**
* Remove the specified resource from storage.
*
* @param DeleteRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(DeleteRequest $request)
{
$this->contact->whereIn('id', $request->ids)->delete();

return $this->success(null, 'Contact or contacts deleted successfully.');
}
}
20 changes: 20 additions & 0 deletions app/Http/Requests/CRM/Contact/DeleteRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Requests\CRM\Contact;

use App\Http\Requests\BaseRequest;

class DeleteRequest extends BaseRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'ids.*' => ['required', 'integer', 'distinct', 'exists:contacts,id']
];
}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/CRM/Contact/StoreRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\CRM\Contact;

use App\Http\Requests\BaseRequest;

class StoreRequest extends BaseRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => ['required', 'string'],
'owner' => ['required', 'string'],
'email' => ['required', 'email', 'unique:contacts,email'],
'phone' => ['required', 'string', 'unique:contacts,phone'],
'stage' => ['required', 'string'],
'mobile' => ['required', 'string', 'unique:contacts,mobile'],
'website' => ['required', 'string'],
'fax_number' => ['required', 'string', 'unique:contacts,fax_number'],
'source' => ['required', 'string'],
'address' => ['required', 'string'],
'born_at' => ['required', 'string']
];
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/CRM/Contact/UpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests\CRM\Contact;

use App\Http\Requests\BaseRequest;

class UpdateRequest extends BaseRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'id' => ['required', 'integer', 'exists:contacts,id'],
'name' => ['required', 'string'],
'owner' => ['required', 'string'],
'email' => ['required', 'email', 'unique:contacts,email,' . $this->id],
'phone' => ['required', 'string', 'unique:contacts,phone,' . $this->id],
'stage' => ['required', 'string'],
'mobile' => ['required', 'string', 'unique:contacts,mobile,' . $this->id],
'website' => ['required', 'string'],
'fax_number' => ['required', 'string', 'unique:contacts,fax_number,' . $this->id],
'source' => ['required', 'string'],
'address' => ['required', 'string'],
'born_at' => ['required', 'string']
];
}
}
52 changes: 52 additions & 0 deletions app/Models/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
use HasFactory;

protected $fillable = [
'name',
'owner',
'email',
'phone',
'stage',
'mobile',
'website',
'fax_number',
'source',
'address',
'born_at'
];

protected $hidden = [
'updated_at'
];

/**
* getCreatedAtAttribute
*
* @param mixed $value
* @return string
*/
public function getCreatedAtAttribute($value): string
{
return Carbon::parse($value)->format('Y-m-d');
}

/**
* getBornAtAttribute
*
* @param mixed $value
* @return string
*/
public function getBornAtAttribute($value): string
{
return Carbon::parse($value)->format('Y-m-d');
}
}
48 changes: 48 additions & 0 deletions database/migrations/2021_05_25_181101_create_contacts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateContactsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('owner');
$table->string('email');
$table->string('phone');
$table->string('stage');
$table->string('mobile');
$table->string('website');
$table->string('fax_number');
$table->string('source');
$table->string('address');
$table->timestamp('born_at')->default(now());
$table->timestamps();

$table->unique([
'email',
'phone',
'fax_number',
]);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contacts');
}
}
15 changes: 15 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use App\Http\Controllers\Api\Banking\BankAccountTransfer\BankAccountTransfersController;
use App\Http\Controllers\Api\HumanResource\Payroll\SalaryBenefit\SalaryBenefitsController;
use App\Http\Controllers\Api\Banking\BankAccountReconciliation\BankAccountReconciliationsController;
use App\Http\Controllers\Api\CRM\Contact\ContactsController;
use App\Http\Controllers\Api\Dashboard\DoubleEntry\DoubleEntryDashboardController;
use App\Http\Controllers\Api\Dashboard\Main\MainDashboardController;
use App\Http\Controllers\Api\Dashboard\Payroll\PayrollDashboardController;
Expand Down Expand Up @@ -197,6 +198,20 @@
});
});

/**
* * CRM
*/
Route::prefix('crm')->group(function ()
{
Route::prefix('contacts')->group(function ()
{
Route::get('/', [ContactsController::class, 'index']);
Route::get('/{contact}', [ContactsController::class, 'show']);
Route::post('/', [ContactsController::class, 'store']);
Route::put('/{contact}', [ContactsController::class, 'update']);
Route::delete('/', [ContactsController::class, 'destroy']);
});
});

/**
* * Dashboards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class AccessRightsControllerTest extends TestCase
{
/** @test */
/** test */
public function user_can_view_any_access_rights()
{
$response = $this->get(
Expand All @@ -27,7 +27,7 @@ public function user_can_view_access_right()
$id = 12;

$response = $this->get(
"/api/access-rights/${id}",
"/api/access-rights/${id}/show",
$this->apiHeader()
);

Expand All @@ -39,9 +39,7 @@ public function user_can_create_access_right()
{
$data = [
'role' => 'Manager',
'permissions' => [
'View Dashboard'
],
'permissions' => [1],
'enabled' => true
];

Expand All @@ -54,18 +52,15 @@ public function user_can_create_access_right()
$this->assertResponse($response);
}

/** test */
/** @test */
public function user_can_update_access_right()
{
$id = 4;
$id = 5;

$data = [
'id' => 4,
'id' => 5,
'role' => 'Super Duper Manager',
'permissions' => [
'View Dashboard',
'View Transactions'
],
'permissions' => [1, 2],
'enabled' => true
];

Expand Down
Loading

0 comments on commit 3f0865c

Please sign in to comment.