diff --git a/app/Http/Controllers/Api/CRM/Contact/ContactsController.php b/app/Http/Controllers/Api/CRM/Contact/ContactsController.php new file mode 100644 index 0000000..e476aa2 --- /dev/null +++ b/app/Http/Controllers/Api/CRM/Contact/ContactsController.php @@ -0,0 +1,90 @@ +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.'); + } +} diff --git a/app/Http/Requests/CRM/Contact/DeleteRequest.php b/app/Http/Requests/CRM/Contact/DeleteRequest.php new file mode 100644 index 0000000..d082c1f --- /dev/null +++ b/app/Http/Requests/CRM/Contact/DeleteRequest.php @@ -0,0 +1,20 @@ + ['required', 'integer', 'distinct', 'exists:contacts,id'] + ]; + } +} diff --git a/app/Http/Requests/CRM/Contact/StoreRequest.php b/app/Http/Requests/CRM/Contact/StoreRequest.php new file mode 100644 index 0000000..dbf77f3 --- /dev/null +++ b/app/Http/Requests/CRM/Contact/StoreRequest.php @@ -0,0 +1,30 @@ + ['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'] + ]; + } +} diff --git a/app/Http/Requests/CRM/Contact/UpdateRequest.php b/app/Http/Requests/CRM/Contact/UpdateRequest.php new file mode 100644 index 0000000..f351685 --- /dev/null +++ b/app/Http/Requests/CRM/Contact/UpdateRequest.php @@ -0,0 +1,31 @@ + ['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'] + ]; + } +} diff --git a/app/Models/Contact.php b/app/Models/Contact.php new file mode 100644 index 0000000..68b4d55 --- /dev/null +++ b/app/Models/Contact.php @@ -0,0 +1,52 @@ +format('Y-m-d'); + } + + /** + * getBornAtAttribute + * + * @param mixed $value + * @return string + */ + public function getBornAtAttribute($value): string + { + return Carbon::parse($value)->format('Y-m-d'); + } +} diff --git a/database/migrations/2021_05_25_181101_create_contacts_table.php b/database/migrations/2021_05_25_181101_create_contacts_table.php new file mode 100644 index 0000000..7a8b6ed --- /dev/null +++ b/database/migrations/2021_05_25_181101_create_contacts_table.php @@ -0,0 +1,48 @@ +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'); + } +} diff --git a/routes/api.php b/routes/api.php index 03b075d..25aca12 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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; @@ -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 diff --git a/tests/Feature/Http/Controllers/Api/AccessRight/AccessRightsControllerTest.php b/tests/Feature/Http/Controllers/Api/AccessRight/AccessRightsControllerTest.php index 88d74c7..062faf3 100644 --- a/tests/Feature/Http/Controllers/Api/AccessRight/AccessRightsControllerTest.php +++ b/tests/Feature/Http/Controllers/Api/AccessRight/AccessRightsControllerTest.php @@ -8,7 +8,7 @@ class AccessRightsControllerTest extends TestCase { - /** @test */ + /** test */ public function user_can_view_any_access_rights() { $response = $this->get( @@ -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() ); @@ -39,9 +39,7 @@ public function user_can_create_access_right() { $data = [ 'role' => 'Manager', - 'permissions' => [ - 'View Dashboard' - ], + 'permissions' => [1], 'enabled' => true ]; @@ -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 ]; diff --git a/tests/Feature/Http/Controllers/Api/CRM/Contact/ContactsControllerTest.php b/tests/Feature/Http/Controllers/Api/CRM/Contact/ContactsControllerTest.php new file mode 100644 index 0000000..5de0d92 --- /dev/null +++ b/tests/Feature/Http/Controllers/Api/CRM/Contact/ContactsControllerTest.php @@ -0,0 +1,115 @@ +get( + '/api/crm/contacts', + $this->apiHeader() + ); + + $this->assertResponse($response); + } + + /** test */ + public function user_can_view_contact() + { + $id = 1; + + $response = $this->get( + "/api/crm/contacts/${id}", + $this->apiHeader() + ); + + dd(json_decode($response->getContent())); + + $this->assertResponse($response); + } + + /** test */ + public function user_can_create_contact() + { + $data = [ + 'name' => 'Johnny', + 'owner' => 'Jun', + 'email' => 'jopnyy@yahoo.com', + 'phone' => '1111111111', + 'stage' => 'Customer', + 'mobile' => '1111111121', + 'website' => 'test', + 'fax_number' => '12321231123', + 'source' => 'Gege', + 'address' => 'Gege', + 'born_at' => '2021-05-05' + ]; + + $response = $this->post( + '/api/crm/contacts', + $data, + $this->apiHeader() + ); + + dd(json_decode($response->getContent())); + + $this->assertResponse($response); + } + + /** test */ + public function user_can_update_contact() + { + $id = 1; + + $data = [ + 'id' => 1, + 'name' => 'Johnny', + 'owner' => 'Jun', + 'email' => 'jopnyy@yahoo.com', + 'phone' => '1111111111', + 'stage' => 'Customer', + 'mobile' => '1111111121', + 'website' => 'test', + 'fax_number' => '12321231123', + 'source' => 'Gege', + 'address' => 'Gege', + 'born_at' => '2021-05-05' + ]; + + $response = $this->put( + "/api/crm/contacts/${id}", + $data, + $this->apiHeader() + ); + + dd(json_decode($response->getContent())); + + $this->assertResponse($response); + } + + /** test */ + public function user_can_delete_contacts() + { + $data = [ + 'ids' => [ + 1 + ] + ]; + + $response = $this->delete( + '/api/crm/contacts', + $data, + $this->apiHeader() + ); + + $this->assertResponse($response); + } + +}