From a840136575970c4e7e863c15f86554db5943dff9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 11 Sep 2019 09:31:55 +1000 Subject: [PATCH] API cleanup --- app/Constants.php | 59 ------------------- app/Http/Controllers/Auth/LoginController.php | 4 +- app/Http/Controllers/BaseController.php | 10 ++-- app/Http/Kernel.php | 1 + app/Http/Middleware/StartupCheck.php | 16 +++-- app/Models/Client.php | 2 +- app/Models/Company.php | 5 ++ app/Models/PaymentTerm.php | 4 +- app/Models/PaymentType.php | 4 ++ app/Models/Size.php | 4 +- app/Transformers/CompanyTransformer.php | 2 +- app/Utils/Statics.php | 55 ++++++++++++++++- app/Utils/TranslationHelper.php | 2 +- config/ninja.php | 52 ++++++++++++++++ .../2014_10_13_000000_create_users_table.php | 1 - database/seeds/UsersTableSeeder.php | 1 - 16 files changed, 143 insertions(+), 79 deletions(-) diff --git a/app/Constants.php b/app/Constants.php index 5cbdec78062f..6c21bd04a2de 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -19,65 +19,6 @@ define('BANK_LIBRARY_OFX', 1); - - -$cached_tables = [ - 'currencies' => 'App\Models\Currency', -// 'sizes' => 'App\Models\Size', - 'industries' => 'App\Models\Industry', -// 'timezones' => 'App\Models\Timezone', -// 'dateFormats' => 'App\Models\DateFormat', -// 'datetimeFormats' => 'App\Models\DatetimeFormat', - 'languages' => 'App\Models\Language', - 'paymentTypes' => 'App\Models\PaymentType', - 'countries' => 'App\Models\Country', -// 'invoiceDesigns' => 'App\Models\InvoiceDesign', -// 'invoiceStatus' => 'App\Models\InvoiceStatus', -// 'frequencies' => 'App\Models\Frequency', -// 'gateways' => 'App\Models\Gateway', -// 'gatewayTypes' => 'App\Models\GatewayType', -// 'fonts' => 'App\Models\Font', -// 'banks' => 'App\Models\Bank', - ]; - -define('CACHED_TABLES', serialize($cached_tables)); - -define('CACHED_PAYMENT_TERMS', serialize( - [ - [ - 'num_days' => 0, - 'name' => '', - ], - [ - 'num_days' => 7, - 'name' => '', - ], - [ - 'num_days' => 10, - 'name' => '', - ], - [ - 'num_days' => 14, - 'name' => '', - ], - [ - 'num_days' => 15, - 'name' => '', - ], - [ - 'num_days' => 30, - 'name' => '', - ], - [ - 'num_days' => 60, - 'name' => '', - ], - [ - 'num_days' => 90, - 'name' => '', - ] - ])); - define('GATEWAY_TYPE_CREDIT_CARD', 1); define('GATEWAY_TYPE_BANK_TRANSFER', 2); define('GATEWAY_TYPE_PAYPAL', 3); diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 18f10988236e..6e3ff52c10ed 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -93,7 +93,7 @@ class LoginController extends BaseController if ($this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); - return response()->json(['message' => 'Too many login attempts, you are being throttled'], 401); + return response()->json(['message' => 'Too many login attempts, you are being throttled'], 401)->header('X-API-VERSION', config('ninja.api_version')); } if ($this->attemptLogin($request)) @@ -102,7 +102,7 @@ class LoginController extends BaseController $this->incrementLoginAttempts($request); - return response()->json(['message' => ctrans('texts.invalid_credentials')], 401); + return response()->json(['message' => ctrans('texts.invalid_credentials')], 401)->header('X-API-VERSION', config('ninja.api_version')); } } diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 84b1fceed658..26ea3b95467b 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -108,7 +108,7 @@ class BaseController extends Controller public function notFound() { return response()->json([ - 'message' => '404 | Nothing to see here!'], 404); + 'message' => '404 | Nothing to see here!'], 404)->header('X-API-VERSION', config('ninja.api_version')); } public function notFoundClient() @@ -197,6 +197,9 @@ class BaseController extends Controller $data = $this->createItem($item, $transformer, $this->entity_type); + if(request()->include_static) + $data['static'] = Statics::company(); + return $this->response($data); } @@ -220,7 +223,7 @@ class BaseController extends Controller //'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, X-Requested-With', //'Access-Control-Allow-Credentials' => 'true', 'X-Total-Count' => $count, - 'X-Muudeo-Version' => config('ninja.api_version'), + 'X-API-VERSION' => config('ninja.api_version'), //'X-Rate-Limit-Limit' - The number of allowed requests in the current period //'X-Rate-Limit-Remaining' - The number of remaining requests in the current period //'X-Rate-Limit-Reset' - The number of seconds left in the current period, @@ -235,9 +238,6 @@ class BaseController extends Controller foreach ($included as $include) { if ($include == 'clients') { $data[] = 'clients.contacts'; - } elseif ($include == 'tracks') { - $data[] = 'tracks.comments'; - $data[] = 'tracks.tags'; } elseif ($include) { $data[] = $include; } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 976ceb81325b..3cde587b1177 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -53,6 +53,7 @@ class Kernel extends HttpKernel 'throttle:60,1', 'bindings', 'query_logging', + \App\Http\Middleware\StartupCheck::class, ], 'contact' => [ 'throttle:60,1', diff --git a/app/Http/Middleware/StartupCheck.php b/app/Http/Middleware/StartupCheck.php index c37888974bdf..39557dd1e9b1 100644 --- a/app/Http/Middleware/StartupCheck.php +++ b/app/Http/Middleware/StartupCheck.php @@ -15,6 +15,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; use Closure; @@ -34,12 +35,14 @@ class StartupCheck */ public function handle(Request $request, Closure $next) { - - $cached_tables = unserialize(CACHED_TABLES); + $start = microtime(true); + Log::error('start up check'); - if (Input::has('clear_cache')) { + $cached_tables = config('ninja.cached_tables'); + + if (Input::has('clear_cache')) Session::flash('message', 'Cache cleared'); - } + foreach ($cached_tables as $name => $class) { if (Input::has('clear_cache') || ! Cache::has($name)) { @@ -47,7 +50,7 @@ class StartupCheck if (! Schema::hasTable((new $class())->getTable())) { continue; } - if ($name == 'paymentTerms') { + if ($name == 'payment_terms') { $orderBy = 'num_days'; } elseif ($name == 'fonts') { $orderBy = 'sort_order'; @@ -63,6 +66,9 @@ class StartupCheck } } + $end = microtime(true) - $start; + Log::error("middleware cost = {$end} ms"); + $response = $next($request); return $response; diff --git a/app/Models/Client.php b/app/Models/Client.php index 8b43dda39827..e933caa91542 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -162,7 +162,7 @@ class Client extends BaseModel return $this->group_settings->{$setting}; //check company level - + } diff --git a/app/Models/Company.php b/app/Models/Company.php index 1af881b9ed00..d18303ddd262 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -173,6 +173,11 @@ class Company extends BaseModel return Language::find($this->settings->language_id); } + public function getLocale() + { + return isset($this->settings->language_id) && $this->language() ? $this->language()->locale : config('ninja.i18n.locale'); + } + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/app/Models/PaymentTerm.php b/app/Models/PaymentTerm.php index b1943cb766b9..b74fbccd0431 100644 --- a/app/Models/PaymentTerm.php +++ b/app/Models/PaymentTerm.php @@ -12,6 +12,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Facades\Log; /** * Class PaymentTerm. @@ -36,7 +37,8 @@ class PaymentTerm extends BaseModel public static function getCompanyTerms() { - $default_terms = collect(unserialize(CACHED_PAYMENT_TERMS)); + //Log::error('getting company terms'); + $default_terms = collect(config('ninja.payment_terms')); $terms = self::scope()->get(); diff --git a/app/Models/PaymentType.php b/app/Models/PaymentType.php index 801f579c2868..21e4166df076 100644 --- a/app/Models/PaymentType.php +++ b/app/Models/PaymentType.php @@ -15,4 +15,8 @@ use Illuminate\Database\Eloquent\Model; class PaymentType extends Model { + /** + * @var bool + */ + public $timestamps = false; } diff --git a/app/Models/Size.php b/app/Models/Size.php index 11197114f69b..73e3d2aea100 100644 --- a/app/Models/Size.php +++ b/app/Models/Size.php @@ -10,13 +10,15 @@ */ namespace App\Models; +use Illuminate\Database\Eloquent\Model; /** * Class Size. */ -class Size extends BaseModel +class Size extends Model { + /** * @var bool */ diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 3b68a686b2ef..19f41a57ac48 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -49,7 +49,7 @@ class CompanyTransformer extends EntityTransformer 'language', 'expenses', 'payments', - 'company_user' + 'company_user', ]; diff --git a/app/Utils/Statics.php b/app/Utils/Statics.php index f8bb62f6d3d2..581f9f7def79 100644 --- a/app/Utils/Statics.php +++ b/app/Utils/Statics.php @@ -9,7 +9,10 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\DataMapper; +namespace App\Utils; + +use = namespace\Cache; +use Psy\Util\Str; /** * Statics @@ -57,6 +60,56 @@ class Statics ]; + /** + * Company statics + * @param string|boolean $locale The user locale + * @return array Array of statics + */ + public static function company($locale = false) :array + { + + $data = []; + + $cached_tables = config('ninja.cached_tables'); + foreach ($cached_tables as $name => $class) { + $data[$name] = Cache::get($name); + } + + if ($locale) { + $data['industries'] = Cache::get('industries')->each(function ($industry) { + $industry->name = ctrans('texts.industry_'.$industry->name); + })->sortBy(function ($industry) { + return $industry->name; + })->values(); + + $data['countries'] = Cache::get('countries')->each(function ($country) { + $country->name = ctrans('texts.country_'.$country->name); + })->sortBy(function ($country) { + return $country->name; + })->values(); + + $data['payment_types'] = Cache::get('payment_types')->each(function ($pType) { + $pType->name = ctrans('texts.payment_type_'.$pType->name); + })->sortBy(function ($pType) { + return $pType->name; + })->values(); + + $data['languages'] = Cache::get('languages')->each(function ($lang) { + $lang->name = ctrans('texts.lang_'.$lang->name); + })->sortBy(function ($lang) { + return $lang->name; + })->values(); + + $data['currencies'] = Cache::get('currencies')->each(function ($currency) { + $currency->name = ctrans('texts.currency_' . \Str::slug($currency->name, '_')); + })->sortBy(function ($currency) { + return $currency->name; + })->values(); + } + + return $data; + } + } diff --git a/app/Utils/TranslationHelper.php b/app/Utils/TranslationHelper.php index d35ea13b1520..41c7018b451a 100644 --- a/app/Utils/TranslationHelper.php +++ b/app/Utils/TranslationHelper.php @@ -38,7 +38,7 @@ class TranslationHelper public static function getPaymentTypes() { - return Cache::get('paymentTypes')->each(function ($pType) { + return Cache::get('payment_types')->each(function ($pType) { $pType->name = ctrans('texts.payment_type_'.$pType->name); })->sortBy(function ($pType) { return $pType->name; diff --git a/config/ninja.php b/config/ninja.php index 115565e5a094..37e9b283a046 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -64,6 +64,58 @@ return [ 'email' => env('MAIL_FROM_ADDRESS'), 'from_name' => env('MAIL_FROM_NAME'), ], + 'cached_tables' => [ + 'currencies' => 'App\Models\Currency', + 'sizes' => 'App\Models\Size', + 'industries' => 'App\Models\Industry', + 'timezones' => 'App\Models\Timezone', + //'dateFormats' => 'App\Models\DateFormat', + //'datetimeFormats' => 'App\Models\DatetimeFormat', + 'languages' => 'App\Models\Language', + 'payment_types' => 'App\Models\PaymentType', + 'countries' => 'App\Models\Country', + //'invoiceDesigns' => 'App\Models\InvoiceDesign', + //'invoiceStatus' => 'App\Models\InvoiceStatus', + //'frequencies' => 'App\Models\Frequency', + 'gateways' => 'App\Models\Gateway', + 'gateway_types' => 'App\Models\GatewayType', + //'fonts' => 'App\Models\Font', + 'banks' => 'App\Models\Bank', + ], + 'payment_terms' => [ + [ + 'num_days' => 0, + 'name' => '', + ], + [ + 'num_days' => 7, + 'name' => '', + ], + [ + 'num_days' => 10, + 'name' => '', + ], + [ + 'num_days' => 14, + 'name' => '', + ], + [ + 'num_days' => 15, + 'name' => '', + ], + [ + 'num_days' => 30, + 'name' => '', + ], + [ + 'num_days' => 60, + 'name' => '', + ], + [ + 'num_days' => 90, + 'name' => '', + ] + ], ]; diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index d57cd07a8314..4cfbe789c78a 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -45,7 +45,6 @@ class CreateUsersTable extends Migration $table->increments('id'); $table->string('name'); $table->integer('gateway_type_id'); - $table->timestamps(6); }); Schema::create('timezones', function ($table) { diff --git a/database/seeds/UsersTableSeeder.php b/database/seeds/UsersTableSeeder.php index c35e5ecd76ad..1a41e3229d21 100644 --- a/database/seeds/UsersTableSeeder.php +++ b/database/seeds/UsersTableSeeder.php @@ -35,7 +35,6 @@ class UsersTableSeeder extends Seeder $account->save(); $user = factory(\App\Models\User::class)->create([ - 'account_id' => $account->id, 'confirmation_code' => $this->createDbHash(config('database.default')) ]);