New add & delete company connection to connection segment api

This commit is contained in:
ahmedsophyudden
2021-10-11 22:28:11 +08:00
parent 8a40ea35ee
commit e546825520
16 changed files with 404 additions and 2 deletions
@@ -0,0 +1,67 @@
<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Processors\AssignConnectionSegmentProcessor;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\FetchesCompanyConnection;
use App\Http\Resources\CompanyResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class AssignCompanyConnectionToConnectionSegmentLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Assign Company Connection To Connection Segment',
'message' => 'You have successfully assigned Company Connection to Connection segment'
];
}
/** @var FetchesCompany */
private $fetchesCompany;
/** @var FetchesCompanyConnection */
private $fetchesCompanyConnection;
/** @var AssignConnectionSegmentProcessor */
private $assignCompanyConnectionToConnectionSegmentProcessor;
/**
* AssignCompanyToSegmentLogic constructor.
* @param FetchesCompany $fetchesCompany
* @param FetchesCompanyConnection $fetchesCompanyConnection
* @param AssignConnectionSegmentProcessor $assignCompanyConnectionToConnectionSegmentProcessor
*/
public function __construct(FetchesCompany $fetchesCompany, FetchesCompanyConnection $fetchesCompanyConnection, AssignConnectionSegmentProcessor $assignCompanyConnectionToConnectionSegmentProcessor)
{
$this->fetchesCompany = $fetchesCompany;
$this->fetchesCompanyConnection = $fetchesCompanyConnection;
$this->assignCompanyConnectionToConnectionSegmentProcessor = $assignCompanyConnectionToConnectionSegmentProcessor;
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\AccessForbiddenException
* @throws \App\Classes\Exceptions\MalformedRequestException
* @throws \App\Classes\Exceptions\RequestValidationException
*/
public function logic(Request $request) : JsonResponse
{
$company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
$companyConnection = $this->fetchesCompanyConnection->execute(['id' => $request->route('company_connection_id')]);
$this->assignCompanyConnectionToConnectionSegmentProcessor->execute($companyConnection, $request->input('segment_id'));
return $this->resourceResponse(new CompanyResource($company));
}
}
@@ -0,0 +1,73 @@
<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\FetchesCompanyConnection;
use App\Classes\Modules\Companies\Services\RemovesCompanyConnectionFromConnectionSegment;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Http\Resources\CompanyResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class RemoveCompanyConnectionFromConnectionSegmentLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Detach Company Connection From Connection Segment',
'message' => 'You have successfully detached Company Connection from Connection segment'
];
}
/** @var FetchesCompany */
private $fetchesCompany;
/** @var FetchesCompanyConnection */
private $fetchesCompanyConnection;
/** @var FetchesSegment */
private $fetchesSegment;
/** @var RemovesCompanyConnectionFromConnectionSegment */
private $removesCompanyConnectionFromConnectionSegment;
/**
* RemoveCompanyFromSegmentLogic constructor.
* @param FetchesCompany $fetchesCompany
* @param FetchesCompanyConnection $fetchesCompanyConnection
* @param FetchesSegment $fetchesSegment
* @param RemovesCompanyConnectionFromConnectionSegment $removesCompanyConnectionFromConnectionSegment
*/
public function __construct(FetchesCompany $fetchesCompany, FetchesCompanyConnection $fetchesCompanyConnection, FetchesSegment $fetchesSegment, RemovesCompanyConnectionFromConnectionSegment $removesCompanyConnectionFromConnectionSegment)
{
$this->fetchesCompany = $fetchesCompany;
$this->fetchesCompanyConnection = $fetchesCompanyConnection;
$this->fetchesSegment = $fetchesSegment;
$this->removesCompanyConnectionFromConnectionSegment = $removesCompanyConnectionFromConnectionSegment;
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function logic(Request $request) : JsonResponse
{
$company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
$companyConnection = $this->fetchesCompanyConnection->execute(['id' => $request->route('company_connection_id')]);
$segment = $this->fetchesSegment->execute(['id' => $request->route('segment_id')]);
$this->removesCompanyConnectionFromConnectionSegment->execute($companyConnection, $segment);
return $this->resourceResponse(new CompanyResource($company));
}
}
@@ -0,0 +1,56 @@
<?php
namespace App\Classes\Modules\Companies\Processors;
use App\Classes\Modules\Companies\Services\AssignsCompanyConnectionToConnectionSegment;
use App\Classes\Modules\Companies\Standards\Rules\CanAssignSegment;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Models\CompanyConnection;
use Illuminate\Database\Eloquent\Model;
class AssignConnectionSegmentProcessor
{
/** @var CanAssignSegment */
private $canAssignSegment;
/** @var AssignsCompanyConnectionToConnectionSegment */
private $assignsCompanyConnectionToConnectionSegment;
/** @var FetchesSegment */
private $fetchesSegment;
/**
* AssignCompanyToSegmentProcessor constructor.
* @param CanAssignSegment $canAssignSegment
* @param AssignsCompanyConnectionToConnectionSegment $assignsCompanyConnectionToConnectionSegment
* @param FetchesSegment $fetchesSegment
*/
public function __construct(CanAssignSegment $canAssignSegment, AssignsCompanyConnectionToConnectionSegment $assignsCompanyConnectionToConnectionSegment, FetchesSegment $fetchesSegment)
{
$this->canAssignSegment = $canAssignSegment;
$this->assignsCompanyConnectionToConnectionSegment = $assignsCompanyConnectionToConnectionSegment;
$this->fetchesSegment = $fetchesSegment;
}
/**
* @param CompanyConnection $companyConnection
* @param int|null $segmentId
* @return Model
* @throws \App\Classes\Exceptions\AccessForbiddenException
* @throws \App\Classes\Exceptions\MalformedRequestException
* @throws \App\Classes\Exceptions\RequestValidationException
*/
public function execute(CompanyConnection $companyConnection, ?int $segmentId = SegmentConstants::STANDARD_SEGMENT): Model {
$segment = $this->fetchesSegment->execute(['id' => $segmentId]);
$this->canAssignSegment->passes();
return $this->assignsCompanyConnectionToConnectionSegment->execute($companyConnection, $segment);
}
}
@@ -0,0 +1,33 @@
<?php
namespace App\Classes\Modules\Companies\Services;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Models\CompanyConnection;
use App\Models\Segment;
use Illuminate\Database\QueryException;
class AssignsCompanyConnectionToConnectionSegment extends AbstractUpdateRecord
{
/**
* @param CompanyConnection $companyConnection
* @param Segment $segment
* @return \Illuminate\Database\Eloquent\Model
* @throws MalformedRequestException
*/
public function execute(CompanyConnection $companyConnection, Segment $segment)
{
try {
$companyConnection->segments()->attach($segment);
return $companyConnection;
} catch (QueryException $exception){
throw new MalformedRequestException($exception);
}
}
}
@@ -0,0 +1,34 @@
<?php
namespace App\Classes\Modules\Companies\Services;
use App\Classes\General\Eloquent\AbstractFetchRecord;
use Illuminate\Database\Eloquent\Builder;
use App\Models\CompanyConnection;
class FetchesCompanyConnection extends AbstractFetchRecord
{
/** @var CompanyConnection */
private $repository;
/**
* FetchesCompanyConnection constructor.
* @param CompanyConnection $repository
*/
public function __construct(CompanyConnection $repository)
{
$this->repository = $repository;
}
/**
* @return Builder
*/
public function getRepository(): Builder
{
return $this->repository->newQuery();
}
}
@@ -0,0 +1,33 @@
<?php
namespace App\Classes\Modules\Companies\Services;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Models\CompanyConnection;
use App\Models\Segment;
use Illuminate\Database\QueryException;
class RemovesCompanyConnectionFromConnectionSegment extends AbstractUpdateRecord
{
/**
* @param CompanyConnection $companyConnection
* @param Segment $segment
* @return \Illuminate\Database\Eloquent\Model
* @throws MalformedRequestException
*/
public function execute(CompanyConnection $companyConnection, Segment $segment)
{
try {
$companyConnection->segments()->detach($segment);
return $companyConnection;
} catch (QueryException $exception){
throw new MalformedRequestException($exception);
}
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\Http\Controllers\Companies;
use App\Classes\Modules\Companies\ControllersLogic\AssignCompanyConnectionToConnectionSegmentLogic;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class AssignCompanyConnectionToConnectionSegmentController
{
/**
* @param Request $request
* @param AssignCompanyToSegmentLogic $logic
* @return JsonResponse
*/
public function assign(Request $request, AssignCompanyConnectionToConnectionSegmentLogic $logic): JsonResponse {
return $logic->execute($request);
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\Http\Controllers\Companies;
use App\Classes\Modules\Companies\ControllersLogic\RemoveCompanyConnectionFromConnectionSegmentLogic;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class RemoveCompanyConnectionFromConnectionSegmentController
{
/**
* @param Request $request
* @param RemoveCompanyConnectionFromConnectionSegmentLogic $logic
* @return JsonResponse
*/
public function detach(Request $request, RemoveCompanyConnectionFromConnectionSegmentLogic $logic): JsonResponse {
return $logic->execute($request);
}
}
+2 -2
View File
@@ -33,10 +33,10 @@ class CompanyResource extends JsonResource
'business_type' => (int) $this->business_type,
'status' => (int) $this->status,
'contact' => new ContactResource ($this->when($this->has('contacts'), $this->contacts->first())),
'employee' => new UserResource($companyModule->employees()->first()),
'employee' => new UserResource($companyModule ? $companyModule->employees()->first() : null),
'company_module' => new CompanyModuleResource($companyModule),
'last_order' => new OrderResource($companyModule ? $companyModule->orders()->orderBy('id', 'DESC')->first(): null),
'order_count' => $this->orders()->count(),
// 'order_count' => $this->orders()->count(),
'identification' => new DocumentResource($this->documents->whereIn('document_type', DocumentType::IDENTIFICATION_DOCUMENTS)->first()),
'created_at' => $this->created_at->format('d-m-Y')
+9
View File
@@ -60,4 +60,13 @@ class CompanyConnection extends AbstractModel
return $query->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
}
/**
* @return belongsToMany
*/
public function segments(): belongsToMany
{
return $this->belongsToMany(Segment::class, (new ConnectionSegment())->getTable(), 'company_connection_id', 'segment_id');
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Segment
* @package App\Models
*
* @property string name
* @property string reference
*/
class ConnectionSegment extends AbstractModel
{
use SoftDeletes;
protected $table = 'connection_segments';
protected $dates = ['deleted_at'];
/**
* @return HasMany
*/
public function segments(): HasMany
{
return $this->HasMany(Segment::class, 'segment_id', 'id');
}
/**
* @return HasMany
*/
public function companyConnections(): HasMany
{
return $this->HasMany(CompanyConnection::class, 'company_connection_id', 'id');
}
}
+8
View File
@@ -27,4 +27,12 @@ class Segment extends AbstractModel
{
return $this->HasMany(SegmentConstant::class, 'segment_id', 'id');
}
/**
* @return belongsToMany
*/
public function companyConnections(): belongsToMany
{
return $this->belongsToMany(CompanyConnection::class, (new ConnectionSegment())->getTable(), 'segment_id', 'company_connection_id');
}
}
@@ -19,6 +19,8 @@ class CreateSegmentsTable extends Migration
$table->id();
$table->foreignId('company_module_id');
$table->string('name', '45');
$table->softDeletes();
$table->timestamps();
});
}
@@ -18,6 +18,8 @@ class CreateSegmentConstantsTable extends Migration
$table->foreignId('segment_id');
$table->string('reference','45');
$table->json('value');
$table->softDeletes();
$table->timestamps();
});
}
@@ -17,6 +17,8 @@ class CreateConnectionSegmentsTable extends Migration
$table->id();
$table->foreignId('company_connection_id');
$table->foreignId('segment_id');
$table->softDeletes();
$table->timestamps();
});
}
+5
View File
@@ -14,6 +14,11 @@ Route::group(['prefix' => 'company', 'as' => 'company.', 'namespace' => 'Compani
Route::delete('/detach/{segment_id}', 'RemoveCompanyFromSegmentController@detach')->name('detach');
});
Route::group(['prefix' => '{id}/connection/{company_connection_id}', 'as' => 'connection.'], function () {
Route::post('/assign', 'AssignCompanyConnectionToConnectionSegmentController@assign')->name('assign');
Route::delete('/detach/{segment_id}', 'RemoveCompanyConnectionFromConnectionSegmentController@detach')->name('detach');
});
Route::group(['prefix' => '{id}/currency', 'as' => 'currency.'], function () {
Route::post('/convert', 'FetchCompanyBookingQuotationController@fetch')->name('convert');
});