API cleanup

This commit is contained in:
David Bomba 2019-09-11 09:31:55 +10:00
parent a74d60a5ee
commit a840136575
16 changed files with 143 additions and 79 deletions

View File

@ -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);

View File

@ -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'));
}
}

View File

@ -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;
}

View File

@ -53,6 +53,7 @@ class Kernel extends HttpKernel
'throttle:60,1',
'bindings',
'query_logging',
\App\Http\Middleware\StartupCheck::class,
],
'contact' => [
'throttle:60,1',

View File

@ -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;

View File

@ -162,7 +162,7 @@ class Client extends BaseModel
return $this->group_settings->{$setting};
//check company level
}

View File

@ -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
*/

View File

@ -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();

View File

@ -15,4 +15,8 @@ use Illuminate\Database\Eloquent\Model;
class PaymentType extends Model
{
/**
* @var bool
*/
public $timestamps = false;
}

View File

@ -10,13 +10,15 @@
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class Size.
*/
class Size extends BaseModel
class Size extends Model
{
/**
* @var bool
*/

View File

@ -49,7 +49,7 @@ class CompanyTransformer extends EntityTransformer
'language',
'expenses',
'payments',
'company_user'
'company_user',
];

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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' => '',
]
],
];

View File

@ -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) {

View File

@ -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'))
]);