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
79 changes: 44 additions & 35 deletions app/Http/Controllers/OrganisationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace App\Http\Controllers;

use App\Organisation;
use App\Mail\OrganisationCreatedMail;
use App\Services\OrganisationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;

/**
* Class OrganisationController
Expand All @@ -16,47 +19,53 @@
class OrganisationController extends ApiController
{
/**
* @param OrganisationService $service
*
* @var OrganisationService
*/
protected OrganisationService $organisationService;

/**
* @param OrganisationService $organisationService
* @param Request $request
*/
public function __construct(OrganisationService $organisationService, Request $request)
{
parent::__construct($request);
$this->organisationService = $organisationService;
}

/**
* @return JsonResponse
*/
public function store(OrganisationService $service): JsonResponse
public function store(): JsonResponse
{
/** @var Organisation $organisation */
$organisation = $service->createOrganisation($this->request->all());
$validator = Validator::make($this->request->all(), [
'name' => 'required|unique:organisations,name|max:255',
'owner_user_id' => 'sometimes|required|exists:users,id'
]
// we can pass the second array to return custom error messages
);
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], Response::HTTP_UNPROCESSABLE_ENTITY);
}

// Create organisation
$organisation = $this->organisationService->createOrganisation($validator->validated());

// Trigger plain text mail
Mail::to(auth()->user())->send(new OrganisationCreatedMail($organisation));

return $this
->transformItem('organisation', $organisation, ['user'])
->transformItem('organisation', $organisation, ['owner'])
->respond();
}

public function listAll(OrganisationService $service)
public function index(): 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);
}
}
}

return json_encode($Organisation_Array);
// Fetch the organisations (filter is available in query params)
$organisations = $this->organisationService->getFilteredOrganisations($this->request->all());
return $this
->transformCollection('organisations', $organisations, ['owner'])
->withPagination($organisations)
->respond();
}
}
38 changes: 38 additions & 0 deletions app/Mail/OrganisationCreatedMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Mail;

use App\Organisation;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class OrganisationCreatedMail extends Mailable
{
use Queueable, SerializesModels;

/**
* @var mixed
*/
public $organisation;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Organisation $organisation)
{
$this->organisation = $organisation;
}

/**
* Build the message.
*
* @return $this
*/
public function build(): OrganisationCreatedMail
{
return $this->text('emails.organisation-created-plain');
}
}
33 changes: 24 additions & 9 deletions app/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
/**
* 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
* @property mixed $owner
*
* @package App
*/
Expand All @@ -30,20 +31,34 @@ class Organisation extends Model
/**
* @var array
*/
protected $fillable = [];
protected $fillable = ['name', 'trial_end', 'subscribed'];

/**
* @var array
*/
protected $dates = [
'deleted_at',
'trial_end'
];
protected $casts = [
'subscribed' => 'boolean'
];

/**
* @return BelongsTo
*/
public function owner(): BelongsTo
{
return $this->belongsTo(User::class);
return $this->belongsTo(User::class, 'owner_user_id');
}

/**
* @param $query
* @param bool $state
* @return mixed
*/
public function scopeIsSubscribed($query, bool $state = true)
{
return $query->where('subscribed', $state);
}
}
31 changes: 29 additions & 2 deletions app/Services/OrganisationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace App\Services;

use App\Organisation;
use App\User;
use Carbon\Carbon;

/**
* Class OrganisationService
Expand All @@ -19,8 +21,33 @@ class OrganisationService
*/
public function createOrganisation(array $attributes): Organisation
{
$organisation = new Organisation();
if (array_key_exists('owner_user_id', $attributes)) {
$user = User::find($attributes['owner_user_id']);
} else {
$user = auth()->user();
}

return $user->organisation()->create([
'name' => $attributes['name'],
'trial_end' => Carbon::now()->addDays(30)->toDateTimeString(),
]);
}

public function getFilteredOrganisations(array $attributes, bool $withPagination = true)
{
$organisations = Organisation::query()->with('owner:id,name,email');

if (array_key_exists('filter', $attributes)) {

if ($attributes['filter'] === 'subbed') {
$organisations->isSubscribed();
}

if ($attributes['filter'] === 'trial') {
$organisations->isSubscribed(false);
}
}
return $withPagination ? $organisations->paginate() : $organisations->get();

return $organisation;
}
}
15 changes: 11 additions & 4 deletions app/Transformers/OrganisationTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Transformers;

use App\Organisation;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;

/**
Expand All @@ -13,23 +14,29 @@
*/
class OrganisationTransformer extends TransformerAbstract
{
protected array $availableIncludes = ['owner'];
/**
* @param Organisation $organisation
*
* @return array
*/
public function transform(Organisation $organisation): array
{
return [];
return [
'name' => $organisation->name,
'trial_end' => $organisation->trial_end->timestamp ?? null,
'subscribed' => $organisation->subscribed,
'created_at' => $organisation->created_at->timestamp
];
}

/**
* @param Organisation $organisation
*
* @return \League\Fractal\Resource\Item
* @return Item
*/
public function includeUser(Organisation $organisation)
public function includeOwner(Organisation $organisation): Item
{
return $this->item($organisation->user, new UserTransformer());
return $this->item($organisation->owner, new UserTransformer());
}
}
28 changes: 28 additions & 0 deletions app/Transformers/UserTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Transformers;

use App\User;
use League\Fractal\TransformerAbstract;

/**
* Class OrganisationTransformer
* @package App\Transformers
*/
class UserTransformer extends TransformerAbstract
{
/**
* @param User $user
* @return array
*/
public function transform(User $user): array
{
return [
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
];
}
}
17 changes: 16 additions & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

namespace App;

use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

/**
* @property mixed $id
* @property mixed $name
* @property mixed $email
*/
class User extends Authenticatable
{
use Notifiable;
use Notifiable, HasApiTokens;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -38,4 +45,12 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

/**
* @return HasOne
*/
public function organisation(): HasOne
{
return $this->hasOne(Organisation::class, 'owner_user_id');
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
],
"license": "MIT",
"require": {
"php": "^7.2",
"php": "^7.4",
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/passport": "^8.0",
"laravel/tinker": "^2.0",
"lcobucci/jwt": "^3.0",
"spatie/laravel-fractal": "^5.6"
},
"require-dev": {
Expand Down
Loading