diff --git a/composer.json b/composer.json index 1fc5ba1..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", @@ -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/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/Casts/Money.php b/src/Casts/Money.php index c5e5389..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,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); } } 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/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/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; 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(); } 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..98d0ab7 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -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); } /** 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); } /**