diff --git a/app/Http/Controllers/OrganisationController.php b/app/Http/Controllers/OrganisationController.php index 765331c..b2db88e 100644 --- a/app/Http/Controllers/OrganisationController.php +++ b/app/Http/Controllers/OrganisationController.php @@ -22,6 +22,10 @@ class OrganisationController extends ApiController */ public function store(OrganisationService $service): JsonResponse { + $this->request->validate([ + 'name' => 'required|unique:organisations|max:255' + ]); + /** @var Organisation $organisation */ $organisation = $service->createOrganisation($this->request->all()); @@ -30,33 +34,19 @@ public function store(OrganisationService $service): JsonResponse ->respond(); } - 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); - } - } - } - - return json_encode($Organisation_Array); + $filter = isset($_GET['filter']) ? $_GET['filter'] : null; + + $organisations = $service->getOrganisations($filter); + + return $this + ->transformCollection('organisation', $organisations) + ->respond(); } } diff --git a/app/Mail/SendMail.php b/app/Mail/SendMail.php new file mode 100644 index 0000000..adfd63f --- /dev/null +++ b/app/Mail/SendMail.php @@ -0,0 +1,35 @@ +data = $data; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->view('confirmation'); + } +} diff --git a/app/Organisation.php b/app/Organisation.php index e3e4d60..3e1427e 100644 --- a/app/Organisation.php +++ b/app/Organisation.php @@ -25,18 +25,27 @@ */ class Organisation extends Model { + protected $table = 'organisations'; + use SoftDeletes; /** * @var array */ - protected $fillable = []; + protected $fillable = [ + 'name', + 'owner_user_id', + 'trial_end', + 'subscribed' + ]; /** * @var array */ protected $dates = [ - 'deleted_at', + 'created_at', + 'updated_at', + 'deleted_at' ]; /** @@ -44,6 +53,6 @@ class Organisation extends Model */ public function owner(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class, 'owner_user_id'); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5b..87703a0 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,6 +3,7 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; +use Laravel\Passport\Passport; class AppServiceProvider extends ServiceProvider { @@ -23,6 +24,8 @@ public function register() */ public function boot() { - // + // $this->registerPolicies(); + + Passport::routes(); } } diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index 2218c84..c069e8c 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -5,6 +5,13 @@ namespace App\Services; use App\Organisation; +use App\Mail\SendMail; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Mail; +use Carbon\Carbon; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\App; +use App\Transformers\OrganisationTransformer; /** * Class OrganisationService @@ -19,8 +26,50 @@ class OrganisationService */ public function createOrganisation(array $attributes): Organisation { + $auth = App::make(Auth::class); $organisation = new Organisation(); + + // $organisation->create($attributes); + + //save organisation + $organisation->name = $attributes['name']; + $organisation->owner_user_id = $auth::id(); + $organisation->trial_end = Carbon::now()->add('day', 30); + $organisation->save(); + + //send email + $mailData['user'] = $auth::user()->name; + $mailData['organisation'] = $organisation->name; + $mailData['trial_end'] = (string)$organisation->trial_end; + + Mail::to($auth::user()->email)->send(new SendMail($mailData)); return $organisation; } + + /** + * @param string $filter + * + * @return Organisations + */ + public function getOrganisations($filter = null) + { + $organisation = new Organisation(); + + if ($filter === 'all' || !$filter) { + $Organisations = $organisation->all(); + } else { + if ($filter == 'subbed') { + $status = 1; + } elseif ($filter == 'trial') { + $status = 0; + } else { + throw new \Exception('Filter param imvalid'); + } + + $Organisations = $organisation->where('subscribed', '=', $status)->get(); + } + + return $Organisations; + } } diff --git a/app/Transformers/OrganisationTransformer.php b/app/Transformers/OrganisationTransformer.php index e55ef51..621ed89 100644 --- a/app/Transformers/OrganisationTransformer.php +++ b/app/Transformers/OrganisationTransformer.php @@ -20,7 +20,19 @@ class OrganisationTransformer extends TransformerAbstract */ public function transform(Organisation $organisation): array { - return []; + $owner = $this->includeUser($organisation); + + return [ + 'id' => (int) $organisation->id, + 'name' => (string) $organisation->name, + 'owner' => [ + 'user_id' => (int) $organisation->owner_user_id, + 'user_name' => $owner['name'], + 'user_email' => $owner['email'] + ], + 'subscribed' => (bool) $organisation->subscribed, + 'trial_end' => ($organisation->trial_end ? (int) strtotime((string)$organisation->trial_end) : null) + ]; } /** @@ -30,6 +42,9 @@ public function transform(Organisation $organisation): array */ public function includeUser(Organisation $organisation) { - return $this->item($organisation->user, new UserTransformer()); + $UserTransformer = new UserTransformer(); + $owner = $UserTransformer->transform($organisation->owner); + + return $owner; } } diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php new file mode 100644 index 0000000..5b1ee4d --- /dev/null +++ b/app/Transformers/UserTransformer.php @@ -0,0 +1,28 @@ + (string) $user->name, + 'email' => (string) $user->email, + ]; + } +} diff --git a/app/User.php b/app/User.php index 12d131f..850ee98 100644 --- a/app/User.php +++ b/app/User.php @@ -2,12 +2,13 @@ namespace App; +use Laravel\Passport\HasApiTokens; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { - use Notifiable; + use HasApiTokens, Notifiable; /** * The attributes that are mass assignable. @@ -38,4 +39,12 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + /** + * @return HasMany + */ + public function organisation(): HasMany + { + return $this->hasMany(organisation::class, 'id', 'onwer_user_id'); + } } diff --git a/config/auth.php b/config/auth.php index aaf982b..04c6eec 100644 --- a/config/auth.php +++ b/config/auth.php @@ -42,7 +42,7 @@ ], 'api' => [ - 'driver' => 'token', + 'driver' => 'passport', 'provider' => 'users', 'hash' => false, ], diff --git a/database/migrations/2019_12_20_094217_create_organisations_table.php b/database/migrations/2019_12_20_094217_create_organisations_table.php index 5998727..eacd714 100644 --- a/database/migrations/2019_12_20_094217_create_organisations_table.php +++ b/database/migrations/2019_12_20_094217_create_organisations_table.php @@ -16,12 +16,16 @@ public function up() Schema::create('organisations', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); - $table->integer('owner_user_id'); + $table->unsignedBigInteger('owner_user_id')->index(); $table->dateTime('trial_end')->nullable(); $table->boolean('subscribed')->default(0); $table->timestamps(); $table->softDeletes(); }); + + Schema::table('organisations', function(Blueprint $table) { + $table->foreign('owner_user_id')->references('id')->on('users'); + }); } /** diff --git a/package-lock.json b/package-lock.json index 77de63b..3cf684f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3792,8 +3792,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -3814,14 +3813,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3836,20 +3833,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -3966,8 +3960,7 @@ "inherits": { "version": "2.0.4", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -3979,7 +3972,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3994,7 +3986,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -4002,14 +3993,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.9.0", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -4028,7 +4017,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -4118,8 +4106,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -4131,7 +4118,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -4217,8 +4203,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -4254,7 +4239,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4274,7 +4258,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4318,14 +4301,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, diff --git a/resources/views/confirmation.blade.php b/resources/views/confirmation.blade.php new file mode 100644 index 0000000..0ef7219 --- /dev/null +++ b/resources/views/confirmation.blade.php @@ -0,0 +1,3 @@ +

Dear {{ $data['user'] }}

+

{{ $data['organisation']}} has been registered

+

Trial ends {{ $data['trial_end'] }}

diff --git a/routes/api.php b/routes/api.php index df3e3d9..625d09f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,5 +21,6 @@ Route::prefix('organisation')->group(function () { Route::get('', 'OrganisationController@listAll'); - Route::post('', 'OrganisationControlller@create'); + Route::middleware('auth:api')->post('', 'OrganisationController@store'); }); +