diff --git a/app/Http/Controllers/StaticController.php b/app/Http/Controllers/StaticController.php index 1d2186090925..99b9a6e03972 100644 --- a/app/Http/Controllers/StaticController.php +++ b/app/Http/Controllers/StaticController.php @@ -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); } diff --git a/app/Http/ValidationRules/Project/ValidProjectForClient.php b/app/Http/ValidationRules/Project/ValidProjectForClient.php index f887961626f2..46f3b1a8c881 100644 --- a/app/Http/ValidationRules/Project/ValidProjectForClient.php +++ b/app/Http/ValidationRules/Project/ValidProjectForClient.php @@ -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']; } diff --git a/app/Models/User.php b/app/Models/User.php index 98a11089042b..04811a1d8b7d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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'); diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php index 21321b0946d6..ffe029fbb0fe 100644 --- a/app/Transformers/UserTransformer.php +++ b/app/Transformers/UserTransformer.php @@ -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 ?? '', ]; } diff --git a/database/migrations/2023_10_01_102220_add_language_id_to_users_table.php b/database/migrations/2023_10_01_102220_add_language_id_to_users_table.php new file mode 100644 index 000000000000..41641462e831 --- /dev/null +++ b/database/migrations/2023_10_01_102220_add_language_id_to_users_table.php @@ -0,0 +1,26 @@ +string('language_id')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + + } +}; diff --git a/tests/Feature/InvoiceTest.php b/tests/Feature/InvoiceTest.php index e0530f5b2056..a40ef6b25d84 100644 --- a/tests/Feature/InvoiceTest.php +++ b/tests/Feature/InvoiceTest.php @@ -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([ diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 4872dd7478a3..e75ee11ac77f 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -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();