diff --git a/app/Http/Controllers/GatemanController.php b/app/Http/Controllers/GatemanController.php index 6228e781c..2d20f8ef3 100644 --- a/app/Http/Controllers/GatemanController.php +++ b/app/Http/Controllers/GatemanController.php @@ -3,15 +3,21 @@ namespace App\Http\Controllers; use App\Gateman; +use App\Estate; use App\Notifications\GatemanAcceptanceNotification; use App\User; +use App\Home; use App\Visitor; +use Exception; use App\Visitor_History; use App\Http\Resources\Visitor as VisitorResource; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Gate; +use Illuminate\Support\Facades\DB; +use \Illuminate\Database\QueryException; +use App\Http\Controllers\ImageController; use JWTAuth; class GatemanController extends Controller @@ -230,7 +236,7 @@ public function admitVisitor(Request $request) else { $res['Error'] = "Unauthorized - Access Denied!"; $res['gatema'] = $resident; - return response()->json($res, 403); + return response()->json($res, 401); } } else { @@ -311,4 +317,260 @@ public function visitor_out(Request $request) return response()->json($res, 404); } } + + /** + * Adds a gateman to an estate + * + * @return \Illuminate\Http\Response + */ + public function addEstateGateman( + $id, + Home $home, + User $new_user, + Request $request + ){ + // Verifies that the logged-in user is assigned to the requested estate + $user_estate = Home::whereUserIdAndEstateId($this->user->id, $id)->first(); + + if (is_null($user_estate)) { + return response()->json([ + 'status' => false, + 'message'=> "Unauthorized!", + ], 401); + } + else + { + // Validate the posted data + $this->validate($request, [ + 'name' => ['required', 'regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/'], + 'phone' => ['required', 'string'], + ]); + + DB::beginTransaction(); + + try{ + // Create user + $new_user->name = $request->name; + $new_user->phone = $request->phone; + $new_user->role = 2; + $new_user->user_type = 'gateman'; + $new_user->save(); + + // Register gateman's estate + $home->user_id = $new_user->id; + $home->estate_id = $id; + $home->save(); + + // transaction was successful + DB::commit(); + + $result = [ + 'name' => $new_user->name, + 'phone' => $new_user->phone, + 'user_id' => $new_user->id, + 'home_id' => $home->id, + 'estate_id' => (int) $home->estate_id + ]; + + // send response + return response()->json([ + 'status' => true, + 'message' => 'The gateman was successfully added', + 'result' => $result + ], 200); + } catch(Exception $e) { + // transaction was not successful + DB::rollBack(); + + return response()->json([ + 'status' => false, + 'message' => 'Error, the gateman could not be added', + 'hint' => $e->getMessage() + ], 501); + } + } + } + + /** + * Gets a single gateman or all gatemen details for an estate + * + * @return \Illuminate\Http\Response + */ + public function estateGatemen( + $estate_id, + $id = null, + Request $request + ){ + // Verifies that the logged-in user is assigned to the requested estate + $user_estate = Home::whereUserIdAndEstateId($this->user->id, $estate_id)->first(); + + if (is_null($user_estate)) { + return response()->json([ + 'status' => false, + 'message'=> "Unauthorized!", + ], 401); + } + else + { + // Check if requests is for a single gateman + if (is_null($id)) { + // Request is for all gatemen associated with the estate + // Get all gatemen users type associated with the estate + $gatemen = User::join('homes', 'homes.user_id', 'users.id') + ->where('users.user_type', 'gateman') + ->where('homes.estate_id', $estate_id) + ->get(); + + return response()->json([ + 'count' => $gatemen->count(), + 'status' => true, + 'gatemen' => $gatemen, + ], 200); + } + else + { + // Request is for a single gateman associated with the estate + // Get the gateman if only he is truly a gateman and is associated with the estate + $gateman = User::join('homes', 'homes.user_id', 'users.id') + ->where('users.id', $id) + ->where('homes.estate_id', $estate_id) + ->first([ + 'users.name', 'users.username', 'users.phone', + 'users.email', 'users.image', 'users.duty_time', + 'homes.id as home_id', 'users.id as user_id' + ]); + + if($gateman) { + return response()->json([ + 'status' => true, + 'gateman' => $gateman + ], 200); + } + else { + return response()->json([ + 'status' => false, + 'message' => "We cannot verify the user with id: {$id} as a gateman assigned to ". Estate::find($estate_id)->estate_name, + ], 406); + } + } + } + } + + /** + * Updates a gateman details for an estate + * + * @return \Illuminate\Http\Response + */ + public function updateEstateGateman( + $estate_id, + $id, + Request $request + ){ + // Verifies that the logged-in user is assigned to the requested estate + $user_estate = Home::whereUserIdAndEstateId($this->user->id, $estate_id)->first(); + + if (is_null($user_estate)) { + return response()->json([ + 'status' => false, + 'message'=> "Unauthorized!", + ], 401); + } + else + { + $gateman = User::join('homes', 'homes.user_id', 'users.id') + ->where('users.id', $id) + ->where('users.user_type', 'gateman') + ->where('homes.estate_id', $estate_id) + ->get(); + + // Check if such user exists as a gateman for the estate + if (!$gateman->isEmpty()) { + // Validate the posted data + $this->validate($request, [ + 'name' => ['regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/'], + 'phone' => ['string'], + ]); + + $updatedUser = User::find($id); + + // Update user's details + $updatedUser->name = $request->name ?? $updatedUser->name; + $updatedUser->phone = $request->phone ?? $updatedUser->phone; + + // Save the update + $updatedUser->save(); + + // Return response + return response()->json([ + 'status' => true, + 'message' => "The gateman's record has successfully been updated", + 'result' => $updatedUser + ], 200); + } + else + { + return response()->json([ + 'status' => false, + 'message' => "We cannot verify the user with id: {$id} as a gateman assigned to ". Estate::find($estate_id)->estate_name, + ], 406); + } + } + } + + /** + * Deletes a gateman record for an estate + * + * @return \Illuminate\Http\Response + */ + public function deleteEstateGateman( + $estate_id, + $id, + Request $request + ){ + // Verifies that the logged-in user is assigned to the requested estate + $user_estate = Home::whereUserIdAndEstateId($this->user->id, $estate_id)->first(); + + if (is_null($user_estate)) { + return response()->json([ + 'status' => false, + 'message'=> "Unauthorized!", + ], 401); + } + else + { + $gateman = User::join('homes', 'homes.user_id', 'users.id') + ->where('users.id', $id) + ->where('users.user_type', 'gateman') + ->where('homes.estate_id', $estate_id) + ->get(); + + // Check if such user exists as a gateman for the estate + if (!$gateman->isEmpty()) { + // Delete the record + $update = User::find($id)->delete(); + + if ($update) { + return response()->json([ + 'status' => true, + 'message' => 'Gateman has been deleted successfully!', + ], 200); + } + else + { + // if delete action fails, send a response + return response()->json([ + 'status' => false, + 'message' => 'Sorry, this gateman could not be deleted at the moment!', + ], 501); + } + } + else + { + return response()->json([ + 'status' => false, + 'message' => "We cannot verify the user with id: {$id} as a gateman assigned to ". Estate::find($estate_id)->estate_name, + ], 406); + } + } + } } diff --git a/app/Http/Controllers/ServiceProviderController.php b/app/Http/Controllers/ServiceProviderController.php index 18dc0c73a..a27b2afb0 100644 --- a/app/Http/Controllers/ServiceProviderController.php +++ b/app/Http/Controllers/ServiceProviderController.php @@ -10,48 +10,52 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; use App\Http\Controllers\ImageController; +use App\Estate; class ServiceProviderController extends Controller { - public function showAll() { + public function showAll() + { $res = array(); - + if (Auth::check()) { - $user = Auth::user(); - $role = $user->role; - - if ($role === "1" || $role === "2") { + $user = Auth::user(); + $role = $user->role; + + if ($role === "1" || $role === "2") { $service = Service_Provider::all(); if (!$service->isEmpty()) { $res["status"] = 200; $res["message"] = "All service providers."; - $res["data"] = $service; + $res["data"] = $service; } else { $res["status"] = 200; $res["message"] = "No service providers registered"; } - } else { - $res['status'] = 401; - $res['message'] = "You must login as a resident or admin."; - } - } else { - $res['status'] = 401; - $res['message'] = "You are not logged in."; - } + } else { + $res['status'] = 401; + $res['message'] = "You must login as a resident or admin."; + } + } else { + $res['status'] = 401; + $res['message'] = "You are not logged in."; + } return response()->json($res, $res['status']); } - public function byEstate() { + public function byEstate() + { $user = Auth::user(); $user_id = $user->id; - $estate_id = Home::where('user_id',$user_id)->pluck('estate_id'); + $estate_id = Home::where('user_id', $user_id)->pluck('estate_id'); $res = array(); try { - - $service = Category::with(['service_provider' => function ($query) use ($estate_id) { - $query->whereIn('estate_id', $estate_id); }])->get(); + + $service = Category::with(['service_provider' => function ($query) use ($estate_id) { + $query->whereIn('estate_id', $estate_id); + }])->get(); if (!$service->isEmpty()) { @@ -74,12 +78,12 @@ public function byEstate() { public function show($id) { $res = array(); - + if (Auth::check()) { - $user = Auth::user(); - $role = $user->role; - - if ($role === "1" || $role === "2") { + $user = Auth::user(); + $role = $user->role; + + if ($role === "1" || $role === "2") { $service = Service_Provider::find($id); if (!is_null($service)) { $res["status"] = 200; @@ -87,9 +91,9 @@ public function show($id) $res["data"] = $service; } else { $res["status"] = 200; - $res["message"] = "No service provider found."; + $res["message"] = "No service provider found."; } - + } else { $res['status'] = 401; $res['message'] = "You must login as a resident or admin."; @@ -100,29 +104,30 @@ public function show($id) } return response()->json($res, $res['status']); } - - public function byCategory($category_id) { + + public function byCategory($category_id) + { $res = array(); - + if (Auth::check()) { $user = Auth::user(); $role = $user->role; - + if ($role === "1" || $role === "2") { try { - $services = Service_Provider::where('category_id', $category_id)->get(); - - if(!$services->isEmpty()) { + $services = Service_Provider::where('category_id', $category_id)->get(); + + if (!$services->isEmpty()) { $res['status'] = 200; $res['message'] = "Retrieved service providers"; $res['data'] = $services; - - } else { + + } else { $res['status'] = 404; $res['message'] = "No service providers in this category"; } - } catch(Exception $e) { + } catch (Exception $e) { $res['status'] = 501; $res['message'] = "An error occurred trying to retrieve service providers $e"; } @@ -139,41 +144,40 @@ public function byCategory($category_id) { public function create(Request $request, ImageController $image) { - $validator = Validator::make($request->all(), [ - 'name' => 'required|string|min:3', - 'phone' => 'required', - 'description' => 'required', - 'estate_id' => 'required|int', - 'category_id' => 'required|int' - ]); + $validator = Validator::make($request->all(), [ + 'name' => 'required|string|min:3', + 'phone' => 'required', + 'description' => 'required', + 'estate_id' => 'required|int', + 'category_id' => 'required|int' + ]); if ($validator->fails()) { - return ['message' => 'Please fill all Fields']; + return ['message' => 'Please fill all Fields']; } //start temporay transaction DB::beginTransaction(); - try{ + try { - $service = new Service_Provider; - $service->name = $request->input("name"); - $service->phone = $request->input("phone"); + $service = new Service_Provider; + $service->name = $request->input("name"); + $service->phone = $request->input("phone"); $service->description = $request->input("description"); - $service->estate_id = $request->input("estate_id"); + $service->estate_id = $request->input("estate_id"); $service->category_id = $request->input("category_id"); - //Upload image - //Upload image - if($request->hasFile('image')) { + //Upload image + //Upload image + if ($request->hasFile('image')) { $data = $this->upload($request, $image); - if($data['status_code'] != 200) { + if ($data['status_code'] != 200) { return response()->json($data, $data['status_code']); } $service->image = $data['image']; - }else { + } else { $data = null; $service->image = 'noimage.jpg'; - } -; + }; $service->save(); //if operation was successful save commit save to database @@ -184,7 +188,7 @@ public function create(Request $request, ImageController $image) $res['image_info'] = $data; return response()->json($res, 200); - }catch(\Exception $e) { + } catch (\Exception $e) { //rollback what is saved DB::rollBack(); @@ -197,48 +201,48 @@ public function create(Request $request, ImageController $image) } - public function update(Request $request, $id, ImageController $image) + public function update(Request $request, $id, ImageController $image) { $this->validate($request, [ - 'name' => 'required|string|min:3', - 'phone' => 'required', + 'name' => 'required|string|min:3', + 'phone' => 'required', 'description' => 'required', - 'estate_id' => 'required|int', + 'estate_id' => 'required|int', 'category_id' => 'required|int' ]); //start temporay transaction DB::beginTransaction(); - try{ - $service = Service_Provider::find($id); - $service->name = $request->input("name"); - $service->phone = $request->input("phone"); + try { + $service = Service_Provider::find($id); + $service->name = $request->input("name"); + $service->phone = $request->input("phone"); $service->description = $request->input("description"); - $service->estate_id = $request->input("estate_id"); + $service->estate_id = $request->input("estate_id"); $service->category_id = $request->input("category_id"); - //Upload image - if($request->hasFile('image')) { + //Upload image + if ($request->hasFile('image')) { $data = $this->upload($request, $image, $service); - if($data['status_code'] != 200) { + if ($data['status_code'] != 200) { return response()->json($data, $data['status_code']); } $service->image = $data['image']; - }else { + } else { $data = null; $service->image = 'noimage.jpg'; } - $service->save(); + $service->save(); - //if operation was successful save commit save to database + //if operation was successful save commit save to database DB::commit(); - $res["status"] = true; + $res["status"] = true; $res["message"] = "Service provider Updated Successfully!"; $res["service"] = $service; - $res['image_info'] = $data; + $res['image_info'] = $data; return response()->json($res, 200); - }catch(\Exception $e) { + } catch (\Exception $e) { //rollback what is saved DB::rollBack(); @@ -252,22 +256,20 @@ public function update(Request $request, $id, ImageController $image) public function destroy($id) { - $service = Service_Provider::destroy($id); - if($service) - { - $res['status'] = 200; - $res["message"] = "Service Provider Deleted!"; - - return response()->json($res, 200); - } - else - { - $res['status'] = 404; - $res["message"] = "Unable To Delete Service Provider!"; - - return response()->json($res, $res['status']); - } - } + $service = Service_Provider::destroy($id); + + if ($service) { + $res['status'] = 200; + $res["message"] = "Service Provider Deleted!"; + + return response()->json($res, 200); + } else { + $res['status'] = 404; + $res["message"] = "No service found"; + + return response()->json($res, $res['status']); + } + } public function softDelete($id) { @@ -288,7 +290,7 @@ public function softDelete($id) return response()->json($res, $res["status"]); } } - + public function search($id) { try { @@ -310,16 +312,17 @@ public function search($id) $res["status"] = "Inactive"; } - // Put all data into an array - $data = array($name, $phone, $des, $created, $updated); - - $cat = Sp_Category::find($cat_id); + $cat = Category::find($cat_id); $cat_name = $cat->title; - $data[] = $cat_name; - + $res["status_code"] = 200; $res["message"] = "Success!"; - $res["data"] = $data; + $res["name"] = $name; + $res["phone"] = $phone; + $res["description"] = $des; + $res["created"] = $created; + $res["updated"] = $updated; + $res["category"] = $cat_name; return response()->json($res, $res["status_code"]); } @@ -331,36 +334,78 @@ public function search($id) return response()->json($res, $res["status_code"]); } - } - - public function restore($id) - { - $service = Service_Provider::onlyTrashed()->find($id); - if($service) - { - $res["status"] = 200; - $res["message"] = "Service Provider Was Unsuspended!"; - $res["data"] = $service; - - return response()->json($res, $res["status"]); } - else + + public function upload($request, $image, $table = null) { - $res["status"] = 501; - $res["message"] = "Unable To Unsuspend Service Provider!"; - - return response()->json($res, $res["status"]); - } - } - - public function upload($request, $image, $table=null) { $user = Auth::user(); $this->validate($request, [ - 'image' => "image|max:4000", + 'image' => "image|max:4000", ]); //Image Engine $res = $image->imageUpload($request, $table); return $res; } + + public function create_request(Request $request, ImageController $image) + { + $validator = Validator::make($request->all(), [ + 'name' => 'required|string|min:3', + 'phone' => 'required', + 'description' => 'required', + 'estate_id' => 'required|int', + 'category_id' => 'required|int' + ]); + + if ($validator->fails()) { + return ['message' => 'Please fill all Fields']; + } + //start temporay transaction + DB::beginTransaction(); + try { + + $service = new Service_Provider; + $service->name = $request->input("name"); + $service->phone = $request->input("phone"); + $service->description = $request->input("description"); + $service->estate_id = $request->input("estate_id"); + $service->category_id = $request->input("category_id"); + $service->status = 0; + + //Upload image + //Upload image + if ($request->hasFile('image')) { + $data = $this->upload($request, $image); + if ($data['status_code'] != 200) { + return response()->json($data, $data['status_code']); + } + $service->image = $data['image']; + } else { + $data = null; + $service->image = 'noimage.jpg'; + }; + $service->save(); + + //if operation was successful save commit save to database + DB::commit(); + $res["status"] = true; + $res["message"] = "Service Provider Request Created Successfully!"; + $res["data"] = $service; + $res['image_info'] = $data; + return response()->json($res, 200); + + } catch (\Exception $e) { + //rollback what is saved + DB::rollBack(); + + $res['status'] = false; + $res['message'] = 'An error occured, please try again!'; + $res['hint'] = $e->getMessage(); + return response()->json($res, 501); + + } + + } + } diff --git a/app/Http/Controllers/VisitorController.php b/app/Http/Controllers/VisitorController.php index a397a4015..f414be666 100644 --- a/app/Http/Controllers/VisitorController.php +++ b/app/Http/Controllers/VisitorController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers; use App\Visitor_History; use App\Visitor; -use Exeception; +use Exception; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; @@ -192,7 +192,7 @@ public function store( 'image_info' => $data, 'qr_image_src'=> $qr_code ], 200); - }catch(Exeception $e) { + }catch(Exception $e) { //if any operation fails, Thanos snaps finger - user was not created rollback what is saved DB::rollBack(); $res['status'] = false; @@ -273,7 +273,7 @@ public function update( 'visitor' => $visitor, 'image_info' => $data ], 200); - }catch(Exeception $e) { + }catch(Exception $e) { //if any operation fails, rollback what is saved DB::rollBack(); $res['status'] = false; diff --git a/routes/v1.php b/routes/v1.php index 2fe006684..d39b2d77a 100644 --- a/routes/v1.php +++ b/routes/v1.php @@ -94,7 +94,7 @@ // Show all visitor - Route::get('visitors/all', 'VisitorController@index')->middleware('superAdminadmin'); + Route::get('visitors/all', 'VisitorController@index')->middleware('superAdmin'); //create faq Route::post('faq', 'FaqController@store')->middleware('superAdmin'); @@ -109,8 +109,6 @@ //delete support message Route::delete('/support/{id}', 'SupportController@destroy')->middleware('superAdmin'); - Route::get('visitors/all', 'VisitorController@index')->middleware('superAdmin'); - // Show Total Number of Estates on the system Route::get('statistics/estate', 'Statistics\EstateStatsController@index')->middleware('superAdmin'); @@ -149,16 +147,7 @@ //Show total number of pending service providers in the estate of logged in Estate Admin Route::get('statistics/pendingEstateService/', 'Statistics\ServiceStatsController@pendingEstateRequests')->middleware('estateAdmin'); - - - - }); - - - - - // General Users Routes ******************************************************* Route::group(['middleware' => ['jwt.verify']], function () { @@ -217,7 +206,17 @@ //Select Estate Route::post('/estate/choose/{id}', 'EstateController@estateMemeber'); + // Get a single gateman or all gatemen for an estate + Route::get('estate/{estate_id}/gateman/{id?}', 'GatemanController@estateGatemen')->middleware('estateAdmin'); + + // Add gateman to an estate + Route::post('estate/{id}/gateman', 'GatemanController@addEstateGateman')->middleware('estateAdmin'); + + // Edit a gateman for an estate + Route::put('estate/{estate_id}/gateman/{id}', 'GatemanController@updateEstateGateman')->middleware('estateAdmin'); + // Delete a single gateman for an estate + Route::delete('estate/{estate_id}/gateman/{id}', 'GatemanController@deleteEstateGateman')->middleware('estateAdmin'); //(Users Messging) //Get message @@ -379,6 +378,8 @@ $gateman->notify(new App\Notifications\VisitorArrivalNotification($resident, $gateman, $visitor)); }); +//----------- Service provider request route ---------------------------------// +Route::post("service_provider/create_request", "ServiceProviderController@create_request"); // Route::get('init', function () { // event(new App\Events\notify('Someone'));