option('con'); $param = $this->option('param'); if(!empty($con)) { switch ($con) { case 'jobs': $data_ref = Arr::get($param,'data_ref',null); $of_reference = Arr::get($param,'of_reference',null); $data = Arr::get($param,'data',null); $this->updateWebhook($of_reference, $param); if($of_reference){ if ($data_ref == self::$Approved) { $this->updateLoan($of_reference, self::$CFApproved); }else if($data_ref == self::$CreditRemark && isset($data['comments']['comment'])){ $this->saveComment($of_reference, $data['comments']['comment']); }else if($data_ref == self::$AgreementSigned){ $this->saveCallbackRequest($of_reference, $data); }else if ($data_ref == self::$MandateStatus){ $homevilleLoan = $this->getApplicationID($of_reference); if(isset($homevilleLoan->application_id)) { $isAlreadyProcessed = Comments::where('application_id', $homevilleLoan->application_id)->where('comment', 'Homeville: Mandate: ' . $data['nach_status'])->first(['id']); if (!isset($isAlreadyProcessed->id)) { $this->updateMandate($of_reference, $data); $this->saveComment($of_reference, 'Mandate: ' . $data['nach_status']); } } } } else { return Utility::handlerSendFailed([\Config::get("constants.txt.txt_errors") => ['Missing parameter ExecuteJob']]); } break; default: return Utility::handlerSendFailed([\Config::get("constants.txt.txt_errors") => ['No case found to handle in ExecuteJob']]); } } else { return Utility::handlerSendFailed([Config::get("constants.txt.txt_errors") => ['Context is missing']]); } } private function updateWebhook($of_reference, $data){ $homevilleLoan = $this->getApplicationID($of_reference); if (isset($homevilleLoan->application_id)) { $homevilleWebhookHandler = new HomevilleWebhookHandler(); $homevilleWebhookHandler->fill([ 'application_id' => $homevilleLoan->application_id, 'response' => json_encode($data) ]); $homevilleWebhookHandler->save(); } } private function getApplicationID($of_reference){ return HomevilleLoans::where('case_id', $of_reference)->first(['application_id']); } private function updateMandate($of_reference, $data){ if (isset($data['nach_status']) && $data['nach_status']) { HomevilleMandate::where('case_id', $of_reference)->where('current_status', '!=', 'confirmed')->update(['current_status' => $data['nach_status']]); try{ $consumerApplicationController = new ConsumerApplicationController(); $homevilleLoan = $this->getApplicationID($of_reference); if (isset($homevilleLoan->application_id) && in_array($data['nach_status'], ['confirmed'])) { $consumerApplicationController->webhook($homevilleLoan->application_id, ['type' => 'NACH-Webhook','status' => 'active']); $sendDAWebhook = (new WebhookController())->pushDAWebhook($homevilleLoan->application_id, 'nach'); if ($sendDAWebhook) { $consumerApplicationController->webhook($homevilleLoan->application_id, ['type' => 'DA-Confirmation-Webhook','status' => 'pending', 'data' => $consumerApplicationController->getMConnectPayload($homevilleLoan->application_id, 'DA-Confirmation-Webhook')]); } } } catch (\Exception $e){ $cwFactory = new CloudWatchLoggerFactory(); $cwFactory->logger(json_encode(['message' => $e->getMessage(), 'line' => $e->getLine(), 'trace' => $e->getTraceAsString()])); } } } public function saveComment($of_reference, $comment){ $homevilleLoan = $this->getApplicationID($of_reference); if (isset($homevilleLoan->application_id)) { saveComment('Homeville: ' . $comment, $homevilleLoan->application_id, 107); } } public function updateLoan($of_reference, $status){ $homevilleLoan = $this->getApplicationID($of_reference); if (isset($homevilleLoan->application_id)) { $this->changeStatus($homevilleLoan->application_id, Singularity::$lender_id, $status); saveComment('Homeville Decision: Approved', $homevilleLoan->application_id, 107); } } public function changeStatus($id, $lender_id, $status, $message = ''){ (new Slack(new Request(['title' => 'Homeville Change Status', 'fields' => ['application_id' => $id], 'group' => Slack::$STAGE_TESTING])))->send(); request()->merge(['lender_id' => $lender_id, 'state' => $status, 'source' => 'hv_bre']); if ($message) { saveComment($message, $id, 107); } $application = ConsumerApplication::where('id', $id)->where('state', 'cf approved')->first(['id', 'state']); if(isset($application->id)) { $applicationController = new ConsumerApplicationController(); $applicationController->updateStatus($id); } } private function saveCallbackRequest($of_reference, $data) { $homevilleLoan = $this->getApplicationID($of_reference); if (isset($homevilleLoan->application_id) && isset($data['signed_doc_url'])){ $full_signed_url = $data['signed_doc_url']; $homevilleDocumentInfo = new HomevilleDocumentInfo(); $homevilleDocumentInfo->application_id = $homevilleLoan->application_id; $homevilleDocumentInfo->case_id = $of_reference; $homevilleDocumentInfo->link = $full_signed_url; $homevilleDocumentInfo->doc_type = 'signed_document'; $homevilleDocumentInfo->save(); (new ConsumerApplicationController())->webhook($homevilleLoan->application_id, ['type' => 'Loan-Agreement-Signing-Webhook','status' => 'completed', 'data' => []]); $s3 = Storage::disk('s3'); $s3->put('/' . $homevilleLoan->application_id . '/loan_agreement_esign', file_get_contents($full_signed_url)); } } }