This repository was archived by the owner on Oct 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Dan b #114 api #124
Open
danut3101
wants to merge
2
commits into
code4romania:master
Choose a base branch
from
danut3101:DanB_#114_API
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dan b #114 api #124
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| /node_modules | ||
| /public/hot | ||
| /public/storage | ||
| /public/uploads | ||
| /storage/*.key | ||
| /vendor | ||
| /.idea | ||
|
|
||
Empty file.
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| use App\Organisation; | ||
| use App\Allocation; | ||
| use App\Rules\Cnp; | ||
| use App\Rules\VolunteerEmail; | ||
| use Carbon\Carbon; | ||
| use App\DBViews\StaticCitiesBySlugAndNameView; | ||
| use App\DBViews\StaticCountiesBySlugAndNameView; | ||
|
|
@@ -26,12 +27,12 @@ class VolunteerController extends Controller | |
| { | ||
| /** | ||
| * Function responsible of processing get all volunteers requests. | ||
| * | ||
| * | ||
| * @param object $request Contains all the data needed for extracting all the volunteers list. | ||
| * | ||
| * | ||
| * @return object 200 and the list of volunteers if successful | ||
| * 500 if an error occurs | ||
| * | ||
| * | ||
| * @SWG\Get( | ||
| * tags={"Volunteers"}, | ||
| * path="/api/volunteers", | ||
|
|
@@ -58,12 +59,12 @@ public function index(Request $request) { | |
|
|
||
| /** | ||
| * Function responsible of processing get volunteer requests. | ||
| * | ||
| * | ||
| * @param string $id The ID of the volunteer to be extracted. | ||
| * | ||
| * | ||
| * @return object 200 and the volunteer details if successful | ||
| * 500 if an error occurs | ||
| * | ||
| * | ||
| * @SWG\Get( | ||
| * tags={"Volunteers"}, | ||
| * path="/api/volunteers/{id}", | ||
|
|
@@ -82,14 +83,14 @@ public function show($id) { | |
|
|
||
| /** | ||
| * Function responsible of processing put volunteer requests. | ||
| * | ||
| * | ||
| * @param object $request Contains all the data needed for saving a new volunteer. | ||
| * | ||
| * | ||
| * @return object 201 and the volunteer details if successful | ||
| * 400 if validation fails | ||
| * 404 if organization not found fails | ||
| * 500 if an error occurs | ||
| * | ||
| * | ||
| * @SWG\Post( | ||
| * tags={"Volunteers"}, | ||
| * path="/api/volunteers", | ||
|
|
@@ -204,7 +205,8 @@ public function store(Request $request) { | |
| $data = $request->all(); | ||
| $rules = [ | ||
| 'organisation_id' => 'required', | ||
| 'email' => 'required|string|email|max:255|unique:volunteers.volunteers', | ||
| // 'email' => 'required|string|email|max:255|unique:volunteers.volunteers', | ||
| 'email' => ['required','string','email','max:255', new VolunteerEmail($request->get('ssn', ''))], | ||
| 'phone' => 'required|string|min:6|', | ||
| 'ssn' => new Cnp, | ||
| 'name' => 'required|string|max:255', | ||
|
|
@@ -215,7 +217,7 @@ public function store(Request $request) { | |
| return response(['errors' => $validator->errors()->all()], 400); | ||
| } | ||
|
|
||
| /** Validate SSN uniqueness. */ | ||
| /** Validate SSN format and uniqueness. */ | ||
| $data = convertData($validator->validated(), $rules); | ||
| if (isset($validator->validated()['ssn']) && $validator->validated()['ssn']) { | ||
| $data['ssn'] = $validator->validated()['ssn']; | ||
|
|
@@ -231,7 +233,7 @@ public function store(Request $request) { | |
| $organisation_id = $request->organisation_id; | ||
| $organisation = \DB::connection('organisations')->collection('organisations')->where('_id', '=', $organisation_id)->get(['_id', 'name', 'website'])->first(); | ||
| if (!$organisation) { | ||
| return response()->json('Organizatia nu exista', 404); | ||
| return response()->json('Organizatia nu exista', 404); | ||
| } | ||
|
|
||
| $data['organisation'] = $organisation; | ||
|
|
@@ -240,15 +242,23 @@ public function store(Request $request) { | |
| if ($request->has('county')) { | ||
| $data['county'] = getCityOrCounty($request->county,County::query()); | ||
| } | ||
| if ($request->has('city')) { | ||
| if ($request->has('city')) { | ||
| $data['city'] = getCityOrCounty($request->city,City::query()); | ||
| } | ||
|
|
||
| /** Add 'adden by' to the the volunteer */ | ||
| $data['added_by'] = \Auth::check() ? \Auth::user()->_id : ''; | ||
| $data['courses'] = []; | ||
| /** Create the volunteer. */ | ||
| $volunteer = Volunteer::create($data); | ||
|
|
||
| /** if SSN already exists in database then update the organisation_id and course */ | ||
| $volunteer = Volunteer::query()->where('ssn', '=', $data['ssn'])->first(); | ||
| if($volunteer){ | ||
| $volunteer->organisation = $data['organisation']; | ||
| $data['courses'] = $volunteer->courses; | ||
| }else{ | ||
cosminbosutar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** Create the volunteer. */ | ||
| $volunteer = Volunteer::create($data); | ||
| } | ||
|
|
||
| /** Extract all courses from request and process them. */ | ||
| $courses = $request->has('courses') ? $request->courses : ''; | ||
|
|
@@ -284,6 +294,37 @@ public function store(Request $request) { | |
| } | ||
| /** Add the 'courses' to the volunteer. */ | ||
| $volunteer->courses = $data['courses']; | ||
| }else{ | ||
| /** if is specified only a course */ | ||
| $course_name_id = $request->has('course_name_id') ? $request->course_name_id : ''; | ||
| if (trim($course_name_id)!='') { | ||
| $course_name = CourseName::find($course_name_id); | ||
| if ($course_name) { | ||
| /** Create a new course. */ | ||
| $newCourse = [ | ||
| 'course_name' => [ | ||
| '_id' => $course_name['_id'], | ||
| 'name' => $course_name['name'], | ||
| 'slug' => removeDiacritics($course_name['name'])], | ||
| 'obtained' => Carbon::parse($request->get('obtained',''))->format('Y-m-d H:i:s') | ||
| ]; | ||
| /** Check if the accreditor already exists in DB. */ | ||
| $courseAccreditor = CourseAccreditor::query()->where('name', '=', $request->get('accredited_by',''))->first(); | ||
| if (!$courseAccreditor) { | ||
| $courseAccreditor = CourseAccreditor::create(['name' => $request->get('accredited_by',''), 'courses' => [$course_name['_id']]]); | ||
| } else { | ||
| if (is_array($courseAccreditor->courses) && !in_array($course_name['_id'], $courseAccreditor->courses)) { | ||
| $courseAccreditor->courses = array_merge( $courseAccreditor->courses, [$course_name['_id']]); | ||
| $courseAccreditor->save(); | ||
| } | ||
| } | ||
| /** Save the accreditor and add the course to the volunteer. */ | ||
| $newCourse['accredited'] = ['_id' => $courseAccreditor->_id,'name' => $courseAccreditor->name]; | ||
| $data['courses'][] = $newCourse; | ||
| $volunteer->courses = $data['courses']; | ||
| } | ||
| } | ||
|
|
||
|
Comment on lines
+298
to
+327
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested this? |
||
| } | ||
| $volunteer->save(); | ||
|
|
||
|
|
@@ -292,27 +333,27 @@ public function store(Request $request) { | |
| notifyUpdate('dsu', new VolunteerAdd(['name' => $volunteer->organisation['name']])); | ||
| } | ||
|
|
||
| return response()->json($volunteer, 201); | ||
| return response()->json($volunteer, 201); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Function responsible of processing a volunteer update requests. | ||
| * | ||
| * | ||
| * @param object $request Contains all the data needed for updating a volunteer. | ||
| * @param string $id The ID of the volunteer to be updated. | ||
| * | ||
| * | ||
| * @return object 201 and the JSON encoded volunteer details if successful | ||
| * 404 if email or CNP/SSN are invalid fails | ||
| * 500 if an error occurs | ||
| * | ||
| * | ||
| * @SWG\put( | ||
| * tags={"Volunteers"}, | ||
| * path="/api/volunteers/{id}", | ||
| * summary="Update volunteer", | ||
| * operationId="update", | ||
| * @SWG\Response(response=200, description="successful operation"), | ||
| * @SWG\Response(response=404, description="not valid"), | ||
| * @SWG\Response(response=404, description="not valid"), | ||
| * @SWG\Response(response=406, description="not acceptable"), | ||
| * @SWG\Response(response=500, description="internal server error") | ||
| * ) | ||
|
|
@@ -340,7 +381,7 @@ public function update(Request $request, $id) { | |
| if ($data['county'] && !is_null($data['county'])) { | ||
| $data['county'] = getCityOrCounty($request['county'],County::query()); | ||
| } | ||
| if ($data['city'] && !is_null($data['city'])) { | ||
| if ($data['city'] && !is_null($data['city'])) { | ||
| $data['city'] = getCityOrCounty($request['city'],City::query()); | ||
| } | ||
| /** Extract and set 'organisation_id'. */ | ||
|
|
@@ -404,12 +445,12 @@ public function update(Request $request, $id) { | |
|
|
||
| /** | ||
| * Function responsible of processing delete volunteers requests. | ||
| * | ||
| * | ||
| * @param string $id The ID of the volunteer to be deleted. | ||
| * | ||
| * | ||
| * @return object 200 if deletion is successful | ||
| * 500 if an error occurs | ||
| * | ||
| * | ||
| * @SWG\Delete( | ||
| * tags={"Volunteers"}, | ||
| * path="/api/volunteers/{id}", | ||
|
|
@@ -443,12 +484,12 @@ public function delete($id) { | |
|
|
||
| /** | ||
| * Function responsible of processing import volunteers requests. | ||
| * | ||
| * | ||
| * @param object $request Contains all the data needed for importing a list of volunteers. | ||
| * | ||
| * | ||
| * @return object 200 if import is successful | ||
| * 500 if an error occurs | ||
| * | ||
| * | ||
| * @SWG\Post( | ||
| * tags={"Volunteers"}, | ||
| * path="/api/volunteers/import", | ||
|
|
@@ -472,7 +513,7 @@ public function delete($id) { | |
| * @SWG\Response(response=406, description="not acceptable"), | ||
| * @SWG\Response(response=500, description="internal server error") | ||
| * ) | ||
| * | ||
| * | ||
| */ | ||
| public function importVolunteers(Request $request) { | ||
| $file = $request->file('file'); | ||
|
|
@@ -533,7 +574,7 @@ public function importVolunteers(Request $request) { | |
| $num = count($setUpData); | ||
| if($i == 0){ | ||
| $i++; | ||
| continue; | ||
| continue; | ||
| } | ||
| $error = verifyErrors($error, $setUpData[0],'Nume'); | ||
| $error = verifyErrors($error, $setUpData[1],'CNP'); | ||
|
|
@@ -677,9 +718,9 @@ public function importVolunteers(Request $request) { | |
|
|
||
| /** | ||
| * Function responsible of extracting the list of allocations of a volunteer. | ||
| * | ||
| * | ||
| * @param string $id The ID of the volunteer for which to extract the allocations list. | ||
| * | ||
| * | ||
| * @return object 200 if extraction is successful | ||
| * 500 if an error occurs | ||
| */ | ||
|
|
@@ -692,7 +733,7 @@ public function allocations($id) { | |
|
|
||
| /** | ||
| * Function responsible of returning the volunteers import template file. | ||
| * | ||
| * | ||
| * @return object 200 and the template-voluntari.csv file if successful | ||
| * 500 if an error occurs | ||
| */ | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -26,9 +26,9 @@ public function __construct() | |
| */ | ||
| public function passes($attribute, $value) | ||
| { | ||
|
|
||
| $exist = Volunteer::query()->where('ssn', '=', $value)->first(); | ||
| if (strlen($value) === 13 && !$exist) { | ||
| // $exist = Volunteer::query()->where('ssn', '=', $value)->first(); | ||
| // if (strlen($value) === 13 && !$exist) { | ||
|
Comment on lines
+29
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete the lines that are no longer needed.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. I will remove |
||
| if (strlen($value) === 13 ) { | ||
| $cnp = array_map('intval',str_split($value)); | ||
|
|
||
| $coefs = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9]; | ||
|
|
||
This file contains hidden or 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,52 @@ | ||
| <?php | ||
|
|
||
| namespace App\Rules; | ||
| use App\Volunteer; | ||
|
|
||
| use Illuminate\Contracts\Validation\Rule; | ||
|
|
||
| class VolunteerEmail implements Rule | ||
| { | ||
| private $cnp; | ||
| /** | ||
| * Create a new rule instance. | ||
| * | ||
| * @param string $cnp | ||
| * | ||
| * @return void | ||
| */ | ||
| public function __construct($cnp = "") | ||
| { | ||
| $this->cnp = $cnp; | ||
| } | ||
|
|
||
| /** | ||
| * Determine if the validation rule passes. | ||
| * | ||
| * @param string $attribute | ||
| * @param mixed $value | ||
| * @return bool | ||
| */ | ||
| public function passes($attribute, $value) | ||
| { | ||
| /** The email address should be unique unless the cnp is the same with an existing volunteer */ | ||
| $exist = Volunteer::query()->where('email', '=', $value)->first(); | ||
| if($exist){ | ||
| if($exist->ssn == $this->cnp){ | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation error message. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function message() | ||
| { | ||
| return 'The email has already been taken.'; | ||
| } | ||
| } |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove old line and a comment to explain what the login of "VolunteerEmail" rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok