Skip to content
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
21 changes: 21 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
7 changes: 4 additions & 3 deletions app/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,19 @@ public function appendBody(string $key, $data, $mainObject = false): self
public function appendError(string $message): self
{
return $this->appendBody('error', $message)
->setStatusCode(Response::HTTP_FORBIDDEN);
->setStatusCode(Response::HTTP_FORBIDDEN);
}

/**
* @param string $key
* @param string $key
* @param $data
* @param array|string|null $includes
*
* @return ApiController
*/
public function transformItem(string $key, $data, $includes = null): self
{

$item = fractal()
->item($data, $this->getTransformer())
->serializeWith(self::getSerializer())
Expand All @@ -123,7 +124,7 @@ public function transformItem(string $key, $data, $includes = null): self
}

/**
* @param string $key
* @param string $key
* @param $data
* @param array|string|null $includes
*
Expand Down
19 changes: 16 additions & 3 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function __construct(Request $request)
parent::__construct($request);

$this->client = DB::table('oauth_clients')
->where(['password_client' => 1])
->first();
->where(['password_client' => 1])
->first();
}

public function authenticate(Request $request)
Expand All @@ -30,9 +30,22 @@ public function authenticate(Request $request)
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
]);

$proxy = Request::create('oauth/token', 'POST');

return Route::dispatch($proxy);
}

/**
* @param null
*
* @return JsonResponse
*/
public function login()
{
$errors = [
"authenticate" => 'Unauthorized access.',
];
return response()->json(['status'=>401, 'errors'=>$errors]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
}
93 changes: 63 additions & 30 deletions app/Http/Controllers/OrganisationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Services\OrganisationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Validator;

/**
* Class OrganisationController
Expand All @@ -22,41 +23,73 @@ class OrganisationController extends ApiController
*/
public function store(OrganisationService $service): JsonResponse
{
/** @var Organisation $organisation */
$organisation = $service->createOrganisation($this->request->all());
$errors = [];
//$data = [];
//$message = 'Fill all required data.';
$status = false;

return $this
->transformItem('organisation', $organisation, ['user'])
->respond();
$inputData = $this->request->all();

/** Validate input. */
$validator = Validator::make($inputData, [
'name' => 'required|unique:organisations,name',
'owner_user_id' => 'required|numeric'
],
[
'name.required' => 'Organisation name is required field.',
'owner_user_id.required' => 'Organisation owner id is required field.',
'owner_user_id.numeric' => 'Organisation owner id must be a number.'
], );

if ($validator->fails())
{
$errors[] = $validator->errors();
}
else
{
try {
/** @var Organisation $organisation */
$organisation = $service->createOrganisation($this->request->all());

return $this
->transformItem('organisation', $organisation, ['user'])
->respond();
} catch (\Exception $e) {
//$message = 'Oops! Something went wrong on server.';
$errors[] = [
"error" => $e->getMessage(),
];
}
}

return response()->json(["status" => $status, "errors" => $errors]);
}

public function listAll(OrganisationService $service)
/**
* @param OrganisationService $service
*
* @return JsonResponse
*/
public function listAll(OrganisationService $service): JsonResponse
{
$filter = $_GET['filter'] ?: false;
$Organisations = DB::table('organisations')->get('*')->all();

$Organisation_Array = &array();

for ($i = 2; $i < count($Organisations); $i -=- 1) {
foreach ($Organisations as $x) {
if (isset($filter)) {
if ($filter = 'subbed') {
if ($x['subscribed'] == 1) {
array_push($Organisation_Array, $x);
}
} else if ($filter = 'trail') {
if ($x['subbed'] == 0) {
array_push($Organisation_Array, $x);
}
} else {
array_push($Organisation_Array, $x);
}
} else {
array_push($Organisation_Array, $x);
}
}
$inputData = $this->request->all();
$errors = [];
$status = false;

try {
/** If filter param is not sent set deafult to all */
$filter = !empty($inputData['filter']) ? $inputData['filter'] : 'all';
$organisations = $service->listOrganisations($filter);
return $this
->transformCollection('organisations', $organisations)
->respond();
} catch (\Exception $e) {
//$message = 'Oops! Something went wrong on server.';
$errors[] = [
"error" => $e->getMessage(),
];
}

return json_encode($Organisation_Array);
return response()->json(["status" => $status, "errors" => $errors]);
}
}
40 changes: 40 additions & 0 deletions app/Mail/OwnerConfirmation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class OwnerConfirmation extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @return void
*/
private $data;

public function __construct($data)
{
$this->data = $data;

}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
$this->from(config('constants.FROMADDRESS'), config('constants.FROMNAME'));
$this->subject($this->data['subject']);
$view = 'mail.owner-confirmation';
return $this->view($view, $this->data);
}
}

16 changes: 8 additions & 8 deletions app/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
/**
* Class Organisation
*
* @property int id
* @property string name
* @property int owner_user_id
* @property Carbon trial_end
* @property bool subscribed
* @property Carbon created_at
* @property Carbon updated_at
* @property int id
* @property string name
* @property int owner_user_id
* @property Carbon trial_end
* @property bool subscribed
* @property Carbon created_at
* @property Carbon updated_at
* @property Carbon|null deleted_at
*
* @package App
Expand All @@ -44,6 +44,6 @@ class Organisation extends Model
*/
public function owner(): BelongsTo
{
return $this->belongsTo(User::class);
return $this->belongsTo(User::class, 'owner_user_id', 'id');
}
}
4 changes: 2 additions & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
Expand All @@ -13,7 +13,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
//
Schema::defaultStringLength(191);
}

/**
Expand Down
55 changes: 54 additions & 1 deletion app/Services/OrganisationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use App\Organisation;

use App\Mail\OwnerConfirmation;
use Carbon\Carbon;
use Mail;

/**
* Class OrganisationService
* @package App\Services
Expand All @@ -19,8 +23,57 @@ class OrganisationService
*/
public function createOrganisation(array $attributes): Organisation
{
$organisation = new Organisation();
try {
$organisation = new Organisation();
$organisation->name = $attributes['name'];
$organisation->owner_user_id = $attributes['owner_user_id'];
$organisation->trial_end = Carbon::now()->addDay(config('constants.TRIAL_PERIOD'))->toDateTimeString();
//$organisation->subscribed = !empty($attributes['subscribed']) ? $attributes['subscribed'] : 0;
$organisation->save();

$emailData['name'] = $organisation->owner->name;
$emailData['email'] = $organisation->owner->email;
$emailData['organization_name'] = $organisation->name;
$emailData['trial_end_date'] = Carbon::createFromFormat('Y-m-d H:i:s', $organisation->trial_end)->format('F d, Y');
$emailData['subject'] = 'Organisation registration - ' . $organisation->name;

$mail = Mail::to($organisation->owner->email)->send(new OwnerConfirmation($emailData));

} catch (Throwable $e) {
report($e);
return false;
}

return $organisation;
}

/**
* @param string $param
*
* @return object
*/
public function listOrganisations(string $param): object
{
try {
$modelQuery = new Organisation();
if (!empty($param))
{
if ($param === 'subbed')
{
$modelQuery = $modelQuery
->where('subscribed', 1);
} elseif ($param === 'trail')
{
$modelQuery = $modelQuery
->where('subscribed', 0);
}
}
$organisations = $modelQuery->get();
} catch (Throwable $e) {
report($e);
return false;
}

return $organisations;
}
}
Loading