Add language id to users

This commit is contained in:
David Bomba 2023-10-01 21:52:26 +11:00
parent ea8381fd11
commit cdc345f75b
7 changed files with 117 additions and 16 deletions

View File

@ -56,7 +56,7 @@ class StaticController extends BaseController
/** @var \App\Models\User $user */
$user = auth()->user();
$response = Statics::company($user->company()->getLocale());
$response = Statics::company($user->getLocale() ?? $user->company()->getLocale());
return response()->json($response, 200, ['Content-type'=> 'application/json; charset=utf-8'], JSON_PRETTY_PRINT);
}

View File

@ -44,18 +44,20 @@ class ValidProjectForClient implements Rule
return true;
}
// if (is_string($this->input['project_id'])) {
// $this->input['project_id'] = $this->decodePrimaryKey($this->input['project_id']);
// }
$project = Project::withTrashed()->find($this->input['project_id']);
if (! $project) {
$this->message = 'Project not found';
return;
}
if(!isset($this->input['client_id'])){
$this->message = 'No Client ID provided.';
return false;
}
return $project->client_id == $this->input['client_id'];
}

View File

@ -19,6 +19,7 @@ use App\Utils\Traits\MakesHash;
use App\Jobs\Mail\NinjaMailerJob;
use App\Services\User\UserService;
use App\Utils\Traits\UserSettings;
use Illuminate\Support\Facades\App;
use App\Jobs\Mail\NinjaMailerObject;
use App\Mail\Admin\ResetPasswordObject;
use Illuminate\Database\Eloquent\Model;
@ -61,6 +62,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
* @property string|null $last_login
* @property string|null $signature
* @property string $password
* @property string $language_id
* @property string|null $remember_token
* @property string|null $custom_value1
* @property string|null $custom_value2
@ -153,6 +155,7 @@ class User extends Authenticatable implements MustVerifyEmail
'custom_value4',
'is_deleted',
'shopify_user_id',
'language_id',
// 'oauth_user_token',
// 'oauth_user_refresh_token',
];
@ -649,6 +652,21 @@ class User extends Authenticatable implements MustVerifyEmail
return new UserService($this);
}
public function language()
{
return $this->belongsTo(Language::class);
}
public function getLocale()
{
$locale = $this->language->locale ?? false;
if($locale)
App::setLocale($locale);
return $locale;
}
public function translate_entity()
{
return ctrans('texts.user');

View File

@ -62,7 +62,8 @@ class UserTransformer extends EntityTransformer
'google_2fa_secret' => (bool) $user->google_2fa_secret,
'has_password' => (bool) empty($user->password) ? false : true,
'oauth_user_token' => empty($user->oauth_user_token) ? '' : '***',
'verified_phone_number' => (bool) $user->verified_phone_number
'verified_phone_number' => (bool) $user->verified_phone_number,
'language_id' => (string) $user->language_id ?? '',
];
}

View File

@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('language_id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
}
};

View File

@ -11,17 +11,18 @@
namespace Tests\Feature;
use App\Helpers\Invoice\InvoiceSum;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Invoice;
use App\Repositories\InvoiceRepository;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
use Tests\TestCase;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Project;
use Tests\MockAccountData;
use App\Models\ClientContact;
use App\Utils\Traits\MakesHash;
use App\Helpers\Invoice\InvoiceSum;
use App\Repositories\InvoiceRepository;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -48,6 +49,41 @@ class InvoiceTest extends TestCase
$this->makeTestData();
}
public function testPostNewInvoiceWithProjectButNoClient()
{
$p = Project::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
]);
$invoice = [
'status_id' => 1,
'number' => 'dfdfd',
'discount' => 0,
'is_amount_discount' => 1,
'po_number' => '3434343',
'public_notes' => 'notes',
'is_deleted' => 0,
'custom_value1' => 0,
'custom_value2' => 0,
'custom_value3' => 0,
'custom_value4' => 0,
'status' => 1,
'project_id' => $p->hashed_id
// 'client_id' => $this->encodePrimaryKey($this->client->id),
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/invoices/', $invoice)
->assertStatus(422);
}
public function testInvoiceGetDatesBetween()
{
$response = $this->withHeaders([

View File

@ -109,6 +109,24 @@ class UserTest extends TestCase
}
public function testUserLocale()
{
$this->user->language_id = "13";
$this->user->save();
$this->assertEquals("fr_CA", $this->user->getLocale());
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/statics');
$response->assertStatus(200);
}
public function testUserResponse()
{
$company_token = $this->mockAccount();