Skip to content

Commit

Permalink
Merge pull request #54 from fleetbase/dev-v1.3.13
Browse files Browse the repository at this point in the history
v1.3.13
  • Loading branch information
roncodes authored Feb 1, 2024
2 parents b1a6b5f + c9f7fc7 commit 9cbad09
Show file tree
Hide file tree
Showing 40 changed files with 453 additions and 108 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.3.12",
"version": "1.3.13",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
46 changes: 46 additions & 0 deletions migrations/2024_01_31_063635_create_comments_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->uuid('uuid')->nullable()->index();
$table->string('public_id')->nullable()->unique();
$table->foreignUuid('company_uuid')->references('uuid')->on('companies')->onUpdate('CASCADE')->onDelete('CASCADE');
$table->foreignUuid('author_uuid')->references('uuid')->on('users')->onUpdate('CASCADE')->onDelete('CASCADE');
// $table->foreignUuid('parent_comment_uuid')->nullable()->references('uuid')->on('comments')->onUpdate('CASCADE')->onDelete('CASCADE');
$table->uuid('subject_uuid');
$table->string('subject_type')->nullable();
$table->mediumText('content');
$table->json('tags')->nullable();
$table->json('meta')->nullable();
$table->timestamps();
$table->softDeletes();
});

Schema::table('comments', function (Blueprint $table) {
$table->foreignUuid('parent_comment_uuid')->nullable()->after('author_uuid')->references('uuid')->on('comments')->onUpdate('CASCADE')->onDelete('CASCADE');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
};
2 changes: 1 addition & 1 deletion src/Events/ResourceLifecycleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function getModelRecord()
*
* @param \Fleetbase\Model\Model $model
*
* @return \Illuminate\Http\Resources\Json\JsonResource
* @return JsonResource
*/
public function getModelResource($model, string $namespace = null, int $version = null)
{
Expand Down
17 changes: 15 additions & 2 deletions src/Http/Controllers/Api/v1/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Fleetbase\Models\Company;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laravel\Sanctum\PersonalAccessToken;

class OrganizationController extends Controller
{
public function getCurrent(Request $request)
{
$token = $request->bearerToken();
$isSecretKey = Str::startsWith($token, '$');
$companyId = null;

// Depending on API key format set the connection to find credential on
$connection = Str::startsWith($token, 'flb_test_') ? 'sandbox' : 'mysql';
Expand All @@ -33,15 +35,26 @@ public function getCurrent(Request $request)

// Get the api credential model record
$apiCredential = $findApKey->first();
if ($apiCredential) {
$companyId = $apiCredential->company_uuid;
}

// If no api credential found then try for personal access token then get company from the tokenable
if (!$apiCredential) {
$apiCredential = PersonalAccessToken::findToken($token);

if ($apiCredential->tokenable) {
$companyId = $apiCredential->tokenable->company_uuid;
}
}

// Handle no api credential found
if (!$apiCredential) {
return response()->error('No API key found to fetch company details with.');
}

// Get the organization owning the API key
$organization = Company::where('uuid', $apiCredential->company_uuid)->first();

$organization = Company::where('uuid', $companyId)->first();
return new Organization($organization);
}
}
15 changes: 15 additions & 0 deletions src/Http/Controllers/Internal/v1/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Fleetbase\Http\Controllers\Internal\v1;

use Fleetbase\Http\Controllers\FleetbaseController;

class CommentController extends FleetbaseController
{
/**
* The resource to query.
*
* @var string
*/
public $resource = 'comments';
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/Internal/v1/CompanyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getTwoFactorSettings()
/**
* Save the two factor authentication settings for the current company.
*
* @param \Illuminate\Http\Request $request the HTTP request
* @param Request $request the HTTP request
*
* @return \Illuminate\Http\Response
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/Internal/v1/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function uploadBase64(UploadBase64FileRequest $request)
/**
* Handle file uploads.
*
* @param \Fleetbase\Http\Requests\Internal\UploadFileRequest $request
* @param UploadFileRequest $request
*
* @return \Illuminate\Http\Response
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/Internal/v1/LookupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function country($code, Request $request)
/**
* Pull the Fleetbase.io blog RSS feed.
*
* @param \Illuminate\Http\Request
* @param Request
*
* @return \Illuminate\Http\Response
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/Internal/v1/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NotificationController extends FleetbaseController
/**
* Receives an array of ID's for notifications which should be marked as read.
*
* @param \Illuminate\Http\Request $request the HTTP request object
* @param Request $request the HTTP request object
*
* @return \Illuminate\Http\Response the HTTP response
*/
Expand Down Expand Up @@ -91,7 +91,7 @@ public function deleteNotification($notificationId)
/**
* Deletes all notifications for the authenticated user.
*
* @param \Illuminate\Http\Request $request the HTTP request object
* @param Request $request the HTTP request object
*
* @return \Illuminate\Http\Response the HTTP response
*/
Expand Down
12 changes: 6 additions & 6 deletions src/Http/Controllers/Internal/v1/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function saveFilesystemConfig(Request $request)
/**
* Creates a file and uploads it to the users default disks.
*
* @param \Illuminate\Http\Request $request the incoming HTTP request containing the authenticated user
* @param Request $request the incoming HTTP request containing the authenticated user
*
* @return \Illuminate\Http\JsonResponse returns a JSON response with a success message and HTTP status 200
*/
Expand Down Expand Up @@ -152,7 +152,7 @@ public function saveMailConfig(Request $request)
* test email to the user's email address. It returns a JSON response indicating whether
* the email was sent successfully.
*
* @param \Illuminate\Http\Request $request the incoming HTTP request containing the authenticated user
* @param Request $request the incoming HTTP request containing the authenticated user
*
* @return \Illuminate\Http\JsonResponse returns a JSON response with a success message and HTTP status 200
*/
Expand Down Expand Up @@ -239,7 +239,7 @@ public function saveQueueConfig(Request $request)
/**
* Sends a test message to the queue .
*
* @param \Illuminate\Http\Request $request the incoming HTTP request containing the authenticated user
* @param Request $request the incoming HTTP request containing the authenticated user
*
* @return \Illuminate\Http\JsonResponse returns a JSON response with a success message and HTTP status 200
*/
Expand Down Expand Up @@ -465,7 +465,7 @@ public function saveBrandingSettings(Request $request)
/**
* Sends a test SMS message using Twilio.
*
* @param \Illuminate\Http\Request $request the incoming HTTP request containing the authenticated user
* @param Request $request the incoming HTTP request containing the authenticated user
*
* @return \Illuminate\Http\JsonResponse returns a JSON response with a success message and HTTP status 200
*/
Expand Down Expand Up @@ -508,7 +508,7 @@ public function testTwilioConfig(Request $request)
/**
* Sends a test exception to Sentry.
*
* @param \Illuminate\Http\Request $request the incoming HTTP request containing the authenticated user
* @param Request $request the incoming HTTP request containing the authenticated user
*
* @return \Illuminate\Http\JsonResponse returns a JSON response with a success message and HTTP status 200
*/
Expand Down Expand Up @@ -566,7 +566,7 @@ public function testSentryConfig(Request $request)
/**
* Test SocketCluster Configuration.
*
* @param \Illuminate\Http\Request $request the incoming HTTP request containing the authenticated user
* @param Request $request the incoming HTTP request containing the authenticated user
*
* @return \Illuminate\Http\JsonResponse returns a JSON response with a success message and HTTP status 200
*/
Expand Down
30 changes: 30 additions & 0 deletions src/Http/Filter/CommentFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Fleetbase\Http\Filter;

use Fleetbase\Support\Utils;
use Illuminate\Support\Str;

class CommentFilter extends Filter
{
public function queryForInternal()
{
$this->builder->where('company_uuid', $this->session->get('company'));
}

public function parent(string $id) {
if (Str::isUuid($id)) {
$this->builder->where('parent_comment_uuid', $id);
}

if (Utils::isPublicId($id)) {
$this->builder->whereHas('parent', function ($query) use ($id) {
$this->builder->where('public_id', $id);
});
}
}

public function withoutParent() {
$this->builder->whereNull('parent_comment_uuid');
}
}
4 changes: 2 additions & 2 deletions src/Http/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class Filter
/**
* The request instance.
*
* @var \Illuminate\Http\Request
* @var Request
*/
protected $request;

Expand All @@ -33,7 +33,7 @@ abstract class Filter
/**
* The builder instance.
*
* @var \Illuminate\Database\Eloquent\Builder
* @var Builder
*/
protected $builder;

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/AuthenticateOnceWithBasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AuthenticateOnceWithBasicAuth
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param Request $request
*/
public function handle($request, \Closure $next)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/JoinOrganizationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class JoinOrganizationRequest extends FormRequest
*/
public function authorize()
{
return true;
return $this->session()->exists('user');
}

/**
Expand Down
16 changes: 3 additions & 13 deletions src/Http/Requests/SwitchOrganizationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Fleetbase\Http\Requests;

use Fleetbase\Support\Utils;
use Illuminate\Support\Str;
use Fleetbase\Support\Http;

class SwitchOrganizationRequest extends FleetbaseRequest
{
Expand All @@ -14,7 +13,7 @@ class SwitchOrganizationRequest extends FleetbaseRequest
*/
public function authorize()
{
return true;
return $this->session()->exists('user');
}

/**
Expand All @@ -25,16 +24,7 @@ public function authorize()
public function rules()
{
return [
'next' => ['required', $this->isApiRequest() ? 'exists:companies,public_id' : 'exists:companies,uuid'],
'next' => ['required', Http::isPublicRequest() ? 'exists:companies,public_id' : 'exists:companies,uuid'],
];
}

public function isApiRequest()
{
$routeNamespace = Utils::get($this->route(), 'action.namespace');
$isFleetOpsApiRequest = $routeNamespace === 'Fleetbase\Http\Controllers\Api\v1';
$isNavigatorApiRequest = Str::startsWith($this->route()->uri, 'navigator/v1');

return $isFleetOpsApiRequest || $isNavigatorApiRequest;
}
}
37 changes: 37 additions & 0 deletions src/Http/Resources/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Fleetbase\Http\Resources;

use Fleetbase\Support\Http;

class Author extends FleetbaseResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
'avatar_uuid' => $this->when(Http::isInternalRequest(), $this->avatar_uuid),
'name' => $this->name,
'email' => $this->email,
'phone' => $this->phone,
'country' => $this->country,
'avatar_url' => $this->avatar_url,
'company_name' => $this->company_name,
// 'type' => $this->type,
'is_admin' => $this->is_admin,
'timezone' => $this->timezone,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
];
}
}
Loading

0 comments on commit 9cbad09

Please sign in to comment.