Skip to content

Commit

Permalink
Merge pull request #43 from fleetbase/fix-internals-for-api
Browse files Browse the repository at this point in the history
patches required for base of fixes on api
  • Loading branch information
roncodes authored Jan 12, 2024
2 parents da81fdb + 1ab9a55 commit 5b63074
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 12 deletions.
3 changes: 2 additions & 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.5",
"version": "1.3.6",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand All @@ -24,6 +24,7 @@
"giggsey/libphonenumber-for-php": "^8.13",
"grimzy/laravel-mysql-spatial": "^5.0",
"guzzlehttp/guzzle": "^7.4",
"hammerstone/fast-paginate": "^1.0",
"illuminate/broadcasting": "^7.0|^8.0|^9.0",
"illuminate/contracts": "^7.0|^8.0|^9.0",
"illuminate/database": "^7.0|^8.0|^9.0",
Expand Down
65 changes: 65 additions & 0 deletions config/laravel-mysql-s3-backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use Aws\Credentials\CredentialProvider;

$provider = CredentialProvider::defaultProvider();

$s3Config = [
'version' => 'latest',
'bucket' => env('DB_BACKUP_BUCKET', 'fleetbase-db-backups'),
'region' => env('AWS_DEFAULT_REGION', 'ap-southeast-1'),
'endpoint' => env('AWS_ENDPOINT')
];

if (env('APP_ENV') === 'local' || env('APP_ENV') === 'development') {
$s3Config['key'] = env('AWS_ACCESS_KEY_ID');
$s3Config['secret'] = env('AWS_SECRET_ACCESS_KEY');
} else {
$s3Config['credentials'] = $provider;
}

return [
/*
* Configure with your Amazon S3 credentials
* You should use an IAM user who only has PutObject access
* to a specified bucket
*/
's3' => $s3Config,

/*
* Want to add some custom mysqldump args?
*/
'custom_mysqldump_args' => '--default-character-set=utf8mb4',

/*
* Whether or not to gzip the .sql file
*/
'gzip' => true,

/*
* Time allowed to run backup
*/
'sql_timout' => 7200, // 2 hours

/*
* Backup filename
*/
'filename' => str_replace([' ', '-'], '_', env('APP_ENV', 'local')) . '_%s_backup-%s.sql',

/*
* Where to store the backup file locally
*/
'backup_dir' => '/tmp',

/*
* Do you want to keep a copy of it or delete it
* after it's been uploaded?
*/
'keep_local_copy' => false,

/*
* Do you want to keep a rolling number of
* backups on S3? How many days worth?
*/
'rolling_backup_days' => 30,
];
5 changes: 5 additions & 0 deletions src/Casts/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fleetbase\Support\Utils;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Str;

class Money implements CastsAttributes
{
Expand All @@ -28,6 +29,10 @@ public function get($model, $key, $value, $attributes)
*/
public function set($model, $key, $value, $attributes)
{
if (is_float($value) || Str::contains($value, '.')) {
$value = number_format($value, 2, '.', '');
}

return Utils::numbersOnly($value);
}
}
30 changes: 30 additions & 0 deletions src/Expansions/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,34 @@ public function error()
);
};
}

/**
* Formats a error response for the consumable API.
*
* @return Closure
*/
public function apiError()
{
/*
* Returns an error response.
*
* @param array $params
* @param mixed $default
* @return mixed
*/
return function ($error, int $statusCode = 400, ?array $data = []) {
if ($error instanceof MessageBag) {
$error = $error->all();
}

/* @var \Illuminate\Support\Facades\Response $this */
return static::json(
[
'error' => $error,
...$data,
],
$statusCode
);
};
}
}
1 change: 0 additions & 1 deletion src/Http/Controllers/Internal/v1/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ public function testNotificationChannelsConfig(Request $request)
$responseMessage = $e->getMessage();
$status = 'error';
} catch (\Throwable $e) {
dd($e);
$responseMessage = $e->getMessage();
$status = 'error';
}
Expand Down
3 changes: 1 addition & 2 deletions src/Mail/TestEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Fleetbase\Mail;

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

class TestEmail extends Mailable implements ShouldQueue
class TestEmail extends Mailable
{
use Queueable;
use SerializesModels;
Expand Down
3 changes: 1 addition & 2 deletions src/Mail/VerifyEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

use Fleetbase\Models\VerificationCode;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\HtmlString;

class VerifyEmail extends Mailable implements ShouldQueue
class VerifyEmail extends Mailable
{
use Queueable;
use SerializesModels;
Expand Down
1 change: 1 addition & 0 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function boot()
$this->mergeConfigFrom(__DIR__ . '/../../config/activitylog.php', 'activitylog');
$this->mergeConfigFrom(__DIR__ . '/../../config/excel.php', 'excel');
$this->mergeConfigFrom(__DIR__ . '/../../config/sentry.php', 'sentry');
$this->mergeConfigFrom(__DIR__ . '/../../config/laravel-mysql-s3-backup.php', 'laravel-mysql-s3-backup');
$this->mergeConfigFromSettings();
$this->addServerIpAsAllowedOrigin();
}
Expand Down
1 change: 1 addition & 0 deletions src/Support/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static function httpFilterForModel(Model $model, string $namespace = null
} else {
$internal = Http::isInternalRequest();

$baseNamespace = $filterNs;
if ($internal) {
$baseNamespace = $filterNs . 'Internal\\';
}
Expand Down
9 changes: 5 additions & 4 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,14 @@ public static function randomNumber($length = 4)
/**
* Converts the param to an integer with numbers only.
*
* @param string|mixed $string
*
* @return int
*/
public static function numbersOnly($string)
public static function numbersOnly($value)
{
return intval(preg_replace('/[^0-9]/', '', $string));
$string = strval($value);
$string = preg_replace('/[^0-9]/', '', $string);

return intval($string);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasApiModelBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function queryFromRequest(Request $request, \Closure $queryCallback = nul
$limit = 999999999;
}

return $builder->paginate($limit, $columns);
return $builder->fastPaginate($limit, $columns);
}

// get the results
Expand Down Expand Up @@ -630,7 +630,7 @@ public function searchRecordFromRequest(Request $request)
$limit = $request->integer('limit', 30);
$builder = $this->searchBuilder($request);

return $builder->paginate($limit);
return $builder->fastPaginate($limit);
}

/**
Expand Down

0 comments on commit 5b63074

Please sign in to comment.