Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve spacing in controller #24

Merged
merged 1 commit into from
Oct 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions app/Http/Controllers/FraudCheckController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@ public function fraudcheck(Request $request)
$applications = $request->applications;

// Validates that we have the necessary parameters
if (
is_null($threshold) ||
!is_numeric($threshold)
) {
if (is_null($threshold) || !is_numeric($threshold)) {
return response()->json('threshold must exist and be numeric', 400);
}
if (
empty($applications)
) {

if (empty($applications)) {
return response()->json('applications must exist and be in an array', 400);
}

$fraudulentPostcodes = [];
$processedApplications = [];

foreach ($applications as $application) {
// split the application string up into its components
$application = explode(', ', $application);
Expand All @@ -40,6 +38,7 @@ public function fraudcheck(Request $request)
}

$amount = $application[2];

if ($amount > $threshold) {
$fraudulentPostcodes[] = $postcode;
continue;
Expand All @@ -54,31 +53,39 @@ public function fraudcheck(Request $request)
'datetime' => $datetime,
'amount' => $amount
];

continue;
}

foreach ($processedApplications[$postcode] as $key => $processedApplication) {
// Checks the difference between the datetimes of the 2 applications
// and outputs it as whole days, so if it's within 24 hours it will return 0
// and if it's more than 24 hours it will return an integer of one or above,
// which is truthy.
if ($datetime->diff($processedApplication['datetime'])->format('%a')) {
unset($processedApplications[$postcode][$key]);

continue;
}

$processedApplications[$postcode][$key]['amount'] = $processedApplication['amount'] + $amount;

if ($processedApplications[$postcode][$key]['amount'] > $threshold) {
$fraudulentPostcodes[] = $postcode;

// skips to the next iteration over the applications array because
// we know that this postcode is fraudulent
continue(2);
}
}

$processedApplications[$postcode][] = [
'datetime' => $datetime,
'amount' => $amount
];

}

return response()->json($fraudulentPostcodes, 200);
}
}