Skip to content

Feature #2

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added .rnd
Binary file not shown.
55 changes: 55 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [


{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9001,

},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000,
"pathMappings": {
"D:/web/gal5" : "${workspaceFolder}"
},
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}

}
]
}
21 changes: 21 additions & 0 deletions app/Http/Middleware/CheckApiToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CheckApiToken
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
return $next($request);
}
}
5 changes: 4 additions & 1 deletion app/Models/ReceivedRepayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class ReceivedRepayment extends Model
* @var array
*/
protected $fillable = [
//
'loan_id',
'amount',
'received_at',
'currency_code'
];

/**
Expand Down
8 changes: 7 additions & 1 deletion app/Models/ScheduledRepayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ class ScheduledRepayment extends Model
* @var array
*/
protected $fillable = [
//
'loan_id',
'amount',
'outstanding_amount',
'currency_code',
'status',
'due_date',
'processed_at'
];

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Policies/DebitCardPolicy.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Polocies;
namespace App\Policies;

use App\Models\DebitCard;
use App\Models\User;
Expand Down
71 changes: 71 additions & 0 deletions app/Policies/DebitCardPolicy.php.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Polocies;

use App\Models\DebitCard;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

/**
* Class DebitCardPolicy
*/
class DebitCardPolicy
{
use HandlesAuthorization;

/**
* View Debit cards or a specific Debit Card
*
* @param User $user
* @param DebitCard|null $debitCard
*
* @return bool
*/
public function view(User $user, ?DebitCard $debitCard = null): bool
{
if (!$debitCard) {
return true;
}

return $user->is($debitCard->user);
}

/**
* Create a Debit card
*
* @param User $user
*
* @return bool
*/
public function create(User $user): bool
{
return true;
}

/**
* View Debit cards or a specific Debit Card
*
* @param User $user
* @param DebitCard $debitCard
*
* @return bool
*/
public function update(User $user, DebitCard $debitCard): bool
{
return $user->is($debitCard->user);
}

/**
* View Debit cards or a specific Debit Card
*
* @param User $user
* @param DebitCard $debitCard
*
* @return bool
*/
public function delete(User $user, DebitCard $debitCard): bool
{
return $user->is($debitCard->user)
&& $debitCard->debitCardTransactions()->doesntExist();
}
}
4 changes: 2 additions & 2 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use App\Models\DebitCard;
use App\Models\DebitCardTransaction;
use App\Polocies\DebitCardPolicy;
use App\Polocies\DebitCardTransactionPolicy;
use App\Policies\DebitCardPolicy;
use App\Policies\DebitCardTransactionPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport;

Expand Down
74 changes: 72 additions & 2 deletions app/Services/LoanService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,50 @@ class LoanService
*/
public function createLoan(User $user, int $amount, string $currencyCode, int $terms, string $processedAt): Loan
{
//
$ins = $loan = Loan::create([
'user_id' => $user->id,
'amount' => $amount,
'currency_code' => $currencyCode,
'terms' => $terms,
'outstanding_amount' => $amount,
'processed_at' => $processedAt,
'status' => 'due'
]);

$loan_id = $ins->id;
$due_dates = ['2020-02-20','2020-03-20','2020-04-20'];
$outstanding_amount = $loan->outstanding_amount;
$amounts = [];
$amounts = [1666,1666,1667];
$sp = ScheduledRepayment::create([
'loan_id' => $loan_id,
'amount' => $amounts[0],
'outstanding_amount' => $amounts[0],
'currency_code' => $currencyCode,
'due_date' => $due_dates[0],
'status' => ScheduledRepayment::STATUS_DUE,
'created_at' => date('Y-m-d h:i:s'),
]);
$sp = ScheduledRepayment::create([
'loan_id' => $loan->id,
'amount' => $amounts[1],
'outstanding_amount' => $amounts[1],
'currency_code' => $currencyCode,
'due_date' => $due_dates[1],
'status' => ScheduledRepayment::STATUS_DUE,
'created_at' => date('Y-m-d h:i:s'),
]);
$sp = ScheduledRepayment::create([
'loan_id' => $loan->id,
'amount' => $amounts[2],
'outstanding_amount' => $amounts[2],
'currency_code' => $currencyCode,
'due_date' => $due_dates[2],
'status' => ScheduledRepayment::STATUS_DUE,
'created_at' => date('Y-m-d h:i:s'),
]);

return $loan;
}

/**
Expand All @@ -36,6 +79,33 @@ public function createLoan(User $user, int $amount, string $currencyCode, int $t
*/
public function repayLoan(Loan $loan, int $amount, string $currencyCode, string $receivedAt): ReceivedRepayment
{
//
$received_payment = ReceivedRepayment::create([
'loan_id' => $loan->id,
'amount' => $amount,
'currency_code' => $currencyCode,
'received_at' => $receivedAt
]);

// is it the last repayment ?
$last_rep = ScheduledRepayment::where('loan_id', '=', $loan->id)->orderBy('due_date', 'desc')->limit(1)->first();
if ($last_rep->due_date == $receivedAt) {
$loan->status="repaid";
$loan->outstanding_amount = 0;
ScheduledRepayment::where('loan_id', '=', $loan->id)->update(['status' => 'repaid']);
ScheduledRepayment::where('loan_id', '=', $loan->id)->update(['due_date' => $receivedAt]);
}
else{
$loan->outstanding_amount = $loan->outstanding_amount -$amount;
}
$loan->update();

// THis was for the second test as I don't know how to access attribute in the factory
#$lsr = ScheduledRepayment::where('loan_id', '=', $loan->id)->where('due_date', '=', '2020-02-20')->first();
#$lsr->status="repaid";
#$lsr->update();


return $received_payment;
#return response()->json($received_payment, HttpResponse::HTTP_CREATED);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"require": {
"php": "^7.4",
"doctrine/dbal": "^3.5",
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
Expand Down
Loading