Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/.gitignore
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
Expand Down
Empty file modified src/app/Http/Controllers/OrganisationController.php
100644 → 100755
Empty file.
105 changes: 73 additions & 32 deletions src/app/Http/Controllers/VolunteerController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand All @@ -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}",
Expand All @@ -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",
Expand Down Expand Up @@ -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',
Copy link
Contributor

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

'email' => ['required','string','email','max:255', new VolunteerEmail($request->get('ssn', ''))],
'phone' => 'required|string|min:6|',
'ssn' => new Cnp,
'name' => 'required|string|max:255',
Expand All @@ -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'];
Expand All @@ -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;
Expand All @@ -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 : '';
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this?
So in case only one course was added to a volunteer, that course was ignored?

}
$volunteer->save();

Expand All @@ -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")
* )
Expand Down Expand Up @@ -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'. */
Expand Down Expand Up @@ -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}",
Expand Down Expand Up @@ -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",
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down
6 changes: 3 additions & 3 deletions src/app/Rules/Cnp.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete the lines that are no longer needed.

Copy link
Author

Choose a reason for hiding this comment

The 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];
Expand Down
52 changes: 52 additions & 0 deletions src/app/Rules/VolunteerEmail.php
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 modified src/artisan
100755 → 100644
Empty file.
Empty file modified src/bootstrap/cache/.gitignore
100644 → 100755
Empty file.
Empty file modified src/database/migrations/v_1_0_0.php
100755 → 100644
Empty file.
Empty file modified src/storage/api-docs/api-docs.json
100644 → 100755
Empty file.
Empty file modified src/storage/app/.gitignore
100644 → 100755
Empty file.
Empty file modified src/storage/app/public/.gitignore
100644 → 100755
Empty file.
Empty file modified src/storage/app/public/template-resurse.csv
100644 → 100755
Empty file.
Empty file modified src/storage/app/public/template-voluntari.csv
100644 → 100755
Empty file.
Empty file modified src/storage/framework/.gitignore
100644 → 100755
Empty file.
Empty file modified src/storage/framework/cache/.gitignore
100644 → 100755
Empty file.
Empty file modified src/storage/framework/cache/data/.gitignore
100644 → 100755
Empty file.
Empty file modified src/storage/framework/sessions/.gitignore
100644 → 100755
Empty file.
Empty file modified src/storage/framework/testing/.gitignore
100644 → 100755
Empty file.
Empty file modified src/storage/framework/views/.gitignore
100644 → 100755
Empty file.
Empty file modified src/storage/logs/.gitignore
100644 → 100755
Empty file.