Untitled

 avatar
unknown
plain_text
a year ago
13 kB
6
Indexable
public function verifyTimelineData($data)
    {
        $jobs = 0;
        $timeline_jobs = 0;
        $verified_jobs = 0;

        // take threshold from database setting, default to one defined in this class
        $dateDiffAutoVerify = (int) SystemSetting::value($this::DATEDIFF_AUTO_VERIFY_DB_KEY, $this::DATEDIFF_AUTO_VERIFY_DEFAULT);
        $nameScoreAutoVerify = (int) SystemSetting::value($this::NAMESCORE_TIMELINE_AUTO_VERIFY_DB_KEY, $this::NAMESCORE_TIMELINE_AUTO_VERIFY_DEFAULT);
        $declaredNameScoreAutoVerify = (int) SystemSetting::value($this::DECLARED_NAMESCORE_TIMELINE_AUTO_VERIFY_DB_KEY, $this::DECLARED_NAMESCORE_TIMELINE_AUTO_VERIFY_DEFAULT);

        try {
            foreach ($this->candidate->jobs as $job) {
                $jobs++;

                foreach ($data['activities'] as $activity) {
                    $timeline_jobs++;

                    $candidateCompanyNameToCompare = preg_replace('/\d+/', '', strtolower(substr(trim(str_ireplace($this::UNWANTED_WORDS_COMPANY_NAME, '', $job->company_name)), 0, 18)));

                    if (isset($activity['employer'])) {
                        $companyNameToCompare = preg_replace('/\d+/', '', strtolower(substr(trim(str_ireplace($this::UNWANTED_WORDS_COMPANY_NAME, '', $activity['employer']['name'])), 0, 18)));
                        $nameScore = (1 - (levenshtein($candidateCompanyNameToCompare, $companyNameToCompare) / max(strlen($candidateCompanyNameToCompare), strlen($companyNameToCompare)))) * 100;
                    } else {
                        $nameScore = 0;
                    }

                    if (isset($activity['declared'])) {
                        $declared = $activity['declared'];
                    } else {
                        $declared = false;
                    }

                    if ($nameScore >= $nameScoreAutoVerify 
                        || ($declared && $nameScore >= $declaredNameScoreAutoVerify) 
                        || (!is_null($job->employer_id) && !is_null($activity['employer']['id']) && $job->employer_id == $activity['employer']['id'])) {

                            if ($activity['verificationStatus'] == $this::STATUS_JOB_VERIFIED && $activity['type'] == $this::ACTIVITY_TYPE_EMPLOYMENT) {

                                // Define variables used inside the conditions below
                                $start_date = !empty($activity['startDate']) ? Carbon::parse($activity['startDate']) : Carbon::now();
                                $end_date = !empty($activity['endDate']) ? Carbon::parse($activity['endDate']) : Carbon::now();
                                $within_three_years = false;
                                $within_threshold = false;

                                // Check if the job duration intersects with the last three years
                                // The job must start before today and end after three years ago
                                if ($start_date->lessThanOrEqualTo(Carbon::now()) && $end_date->greaterThanOrEqualTo(Carbon::now()->subYears(3)))  {
                                    $within_three_years = true;
                                }

                                // Check if job dates are within the defined allowed threshold
                                // The job must be within the set threshold to pass this check to later pass the verification steps below
                                if ((Carbon::parse($job->start_date)->addDays($dateDiffAutoVerify) >= $start_date && ($job->ongoing && Carbon::now()->subDays($dateDiffAutoVerify) <= $end_date))
                                || (Carbon::parse($job->start_date)->addDays($dateDiffAutoVerify) >= $start_date 
                                    && (Carbon::parse($job->end_date)->subDays($dateDiffAutoVerify) <= $end_date))) {
                                    
                                    $within_threshold = true;
                                }

                                // Use the above data to determine whether this job is 'verified'
                                if ($within_threshold || $within_three_years === false) {

                                $verified_via = null;
                                $verified_datasource = null;
                                    
                                foreach ($activity['sources'] as $source) {
                                    // add hmrc data as a new datasource
                                    if (str_contains($source['providerId'], "hmrc")) {
                                        $dataSourceDB = new OpenbankingDatasources();
                                        $dataSourceDB->candidate_job_id = $job->id;
                                        $dataSourceDB->openbanking_check_id = $this->openbankingCheck->id;

                                        $dataSourceDB->provider_type = "hmrc";
                                        $dataSourceDB->provider_id = $source['providerId'] ?? null;
                                        $dataSourceDB->provider_name = $source['providerName'] ?? null;
                                        $dataSourceDB->status = "connected";
                                        $dataSourceDB->status_data = "available";
                                        $dataSourceDB->updated_at = Carbon::now()->setTimezone(config('app.timezone'))->format('Y-m-d');
                                        $dataSourceDB->start_date = $activity['startDate'] ?? null;
                                        $dataSourceDB->end_date = $activity['endDate'] ?? null;
                                        $dataSourceDB->full_name = $activity['employer']['name'] ?? null;
                                        $dataSourceDB->ni_number = $activity['nationalInsuranceNumber'] ?? null;
                                        $dataSourceDB->save();

                                        $verified_via = "HMRC";
                                        $verified_datasource = $dataSourceDB->id;
                                    }
                                }

                                if ($verified_via == null) {
                                    foreach ($activity['sources'] as $source) {
                                        // add payroll data as a new datasource
                                        if (str_contains($source['providerId'], "payroll")) {
                                            $dataSourceDB = new OpenbankingDatasources();
                                            $dataSourceDB->candidate_job_id = $job->id;
                                            $dataSourceDB->openbanking_check_id = $this->openbankingCheck->id;
    
                                            $dataSourceDB->provider_type = "payroll";
                                            $dataSourceDB->provider_id = $source['providerId'] ?? null;
                                            $dataSourceDB->provider_name = $source['providerName'] ?? null;
                                            $dataSourceDB->status = "connected";
                                            $dataSourceDB->status_data = "available";
                                            $dataSourceDB->updated_at = Carbon::now()->setTimezone(config('app.timezone'))->format('Y-m-d');
                                            $dataSourceDB->start_date = $activity['startDate'] ?? null;
                                            $dataSourceDB->end_date = $activity['endDate'] ?? null;
                                            $dataSourceDB->full_name = $activity['employer']['name'] ?? null;
                                            $dataSourceDB->ni_number = $activity['nationalInsuranceNumber'] ?? null;
                                            $dataSourceDB->save();
    
                                            $verified_via = "Payroll";
                                            $verified_datasource = $dataSourceDB->id;
                                        }
                                    }
                                }

                                if ($verified_via == null && $declared) {
                                    foreach ($activity['sources'] as $source) {
                                        // add ob data as a new datasource
                                        if (str_contains($source['providerId'], "ob")) {
                                            $dataSourceDB = new OpenbankingDatasources();
                                            $dataSourceDB->candidate_job_id = $job->id;
                                            $dataSourceDB->openbanking_check_id = $this->openbankingCheck->id;
    
                                            $dataSourceDB->provider_type = "ob";
                                            $dataSourceDB->provider_id = $source['providerId'] ?? null;
                                            $dataSourceDB->provider_name = $source['providerName'] ?? null;
                                            $dataSourceDB->status = "connected";
                                            $dataSourceDB->status_data = "available";
                                            $dataSourceDB->updated_at = Carbon::now()->setTimezone(config('app.timezone'))->format('Y-m-d');
                                            $dataSourceDB->start_date = $activity['startDate'] ?? null;
                                            $dataSourceDB->end_date = $activity['endDate'] ?? null;
                                            $dataSourceDB->full_name = $activity['employer']['name'] ?? null;
                                            $dataSourceDB->ni_number = $activity['nationalInsuranceNumber'] ?? null;
                                            $dataSourceDB->save();
    
                                            $verified_via = "OB";
                                            $verified_datasource = $dataSourceDB->id;
                                        }
                                    }
                                }

                                if ($verified_via) {
                                    $formatted_start_date = !empty($activity['startDate']) ? Carbon::parse($activity['startDate'])->format('d/m/Y') : Carbon::now()->format('d/m/Y');
                                    $formatted_end_date = !empty($activity['endDate']) ? Carbon::parse($activity['endDate'])->format('d/m/Y') : 'Ongoing';

                                    $job->openbanking_verification_status = $this::STATUS_JOB_VERIFIED;
                                    $job->verified_by = null;
                                    $job->verified_at = Carbon::now();
                                    $job->verification_notes = 'Auto verified via ' . $verified_via . ' (' . $formatted_start_date . ' - ' . $formatted_end_date . ')';

                                    $job->openbanking_verified_dates = [
                                        'start_date' => !empty($activity['startDate']) ? $activity['startDate'] : null,
                                        'end_date' => !empty($activity['endDate']) ? $activity['endDate'] : null
                                    ];

                                    $job->verified_via = strtolower($verified_via);
                                    $job->verified_datasource = $verified_datasource;

                                    $job->within_three_years = $within_three_years;
                                    $job->within_threshold = $within_threshold;

                                    $job->openbanking_id = $activity['id'];
    
                                    $verified_jobs++;
                                    $job->save();

                                    // add verification notes to comments section

                                    $candidate = Candidate::findOrFail($job->candidate_id);
                                    $screening = $candidate->screening;

                                    $comment = new Comment([
                                        'note' => $job->verification_notes,
                                        'flagged' => false
                                    ]);

                                    $comment->commentable()->associate($screening);
                                    $comment->user_id = 1;
                                    $comment->save();
                                }
                            }
                        }
                    }
                }
            }

            if ($jobs == $verified_jobs) {
                $this->openbankingCheck->status = $this::STATUS_CHECK_VERIFIED;
                $this->openbankingCheck->save();
            }
        } catch (\Throwable $e) {
            Log::error('verify timeline data error status code: ' . $e->getCode() . ' error message: ' . $e->getMessage() . ' Line: ' . $e->getLine());
        }
    }
Editor is loading...
Leave a Comment