'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)); } }