From e96f175272b4cc726013787c1020672037ff6ce0 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Wed, 10 Jan 2024 18:05:47 +0800 Subject: [PATCH 1/7] patches required for base of fixes on api --- src/Casts/Money.php | 1 + src/Expansions/Response.php | 30 ++++++++++++++++++++++++++++++ src/Support/Find.php | 1 + src/Support/Utils.php | 6 +++++- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Casts/Money.php b/src/Casts/Money.php index c5e5389..9d3533f 100644 --- a/src/Casts/Money.php +++ b/src/Casts/Money.php @@ -28,6 +28,7 @@ public function get($model, $key, $value, $attributes) */ public function set($model, $key, $value, $attributes) { + // $currency = data_get($attributes, 'currency'); return Utils::numbersOnly($value); } } diff --git a/src/Expansions/Response.php b/src/Expansions/Response.php index cbf6bc0..25d76e2 100644 --- a/src/Expansions/Response.php +++ b/src/Expansions/Response.php @@ -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 + ); + }; + } } diff --git a/src/Support/Find.php b/src/Support/Find.php index 4a6a341..caad3a9 100644 --- a/src/Support/Find.php +++ b/src/Support/Find.php @@ -119,6 +119,7 @@ public static function httpFilterForModel(Model $model, string $namespace = null } else { $internal = Http::isInternalRequest(); + $baseNamespace = $filterNs; if ($internal) { $baseNamespace = $filterNs . 'Internal\\'; } diff --git a/src/Support/Utils.php b/src/Support/Utils.php index baf1d5e..2b0b576 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -639,7 +639,11 @@ public static function randomNumber($length = 4) */ public static function numbersOnly($string) { - return intval(preg_replace('/[^0-9]/', '', $string)); + // Remove everything except numbers and decimal points + $number = preg_replace('/[^0-9.]/', '', $string); + + // Convert to float and then to integer to handle decimals + return intval(floatval($number) * 100); } /** From a59b06143ce85c6ca417c7153a174e4d75ec8615 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 11 Jan 2024 16:22:43 +0800 Subject: [PATCH 2/7] patches for casting money and fix numbersOnly util --- src/Casts/Money.php | 6 +++++- src/Support/Utils.php | 12 +++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Casts/Money.php b/src/Casts/Money.php index 9d3533f..f03c6f2 100644 --- a/src/Casts/Money.php +++ b/src/Casts/Money.php @@ -4,6 +4,7 @@ use Fleetbase\Support\Utils; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; +use Illuminate\Support\Str; class Money implements CastsAttributes { @@ -28,7 +29,10 @@ public function get($model, $key, $value, $attributes) */ public function set($model, $key, $value, $attributes) { - // $currency = data_get($attributes, 'currency'); + if (is_float($value) || Str::contains($value, '.')) { + $value = number_format($value, 2, '.', ''); + } + return Utils::numbersOnly($value); } } diff --git a/src/Support/Utils.php b/src/Support/Utils.php index 2b0b576..442b06f 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -633,17 +633,15 @@ public static function randomNumber($length = 4) /** * Converts the param to an integer with numbers only. * - * @param string|mixed $string - * + * @param string|float|integer|mixed $string * @return int */ - public static function numbersOnly($string) + public static function numbersOnly($value) { - // Remove everything except numbers and decimal points - $number = preg_replace('/[^0-9.]/', '', $string); + $string = strval($value); + $string = preg_replace('/[^0-9]/', '', $string); - // Convert to float and then to integer to handle decimals - return intval(floatval($number) * 100); + return intval($string); } /** From 4ec25f0ebb4b2c7752cd2a03d6adc8156f0b8202 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 11 Jan 2024 16:58:28 +0800 Subject: [PATCH 3/7] patched db backup command --- config/laravel-mysql-s3-backup.php | 65 +++++++++++++++++++ .../Internal/v1/SettingController.php | 1 - src/Providers/CoreServiceProvider.php | 1 + 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 config/laravel-mysql-s3-backup.php diff --git a/config/laravel-mysql-s3-backup.php b/config/laravel-mysql-s3-backup.php new file mode 100644 index 0000000..ca8bd22 --- /dev/null +++ b/config/laravel-mysql-s3-backup.php @@ -0,0 +1,65 @@ + '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, +]; diff --git a/src/Http/Controllers/Internal/v1/SettingController.php b/src/Http/Controllers/Internal/v1/SettingController.php index b0f4ed0..9ffca05 100644 --- a/src/Http/Controllers/Internal/v1/SettingController.php +++ b/src/Http/Controllers/Internal/v1/SettingController.php @@ -411,7 +411,6 @@ public function testNotificationChannelsConfig(Request $request) $responseMessage = $e->getMessage(); $status = 'error'; } catch (\Throwable $e) { - dd($e); $responseMessage = $e->getMessage(); $status = 'error'; } diff --git a/src/Providers/CoreServiceProvider.php b/src/Providers/CoreServiceProvider.php index 5cee655..2fd2ec2 100644 --- a/src/Providers/CoreServiceProvider.php +++ b/src/Providers/CoreServiceProvider.php @@ -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(); } From b1d5b428bf296ce0681535b1a629cbf892a011d4 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Thu, 11 Jan 2024 17:59:06 +0800 Subject: [PATCH 4/7] migrate to use `fastPaginate()` for faster pagination --- composer.json | 1 + src/Traits/HasApiModelBehavior.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 1fc5ba1..a921bdf 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Traits/HasApiModelBehavior.php b/src/Traits/HasApiModelBehavior.php index 8339b91..858d8f2 100644 --- a/src/Traits/HasApiModelBehavior.php +++ b/src/Traits/HasApiModelBehavior.php @@ -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 @@ -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); } /** From 5288b8799556db8eed0a37ff5cfeb866c5d85096 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 12 Jan 2024 17:06:33 +0800 Subject: [PATCH 5/7] disable queing of emails --- src/Mail/TestEmail.php | 3 +-- src/Mail/VerifyEmail.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Mail/TestEmail.php b/src/Mail/TestEmail.php index 042f23d..edd936d 100644 --- a/src/Mail/TestEmail.php +++ b/src/Mail/TestEmail.php @@ -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; diff --git a/src/Mail/VerifyEmail.php b/src/Mail/VerifyEmail.php index 5eeba74..b544fc7 100644 --- a/src/Mail/VerifyEmail.php +++ b/src/Mail/VerifyEmail.php @@ -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; From 1e0134f15076350f90ec2140c890b4bcd2890507 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 12 Jan 2024 17:07:51 +0800 Subject: [PATCH 6/7] bump version to 1.3.6 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a921bdf..72fc674 100644 --- a/composer.json +++ b/composer.json @@ -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", From 1ab9a555654ce5e4623521b8d3710cd7b9d166f4 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 12 Jan 2024 17:09:01 +0800 Subject: [PATCH 7/7] ran linter --- src/Support/Utils.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Support/Utils.php b/src/Support/Utils.php index 442b06f..98d0ab7 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -633,7 +633,6 @@ public static function randomNumber($length = 4) /** * Converts the param to an integer with numbers only. * - * @param string|float|integer|mixed $string * @return int */ public static function numbersOnly($value)