Skip to content

Commit

Permalink
Merge pull request #436 from abdielbytes/abdiel
Browse files Browse the repository at this point in the history
fix: broken tests
  • Loading branch information
timiajayi authored Aug 8, 2024
2 parents 4b2f884 + 737f749 commit 1b77aa1
Show file tree
Hide file tree
Showing 24 changed files with 574 additions and 566 deletions.
18 changes: 9 additions & 9 deletions .scribe/endpoints.cache/00.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ endpoints:
-
httpMethods:
- POST
uri: 'api/v1/organizations/{org_id}/products'
uri: 'api/v1/organisations/{org_id}/products'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -1691,7 +1691,7 @@ endpoints:
-
httpMethods:
- PATCH
uri: 'api/v1/organizations/{org_id}/products/{product_id}'
uri: 'api/v1/organisations/{org_id}/products/{product_id}'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -3110,7 +3110,7 @@ endpoints:
responses:
-
status: 400
content: '{"message":"Invalid or expired invitation link","errors":["Invalid invitation link format","Expired invitation link","Organization not found"],"status_code":400}'
content: '{"message":"Invalid or expired invitation link","errors":["Invalid invitation link format","Expired invitation link","organisation not found"],"status_code":400}'
headers:
cache-control: 'no-cache, private'
content-type: application/json
Expand Down Expand Up @@ -3983,7 +3983,7 @@ endpoints:
-
httpMethods:
- POST
uri: api/v1/organizations
uri: api/v1/organisations
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4093,7 +4093,7 @@ endpoints:
-
httpMethods:
- GET
uri: api/v1/organizations
uri: api/v1/organisations
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4132,7 +4132,7 @@ endpoints:
-
httpMethods:
- PUT
uri: 'api/v1/organizations/{org_id}'
uri: 'api/v1/organisations/{org_id}'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4251,7 +4251,7 @@ endpoints:
-
httpMethods:
- DELETE
uri: 'api/v1/organizations/{org_id}'
uri: 'api/v1/organisations/{org_id}'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4291,7 +4291,7 @@ endpoints:
-
httpMethods:
- DELETE
uri: 'api/v1/organizations/{org_id}/users/{user_id}'
uri: 'api/v1/organisations/{org_id}/users/{user_id}'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4341,7 +4341,7 @@ endpoints:
-
httpMethods:
- GET
uri: 'api/v1/organizations/{organisation}/users'
uri: 'api/v1/organisations/{organisation}/users'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down
18 changes: 9 additions & 9 deletions .scribe/endpoints/00.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@ endpoints:
-
httpMethods:
- POST
uri: 'api/v1/organizations/{org_id}/products'
uri: 'api/v1/organisations/{org_id}/products'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -1689,7 +1689,7 @@ endpoints:
-
httpMethods:
- PATCH
uri: 'api/v1/organizations/{org_id}/products/{product_id}'
uri: 'api/v1/organisations/{org_id}/products/{product_id}'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -3108,7 +3108,7 @@ endpoints:
responses:
-
status: 400
content: '{"message":"Invalid or expired invitation link","errors":["Invalid invitation link format","Expired invitation link","Organization not found"],"status_code":400}'
content: '{"message":"Invalid or expired invitation link","errors":["Invalid invitation link format","Expired invitation link","organisation not found"],"status_code":400}'
headers:
cache-control: 'no-cache, private'
content-type: application/json
Expand Down Expand Up @@ -3981,7 +3981,7 @@ endpoints:
-
httpMethods:
- POST
uri: api/v1/organizations
uri: api/v1/organisations
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4091,7 +4091,7 @@ endpoints:
-
httpMethods:
- GET
uri: api/v1/organizations
uri: api/v1/organisations
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4130,7 +4130,7 @@ endpoints:
-
httpMethods:
- PUT
uri: 'api/v1/organizations/{org_id}'
uri: 'api/v1/organisations/{org_id}'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4249,7 +4249,7 @@ endpoints:
-
httpMethods:
- DELETE
uri: 'api/v1/organizations/{org_id}'
uri: 'api/v1/organisations/{org_id}'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4289,7 +4289,7 @@ endpoints:
-
httpMethods:
- DELETE
uri: 'api/v1/organizations/{org_id}/users/{user_id}'
uri: 'api/v1/organisations/{org_id}/users/{user_id}'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down Expand Up @@ -4339,7 +4339,7 @@ endpoints:
-
httpMethods:
- GET
uri: 'api/v1/organizations/{organisation}/users'
uri: 'api/v1/organisations/{organisation}/users'
metadata:
groupName: Endpoints
groupDescription: ''
Expand Down
2 changes: 1 addition & 1 deletion app/Exports/UsersExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function headings(): array
'Profile Phone',
'Profile Avatar URL',
'Products',
'Organizations',
'organisations',
];
}

Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Api/V1/Auth/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ public function store(Request $request)
'last_name' => $request->last_name
]);

$organization = $user->owned_organisations()->create([
$organisation = $user->owned_organisations()->create([
'name' => $request->first_name."'s Organisation",
]);

$organization_user = OrganisationUser::create([
$organisation_user = OrganisationUser::create([
'user_id' => $user->id,
'org_id' => $organization->org_id
'org_id' => $organisation->org_id
]);

$roles = $user->roles()->create([
'name' => $role,
'org_id' => $organization->org_id
'org_id' => $organisation->org_id
]);
DB::table('users_roles')->insert([
'user_id' => $user->id,
Expand Down
93 changes: 59 additions & 34 deletions app/Http/Controllers/Api/V1/Organisation/OrganisationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,72 @@ class OrganisationController extends Controller
/**
* Display a listing of the resource.
*/
public function index()
{
try {
$user = auth('api')->user();
if (!$user) {
return response()->json([
'status' => 'error',
'message' => 'Unauthorized',
'status_code' => 401
], 401);
}
public function index($user_id)
{
try {
// Ensure that the authenticated user is accessing their own organisations
$authUser = auth('api')->user();
if (!$authUser) {
return response()->json([
'status' => 'error',
'message' => 'Unauthorized',
'status_code' => 401
], 401);
}

$organisations = $user->organisations;
// Check if the authenticated user is trying to access their own data
if ($authUser->id !== (string) $user_id) {
Log::info('Authenticated User ID:', ['id' => $authUser->id]);
Log::info('Requested User ID:', ['requested_id' => $user_id]);
return response()->json([
'status' => 'error',
'message' => 'Forbidden',
'status_code' => 403
], 403);
}

if ($organisations->isEmpty()) {
return response()->json([
'status' => 'success',
'message' => 'No organisations available',
'data' => [
'organisations' => []
]
], 200);
}
// Find the user by ID, handling potential non-existence
$user = User::where('id', $user_id)->first();
if (!$user) {
return response()->json([
'status' => 'error',
'message' => 'User not found',
'status_code' => 404
], 404);
}

// Retrieve the user's organisations
$organisations = $user->organisations;

if ($organisations->isEmpty()) {
return response()->json([
'status' => 'success',
'message' => 'Organizations retrieved successfully',
'message' => 'No organisations available',
'status_code' => 200,
'data' => [
'organisations' => OrganisationResource::collection($organisations)
'organisations' => []
]
]);
} catch (\Exception $e) {
return response()->json([
'status' => 'error',
'message' => 'An error occurred',
'status_code' => 500,
'error' => $e->getMessage()
], 500);
], 200);
}

return response()->json([
'status' => 'success',
'message' => 'Organisations retrieved successfully',
'status_code' => 200,
'data' => [
'organisations' => OrganisationResource::collection($organisations)
]
]);
} catch (\Exception $e) {
Log::error('An error occurred while retrieving organisations:', ['error' => $e->getMessage()]);
return response()->json([
'status' => 'error',
'message' => 'An error occurred',
'status_code' => 500,
'error' => $e->getMessage()
], 500);
}
}


/**
Expand Down Expand Up @@ -141,10 +166,10 @@ public function destroy($org_id)

public function removeUser(Request $request, $org_id, $user_id)
{
$organization = Organisation::findOrFail($org_id);
$organisation = Organisation::findOrFail($org_id);

// Use $request->auth instead of Auth::user()
if (!$request->user()->can('removeUser', $organization)) {
if (!$request->user()->can('removeUser', $organisation)) {
return response()->json([
'status' => 'Forbidden',
'message' => 'Only admin can remove users',
Expand All @@ -154,7 +179,7 @@ public function removeUser(Request $request, $org_id, $user_id)

$user = User::find($user_id);

if (!$user || !$organization->users()->detach($user)) {
if (!$user || !$organisation->users()->detach($user)) {
return response()->json([
'status' => 'forbidden',
'message' => 'user not found',
Expand Down
Loading

0 comments on commit 1b77aa1

Please sign in to comment.