Refactor for store client request

This commit is contained in:
David Bomba 2022-06-23 13:12:28 +10:00
parent ade8b24031
commit 4eed22d811
4 changed files with 36 additions and 44 deletions

View File

@ -68,6 +68,7 @@ class StoreClientRequest extends Request
$rules['contacts'] = 'array'; $rules['contacts'] = 'array';
$rules['contacts.*.email'] = 'bail|nullable|distinct|sometimes|email'; $rules['contacts.*.email'] = 'bail|nullable|distinct|sometimes|email';
$rules['contacts.*.password'] = [ $rules['contacts.*.password'] = [
'bail',
'nullable', 'nullable',
'sometimes', 'sometimes',
'string', 'string',
@ -82,8 +83,8 @@ class StoreClientRequest extends Request
$rules['id'] = new CanStoreClientsRule(auth()->user()->company()->id); $rules['id'] = new CanStoreClientsRule(auth()->user()->company()->id);
} }
$rules['number'] = ['nullable', Rule::unique('clients')->where('company_id', auth()->user()->company()->id)]; $rules['number'] = ['bail', 'nullable', Rule::unique('clients')->where('company_id', auth()->user()->company()->id)];
$rules['id_number'] = ['nullable', Rule::unique('clients')->where('company_id', auth()->user()->company()->id)]; $rules['id_number'] = ['bail', 'nullable', Rule::unique('clients')->where('company_id', auth()->user()->company()->id)];
return $rules; return $rules;
} }
@ -92,37 +93,30 @@ class StoreClientRequest extends Request
{ {
$input = $this->all(); $input = $this->all();
$settings = ClientSettings::defaults(); /* Default settings */
$tmp = []; $settings = (array)ClientSettings::defaults();
$tmp['settings'] = (array)$settings; /* Stub settings if they don't exist */
if(!array_key_exists('settings', $input))
nlog("seeetings"); $input['settings'] = [];
nlog($tmp['settings']);
/* Merge default into base settings */
if (array_key_exists('settings', $input)) $input['settings'] = array_merge($input['settings'], $settings);
nlog($input['settings']);
if (array_key_exists('settings', $input) && ! empty($input['settings'])) { /* Type enforcement */
foreach ($input['settings'] as $key => $value) { foreach ($input['settings'] as $key => $value)
if ($key == 'default_task_rate') { {
$value = floatval($value); if ($key == 'default_task_rate') {
} $value = floatval($value);
$input['settings'][$key] = $value;
$tmp['settings'][$key] = $value;
} }
} }
$input['settings'] = $tmp['settings']; /* Convert hashed IDs to IDs*/
$input = $this->decodePrimaryKeys($input); $input = $this->decodePrimaryKeys($input);
if (isset($input['group_settings_id'])) {
$input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']);
}
//is no settings->currency_id is set then lets dive in and find either a group or company currency all the below may be redundant!! //is no settings->currency_id is set then lets dive in and find either a group or company currency all the below may be redundant!!
if (! property_exists($settings, 'currency_id') && isset($input['group_settings_id'])) { if (! array_key_exists('currency_id', $input['settings']) && isset($input['group_settings_id'])) {
$group_settings = GroupSetting::find($input['group_settings_id']); $group_settings = GroupSetting::find($input['group_settings_id']);
if ($group_settings && property_exists($group_settings->settings, 'currency_id') && isset($group_settings->settings->currency_id)) { if ($group_settings && property_exists($group_settings->settings, 'currency_id') && isset($group_settings->settings->currency_id)) {
@ -130,7 +124,8 @@ class StoreClientRequest extends Request
} else { } else {
$input['settings']['currency_id'] = (string) auth()->user()->company()->settings->currency_id; $input['settings']['currency_id'] = (string) auth()->user()->company()->settings->currency_id;
} }
} elseif (! property_exists($settings, 'currency_id')) {
} elseif (! array_key_exists('currency_id', $input['settings'])) {
$input['settings']['currency_id'] = (string) auth()->user()->company()->settings->currency_id; $input['settings']['currency_id'] = (string) auth()->user()->company()->settings->currency_id;
} }
@ -145,8 +140,6 @@ class StoreClientRequest extends Request
unset($input['settings']['language_id']); unset($input['settings']['language_id']);
} }
// $input['settings'] = $settings;
if (isset($input['country_code'])) { if (isset($input['country_code'])) {
$input['country_id'] = $this->getCountryCode($input['country_code']); $input['country_id'] = $this->getCountryCode($input['country_code']);
} }
@ -155,15 +148,11 @@ class StoreClientRequest extends Request
$input['shipping_country_id'] = $this->getCountryCode($input['shipping_country_code']); $input['shipping_country_id'] = $this->getCountryCode($input['shipping_country_code']);
} }
/* If there is no number, just unset it here. */ /* If there is a client number, just unset it here. */
if (array_key_exists('number', $input) && (is_null($input['number']) || empty($input['number']))) { if (array_key_exists('number', $input) && (is_null($input['number']) || empty($input['number']))) {
unset($input['number']); unset($input['number']);
} }
nlog($input);
$this->replace($input); $this->replace($input);
} }

View File

@ -77,6 +77,10 @@ class Request extends FormRequest
public function decodePrimaryKeys($input) public function decodePrimaryKeys($input)
{ {
if (array_key_exists('group_settings_id', $input) && is_string($input['group_settings_id'])) {
$input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']);
}
if (array_key_exists('group_id', $input) && is_string($input['group_id'])) { if (array_key_exists('group_id', $input) && is_string($input['group_id'])) {
$input['group_id'] = $this->decodePrimaryKey($input['group_id']); $input['group_id'] = $this->decodePrimaryKey($input['group_id']);
} }

12
composer.lock generated
View File

@ -378,16 +378,16 @@
}, },
{ {
"name": "aws/aws-sdk-php", "name": "aws/aws-sdk-php",
"version": "3.228.0", "version": "3.228.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/aws/aws-sdk-php.git", "url": "https://github.com/aws/aws-sdk-php.git",
"reference": "4ff51d01da43aa3bd36eef921a9cd4e0ff843fab" "reference": "53b7f43945b19bb0700c75d4c5f130055096e817"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/4ff51d01da43aa3bd36eef921a9cd4e0ff843fab", "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/53b7f43945b19bb0700c75d4c5f130055096e817",
"reference": "4ff51d01da43aa3bd36eef921a9cd4e0ff843fab", "reference": "53b7f43945b19bb0700c75d4c5f130055096e817",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -463,9 +463,9 @@
"support": { "support": {
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sdk-php/issues", "issues": "https://github.com/aws/aws-sdk-php/issues",
"source": "https://github.com/aws/aws-sdk-php/tree/3.228.0" "source": "https://github.com/aws/aws-sdk-php/tree/3.228.1"
}, },
"time": "2022-06-21T18:13:25+00:00" "time": "2022-06-22T18:16:48+00:00"
}, },
{ {
"name": "bacon/bacon-qr-code", "name": "bacon/bacon-qr-code",

View File

@ -32,6 +32,7 @@ class ClientSettingsTest extends TestCase
$this->makeTestData(); $this->makeTestData();
$this->faker = \Faker\Factory::create(); $this->faker = \Faker\Factory::create();
} }
public function testClientBaseline() public function testClientBaseline()
@ -90,7 +91,7 @@ class ClientSettingsTest extends TestCase
$response->assertStatus(200); $response->assertStatus(200);
$arr = $response->json(); $arr = $response->json();
nlog($arr);
$this->assertEquals('1', $arr['data']['settings']['currency_id']); $this->assertEquals('1', $arr['data']['settings']['currency_id']);
$this->assertEquals('1', $arr['data']['settings']['language_id']); $this->assertEquals('1', $arr['data']['settings']['language_id']);
$this->assertEquals('1', $arr['data']['settings']['payment_terms']); $this->assertEquals('1', $arr['data']['settings']['payment_terms']);
@ -100,13 +101,13 @@ nlog($arr);
} }
public function testClientIllegalCurrency() public function testClientIllegalCurrency()
{nlog("illegal"); {
$data = [ $data = [
'name' => $this->faker->firstName(), 'name' => $this->faker->firstName(),
'id_number' => 'Cooliox2', 'id_number' => 'Cooliox2',
'settings' => [ 'settings' => [
'currency_id' => '2', 'currency_id' => 'a',
'language_id' => '1', 'language_id' => '1',
'payment_terms' => '1', 'payment_terms' => '1',
'valid_until' => '2', 'valid_until' => '2',
@ -127,8 +128,6 @@ nlog($arr);
nlog($message); nlog($message);
} }
nlog($response->json());
$response->assertStatus(302); $response->assertStatus(302);
} }