Skip to content

Commit

Permalink
Merge pull request #80 from fleetbase/dev-v1.4.15
Browse files Browse the repository at this point in the history
v1.4.15
  • Loading branch information
roncodes authored Mar 27, 2024
2 parents 0080528 + 97ff284 commit d7f9646
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 9 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.4.14",
"version": "1.4.15",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('model_has_permissions', function (Blueprint $table) {
$table->renameColumn('model_id', 'model_uuid');
});

Schema::table('model_has_roles', function (Blueprint $table) {
$table->renameColumn('model_id', 'model_uuid');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('model_has_permissions', function (Blueprint $table) {
$table->renameColumn('model_uuid', 'model_id');
});

Schema::table('model_has_roles', function (Blueprint $table) {
$table->renameColumn('model_uuid', 'model_id');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Fleetbase\Models\Group;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('groups', function (Blueprint $table) {
$table->string('public_id')->nullable()->after('company_uuid')->index();
});

$groups = Group::withTrashed()->get();
foreach ($groups as $group) {
$group->update(['public_id' => Group::generatePublicId('group')]);
}
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('groups', function (Blueprint $table) {
$table->dropIndex(['public_id']);
$table->dropColumn(['public_id']);
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('categories', function (Blueprint $table) {
$table->boolean('core_category')->default(0)->index()->after('for');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('categories', function (Blueprint $table) {
$table->dropColumn('core_category');
});
}
};
58 changes: 58 additions & 0 deletions src/Casts/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,68 @@ public function get($model, $key, $value, $attributes)
*/
public function set($model, $key, $value, $attributes)
{
if ($value === null) {
return 0;
}
$value = $this->removeCurrencySymbols($value);
$value = $this->removeSpecialCharactersExceptDotAndComma($value);
if (is_float($value) || Str::contains($value, '.')) {
$value = number_format((float) $value, 2, '.', '');
}

return Utils::numbersOnly($value);
}

/**
* Removes common currency symbols from a given string.
*
* This function uses a regular expression to identify and strip out
* common currency symbols such as the Dollar ($), Euro (€), Pound (£),
* Yen (¥), Indian Rupee (₹), Cent (¢), Thai Baht (฿), Russian Ruble (₽),
* Israeli New Shekel (₪), and Korean Won (₩) from the input string.
*
* @param string $string the string from which currency symbols will be removed
*
* @return string The input string with currency symbols removed.
*
* Example usage:
* $price = "$123.45";
* $cleanPrice = removeCurrencySymbols($price);
* // $cleanPrice will be "123.45"
*/
private function removeCurrencySymbols($string)
{
$currencySymbols = '/[\$€£¥₹¢฿₽₪₩₮]/';
$cleanString = preg_replace($currencySymbols, '', $string);

return $cleanString;
}

/**
* Removes all special characters from a given string except for periods (.) and commas (,).
*
* This function is designed to clean a string by removing special characters
* and leaving only numeric characters, periods, and commas. This can be particularly
* useful for processing strings representing monetary values where the currency symbol
* and other special characters need to be removed.
*
* Note: This function assumes that periods and commas are used in the numeric context
* (as decimal or thousand separators). It may not be suitable for strings where
* periods and commas are used in other contexts.
*
* @param string $string the string to be cleaned of special characters
*
* @return string The cleaned string with only numbers, periods, and commas.
*
* Example usage:
* $price = "$1,234.56";
* $cleanPrice = removeSpecialCharactersExceptDotAndComma($price);
* // $cleanPrice will be "1,234.56"
*/
public function removeSpecialCharactersExceptDotAndComma($string)
{
$cleanString = preg_replace('/[^\d.,]/', '', $string);

return $cleanString;
}
}
12 changes: 12 additions & 0 deletions src/Http/Filter/CategoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Fleetbase\Http\Filter;

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

class CategoryFilter extends Filter
{
public function queryForInternal()
{
if ($this->request->boolean('core_category')) {
return;
}
$this->builder->where('company_uuid', $this->session->get('company'));
}

Expand All @@ -18,6 +22,14 @@ public function parentsOnly()
}
}

public function coreCategory($is)
{
$is = Utils::isTrue($is);
if ($is) {
$this->builder->where('core_category', 1);
}
}

public function parentCategory(?string $id)
{
if (Str::isUuid($id)) {
Expand Down
6 changes: 4 additions & 2 deletions src/Http/Middleware/AuthenticateOnceWithBasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ private function authenticateSanctumToken(PersonalAccessToken $sanctumToken)
if ($apiCredential) {
// Set api credential session
Auth::setApiKey($apiCredential);

return true;
} else {
Auth::setApiKey($sanctumToken);
}

return true;
}

return response()->error('Oops! The api credentials provided were not valid', 401);
Expand Down
11 changes: 11 additions & 0 deletions src/Http/Requests/Internal/UploadFileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public function rules()
'application/x-x509-ca-cert',
'application/octet-stream',
'application/json',
'application/zip',
'application/x-zip',
'application/x-zip-compressed',
'application/x-compressed',
'multipart/x-zip',
'application/x-tar',
'application/gzip',
'application/x-gzip',
'application/x-tgz',
'application/x-bzip2',
'application/x-xz',
]),
],
];
Expand Down
1 change: 1 addition & 0 deletions src/Http/Resources/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function toArray($request)
'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
'owner_uuid' => $this->when(Http::isInternalRequest(), $this->owner_uuid),
'owner_type' => $this->when(Http::isInternalRequest(), $this->owner_type),
'icon' => $this->icon,
'icon_url' => $this->icon_url,
'name' => $this->name,
'description' => $this->description,
Expand Down
9 changes: 5 additions & 4 deletions src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Category extends Model
*
* @var array
*/
protected $fillable = ['public_id', 'company_uuid', 'owner_uuid', 'parent_uuid', 'icon_file_uuid', 'owner_type', 'internal_id', 'name', 'description', 'translations', 'meta', 'tags', 'icon', 'icon_color', 'slug', 'order', 'for'];
protected $fillable = ['public_id', 'company_uuid', 'owner_uuid', 'parent_uuid', 'icon_file_uuid', 'owner_type', 'internal_id', 'name', 'description', 'translations', 'meta', 'tags', 'icon', 'icon_color', 'slug', 'order', 'for', 'core_category'];

/**
* Dynamic attributes that are appended to object.
Expand Down Expand Up @@ -76,9 +76,10 @@ class Category extends Model
* @var array
*/
protected $casts = [
'tags' => 'array',
'meta' => Json::class,
'translations' => Json::class,
'core_category' => 'boolean',
'tags' => 'array',
'meta' => Json::class,
'translations' => Json::class,
];

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Fleetbase\Traits\Filterable;
use Fleetbase\Traits\HasApiModelBehavior;
use Fleetbase\Traits\HasPolicies;
use Fleetbase\Traits\HasPublicId;
use Fleetbase\Traits\HasUuid;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasPermissions;
Expand All @@ -18,6 +19,7 @@
class Group extends Model
{
use HasUuid;
use HasPublicId;
use HasApiModelBehavior;
use HasPermissions;
use HasPolicies;
Expand All @@ -40,6 +42,13 @@ class Group extends Model
*/
protected $table = 'groups';

/**
* The type of public Id to generate.
*
* @var string
*/
protected $publicIdType = 'group';

/**
* The attributes that can be queried.
*
Expand All @@ -52,7 +61,7 @@ class Group extends Model
*
* @var array
*/
protected $fillable = ['_key', 'company_uuid', 'name', 'description', 'slug'];
protected $fillable = ['_key', 'public_id', 'company_uuid', 'name', 'description', 'slug'];

/**
* The relationships that will always be appended.
Expand Down
19 changes: 19 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ public function companies()
return $this->hasMany(CompanyUser::class, 'user_uuid');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function groups()
{
return $this->hasManyThrough(Group::class, GroupUser::class, 'user_uuid', 'uuid', 'uuid', 'group_uuid');
}

/**
* Generates a unique username based on the provided name.
*
Expand Down Expand Up @@ -290,6 +298,17 @@ public function assignCompany(Company $company): User
return $this;
}

/**
* Set the company for the user.
*/
public function setCompany(Company $company): User
{
$this->company_uuid = $company->uuid;
$this->save();

return $this;
}

/**
* Checks if user is the owner of the company.
*/
Expand Down
18 changes: 17 additions & 1 deletion src/Support/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Auth as Authentication;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laravel\Sanctum\PersonalAccessToken;

class Auth extends Authentication
{
Expand Down Expand Up @@ -83,10 +84,25 @@ public static function setSession($user = null, $login = false): bool
*/
public static function setApiKey($apiCredential)
{
// If sanctum token indicate in session
if ($apiCredential instanceof PersonalAccessToken) {
session([
'is_sanctum_token' => true,
'api_credential' => $apiCredential->id,
'api_key' => $apiCredential->token,
'api_key_version' => (string) $apiCredential->created_at,
'api_secret' => $apiCredential->token,
'api_environment' => 'live',
'api_test_mode' => false,
]);

return true;
}

session([
'api_credential' => $apiCredential->uuid,
'api_key' => $apiCredential->key,
'api_key_version' => $apiCredential->created_at,
'api_key_version' => (string) $apiCredential->created_at,
'api_secret' => $apiCredential->secret,
'api_environment' => $apiCredential->test_mode ? 'test' : 'live',
'api_test_mode' => $apiCredential->test_mode,
Expand Down
Loading

0 comments on commit d7f9646

Please sign in to comment.