This repository has been archived by the owner on Apr 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from coordinape/feature/dev-343-bulk-createupd…
…ate-user-list-api-endpoint Bulk Api user actions
- Loading branch information
Showing
10 changed files
with
1,158 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class BulkUserCreateRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$circle_id = $this->route('circle_id'); | ||
|
||
return [ | ||
'users' => ['required', 'array', 'min:1'], | ||
'users.*.address' => ['required', 'string', 'size:42', 'distinct', | ||
// validate id is unique within circle | ||
Rule::unique('users')->withoutTrashed()->where(function ($query) use ($circle_id) { | ||
return $query->where('circle_id', $circle_id); | ||
})], | ||
'users.*.name' => ['required', 'string', 'max:255'], | ||
'users.*.non_giver' => 'integer|min:0|max:1|nullable', | ||
'users.*.fixed_non_receiver' => 'integer|min:0|max:1|nullable', | ||
'users.*.starting_tokens' => 'integer|max:1000000|nullable', | ||
'users.*.role' => 'integer|min:0|max:1|nullable' | ||
]; | ||
} | ||
|
||
protected function prepareForValidation() | ||
{ | ||
$this->merge($this->sanitizeInput()); | ||
|
||
} | ||
|
||
private function sanitizeInput(): array | ||
{ | ||
$users = $this->get('users'); | ||
$address_array = []; | ||
if ($users) { | ||
$circle_id = $this->route('circle_id'); | ||
$allowed_fields = ['address', 'name', 'non_giver', 'fixed_non_receiver', 'starting_tokens', 'role']; | ||
foreach ($users as $idx => $user) { | ||
$users[$idx] = array_filter($user, function ($key) use ($allowed_fields) { | ||
return in_array($key, $allowed_fields); | ||
} | ||
, ARRAY_FILTER_USE_KEY); | ||
$users[$idx]['circle_id'] = $circle_id; | ||
if (!empty($user['address'])) { | ||
$lcAddress = strtolower($user['address']); | ||
$users[$idx]['address'] = $lcAddress; | ||
$address_array[] = $lcAddress; | ||
} | ||
} | ||
} | ||
return compact('users', 'address_array'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class BulkUserDeleteRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$circle_id = $this->route('circle_id'); | ||
return [ | ||
"users" => ['required', 'array', 'min:1'], | ||
'users.*' => ['required', 'integer', 'distinct', | ||
//validate id exists as a user in the circle | ||
Rule::exists('users', 'id')->withoutTrashed()->where(function ($query) use ($circle_id) { | ||
return $query->where('circle_id', $circle_id); | ||
})], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Models\User; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class BulkUserRestoreRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$circle_id = $this->route('circle_id'); | ||
return [ | ||
"users" => ['required', 'array', 'min:1'], | ||
'users.*' => ['required', 'integer', 'distinct', | ||
//validate id exists as a deleted user in circle | ||
Rule::exists('users', 'id')->where(function ($query) use ($circle_id) { | ||
return $query->where('circle_id', $circle_id)->whereNotNull('deleted_at'); | ||
})], | ||
'addresses.*' => ['distinct', 'string', 'size:42', | ||
//validate restored user doesn't conflict with existing user addresses | ||
Rule::unique('users', 'address')->withoutTrashed()->where(function ($query) use ($circle_id) { | ||
return $query->where('circle_id', $circle_id); | ||
})], | ||
]; | ||
} | ||
|
||
protected function prepareForValidation() | ||
{ | ||
$users = $this->get('users'); | ||
$restoring_addresses = []; | ||
if ($users && is_array($users)) { | ||
$restoring_addresses = User::onlyTrashed()->whereIn('id', $users)->where('circle_id', $this->route('circle_id'))->pluck('address')->toArray(); | ||
} | ||
$this->merge(['addresses' => $restoring_addresses]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Models\User; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class BulkUserUpdateRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$user_id = $this->input('users.*.id'); | ||
$circle_id = $this->route('circle_id'); | ||
return [ | ||
'users' => ['required', 'array', 'min:1'], | ||
'users.*.id' => ['required', 'integer', | ||
// validate id exists as a user in circle | ||
Rule::exists('users')->withoutTrashed()->where(function ($query) use ($circle_id) { | ||
return $query->where('circle_id', $circle_id); | ||
})], | ||
'users.*.address' => ['required', 'string', 'size:42', 'distinct', | ||
// validate address is unique within circle | ||
Rule::unique('users')->withoutTrashed()->where(function ($query) use ($circle_id, $user_id) { | ||
return $query->where('circle_id', $circle_id)->whereNotIn('id', $user_id); | ||
})], | ||
'users.*.name' => ['required', 'string', 'max:255'], | ||
'users.*.non_giver' => 'integer|min:0|max:1|nullable', | ||
'users.*.fixed_non_receiver' => 'integer|min:0|max:1', | ||
'users.*.starting_tokens' => 'integer|min:0|max:1000000', | ||
'users.*.role' => 'integer|min:0|max:1|nullable' | ||
]; | ||
} | ||
|
||
protected function prepareForValidation() | ||
{ | ||
$this->merge($this->sanitizeInput()); | ||
} | ||
|
||
private function sanitizeInput(): array | ||
{ | ||
$users_values = $this->get('users'); | ||
$admin_user_id = $this->get('admin_user')->id; | ||
$address_array = []; | ||
$id_array = []; | ||
$users = []; | ||
if ($users_values) { | ||
$circle_id = $this->route('circle_id'); | ||
$allowed_fields = ['id', 'address', 'name', 'non_giver', 'fixed_non_receiver', 'starting_tokens', 'role']; | ||
$id_array = array_map(function ($u) { | ||
return !empty($u['id']) ? $u['id'] : -1; | ||
}, $users_values); | ||
$users_current_data = User::where('circle_id', $circle_id)->whereIn('id', $id_array)->get()->keyBy('id'); | ||
foreach ($users_values as $users_value) { | ||
$user_id = !empty($users_value['id']) ? $users_value['id'] : null; | ||
if ($user_id) { | ||
$user = []; | ||
if ($users_current_data->has($user_id)) { | ||
foreach ($allowed_fields as $allowed_field) { | ||
$user[$allowed_field] = !empty($users_value[$allowed_field]) ? $users_value[$allowed_field] : $users_current_data[$user_id]->{$allowed_field}; | ||
} | ||
$user['address'] = strtolower($user['address']); | ||
$address_array[] = $user['address']; | ||
if ($user_id == $admin_user_id) { | ||
// prevents admin user from removing his own admin role | ||
$user['role'] = config('enums.user_types.admin'); | ||
} | ||
} else { | ||
$user['id'] = $user_id; | ||
} | ||
$user['circle_id'] = $circle_id; | ||
$users[] = $user; | ||
} | ||
} | ||
} | ||
return compact('users', 'address_array', 'id_array'); | ||
} | ||
} |
Oops, something went wrong.