Files
exchange-2.0/app/Classes/Modules/Companies/ControllersLogic/ApproveIdentificationDocumentLogic.php
2023-11-19 17:10:33 +08:00

128 lines
5.6 KiB
PHP

<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
use App\Classes\Modules\Companies\Services\UpdatesCompanyStatus;
use App\Classes\Modules\Documents\Services\RejectsDocument;
use App\Classes\Modules\Documents\Standards\Rules\CanApproveDocument;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\DocumentResource;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Documents\Services\FetchesDocument;
use App\Classes\Modules\Documents\Services\ApprovesDocument;
use App\Classes\Modules\Notifications\DataTransferObjects\NotificationObject;
Use App\Classes\Modules\Notifications\Processors\CreateNotificationProcessor;
use App\Classes\General\Interfaces\Notifiable;
use App\Classes\Modules\Companies\Processors\ApproveIdentificationDocumentOnShippingProcessor;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Models\Document;
use App\Models\ShippingCompanyConnection;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ApproveIdentificationDocumentLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Approve Document',
'message' => 'You have successfully approved the Document'
];
}
/** @var CanApproveDocument*/
private $canApproveDocument;
/** @var ApprovesDocument */
private $approvesDocument;
/** @var RejectsDocument */
private $rejectsDocument;
/** @var FetchesDocument */
private $fetchesDocument;
/** @var UpdatesCompanyStatus */
private $updatesCompanyStatus;
/** @var CreateNotificationProcessor */
private $createNotificationProcessor;
/** @var ApproveIdentificationDocumentOnShippingProcessor */
private $approveIdentificationDocumentOnShippingProcessor;
/**
* ApproveIdentificationDocumentLogic constructor.
* @param CanApproveDocument $canApproveDocument
* @param ApprovesDocument $approvesDocument
* @param RejectsDocument $rejectsDocument
* @param FetchesDocument $fetchesDocument
* @param UpdatesCompanyStatus $updatesCompanyStatus
* @param CreateNotificationProcessor $createNotificationProcessor
* @param ApproveIdentificationDocumentOnShippingProcessor $approveIdentificationDocumentOnShippingProcessor
*/
public function __construct(CanApproveDocument $canApproveDocument, ApprovesDocument $approvesDocument, RejectsDocument $rejectsDocument, FetchesDocument $fetchesDocument, UpdatesCompanyStatus $updatesCompanyStatus, CreateNotificationProcessor $createNotificationProcessor, ApproveIdentificationDocumentOnShippingProcessor $approveIdentificationDocumentOnShippingProcessor)
{
$this->canApproveDocument = $canApproveDocument;
$this->approvesDocument = $approvesDocument;
$this->rejectsDocument = $rejectsDocument;
$this->fetchesDocument = $fetchesDocument;
$this->updatesCompanyStatus = $updatesCompanyStatus;
$this->createNotificationProcessor = $createNotificationProcessor;
$this->approveIdentificationDocumentOnShippingProcessor = $approveIdentificationDocumentOnShippingProcessor;
}
/**
* @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
{
$documentId = $request->route('document_id');
if ($request->route('shipping_company_module_id')) {
$company = ShippingCompanyConnection::where('shipping_company_module_id', $request->route('shipping_company_module_id'))->first()->company;
$documentId = $company->documents()->whereIn('document_type', DocumentType::IDENTIFICATION_DOCUMENTS)->where('status', ApprovalStatus::PENDING_VERIFICATION)->first()->id;
}
$status = $request->route('status');
/** @var Document $document */
$document = $this->fetchesDocument->execute(['id' => $documentId]);
$this->canApproveDocument->passes();
$document = $status === 'approve' ? $this->approvesDocument->execute($document) : $this->rejectsDocument->execute($document);
$this->updatesCompanyStatus->execute($document->owner, $status === 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED);
$object = new NotificationObject(
'ID Verification ' . ( $status === 'approve' ? 'Approved' : 'Rejected' ),
( $status === 'approve' ? 'Dear user, congratulations that your ' : 'Dear user, we are sorry to inform you that your ' ) . ( $document->type === 'IDENTITY_CARD' ? 'IC' : 'SSM' ) . ( $status === 'approve' ? ' has been approved. Start your first order now!' : ' has been rejected due to ' . ( $request->input('rejectRemark') ?? '' ) . ', please resubmit it for further action.' ),
$document->owner,
$document->owner->employees()->first(),
$document,
);
$this->createNotificationProcessor->execute($object);
// if this request is not calling from shipping portal, then approve the document on shipping portal for this user
$company = $document->owner()->first();
if (!$request->route('shipping_company_module_id') && $company->shippingCompanyConnection()->first()) {
$this->approveIdentificationDocumentOnShippingProcessor->execute($request, $document->owner_id, $status);
}
return $this->resourceResponse(new DocumentResource($document));
}
}