diff --git a/src/.gitignore b/src/.gitignore index b6a4b86..fae607d 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,6 +1,7 @@ /node_modules /public/hot /public/storage +/public/uploads /storage/*.key /vendor /.idea diff --git a/src/app/Http/Controllers/OrganisationController.php b/src/app/Http/Controllers/OrganisationController.php old mode 100644 new mode 100755 diff --git a/src/app/Http/Controllers/VolunteerController.php b/src/app/Http/Controllers/VolunteerController.php old mode 100644 new mode 100755 index 51227ae..d660a87 --- a/src/app/Http/Controllers/VolunteerController.php +++ b/src/app/Http/Controllers/VolunteerController.php @@ -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{ + /** 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']; + } + } + } $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 */ diff --git a/src/app/Rules/Cnp.php b/src/app/Rules/Cnp.php old mode 100644 new mode 100755 index 1526bbb..23643f6 --- a/src/app/Rules/Cnp.php +++ b/src/app/Rules/Cnp.php @@ -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) { + if (strlen($value) === 13 ) { $cnp = array_map('intval',str_split($value)); $coefs = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9]; diff --git a/src/app/Rules/VolunteerEmail.php b/src/app/Rules/VolunteerEmail.php new file mode 100755 index 0000000..dcb608f --- /dev/null +++ b/src/app/Rules/VolunteerEmail.php @@ -0,0 +1,52 @@ +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.'; + } +} diff --git a/src/artisan b/src/artisan old mode 100755 new mode 100644 diff --git a/src/bootstrap/cache/.gitignore b/src/bootstrap/cache/.gitignore old mode 100644 new mode 100755 diff --git a/src/database/migrations/v_1_0_0.php b/src/database/migrations/v_1_0_0.php old mode 100755 new mode 100644 diff --git a/src/storage/api-docs/api-docs.json b/src/storage/api-docs/api-docs.json old mode 100644 new mode 100755 diff --git a/src/storage/app/.gitignore b/src/storage/app/.gitignore old mode 100644 new mode 100755 diff --git a/src/storage/app/public/.gitignore b/src/storage/app/public/.gitignore old mode 100644 new mode 100755 diff --git a/src/storage/app/public/template-resurse.csv b/src/storage/app/public/template-resurse.csv old mode 100644 new mode 100755 diff --git a/src/storage/app/public/template-voluntari.csv b/src/storage/app/public/template-voluntari.csv old mode 100644 new mode 100755 diff --git a/src/storage/framework/.gitignore b/src/storage/framework/.gitignore old mode 100644 new mode 100755 diff --git a/src/storage/framework/cache/.gitignore b/src/storage/framework/cache/.gitignore old mode 100644 new mode 100755 diff --git a/src/storage/framework/cache/data/.gitignore b/src/storage/framework/cache/data/.gitignore old mode 100644 new mode 100755 diff --git a/src/storage/framework/sessions/.gitignore b/src/storage/framework/sessions/.gitignore old mode 100644 new mode 100755 diff --git a/src/storage/framework/testing/.gitignore b/src/storage/framework/testing/.gitignore old mode 100644 new mode 100755 diff --git a/src/storage/framework/views/.gitignore b/src/storage/framework/views/.gitignore old mode 100644 new mode 100755 diff --git a/src/storage/logs/.gitignore b/src/storage/logs/.gitignore old mode 100644 new mode 100755