Skip to content

Commit

Permalink
Merge branch 'backend' of https://github.com/hngi/Gateapp-api into ba…
Browse files Browse the repository at this point in the history
…ckend
  • Loading branch information
tobecci committed Oct 31, 2019
2 parents 460f033 + 925bd3b commit a652755
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 138 deletions.
264 changes: 263 additions & 1 deletion app/Http/Controllers/GatemanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
}
}
Loading

0 comments on commit a652755

Please sign in to comment.