Skip to content

Commit 8f34e6c

Browse files
Refactor group deletion and privacy update endpoints to streamline permission checks and error handling
1 parent 501ddab commit 8f34e6c

File tree

2 files changed

+22
-47
lines changed

2 files changed

+22
-47
lines changed

api/auth/group/delete.py

+8-21
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,25 @@
66

77
@delete_group_bp.route('/auth/group/delete', methods=['DELETE'])
88
def delete_group():
9-
"""
10-
Route to delete a group. Only the group creator can delete the group.
11-
"""
129
id_token = request.headers.get('Authorization')
13-
1410
if not id_token:
1511
return jsonify({"error": "Missing Firebase ID token"}), 401
1612

1713
try:
1814
decoded_token = auth.verify_id_token(id_token)
1915
user_id = decoded_token['uid']
2016

21-
group_data = request.json
22-
if not group_data or 'group_id' not in group_data:
17+
group_id = request.json.get("group_id")
18+
if not group_id:
2319
return jsonify({"error": "Missing 'group_id' in request body"}), 400
2420

25-
group_id = group_data['group_id']
2621
group_ref = db.collection('groups').document(group_id)
27-
group = group_ref.get()
28-
29-
if not group.exists:
30-
return jsonify({"error": "Group not found"}), 404
31-
32-
group_data = group.to_dict()
33-
34-
if group_data['created_by'] != user_id:
35-
return jsonify({"error": "Only the group creator can delete the group"}), 403
36-
37-
group_ref.delete()
38-
return jsonify({"success": True, "message": "Group deleted successfully"}), 200
22+
group_data = group_ref.get()
3923

40-
except auth.InvalidIdTokenError:
41-
return jsonify({"error": "Invalid Firebase ID token"}), 401
24+
if group_data.exists and group_data.to_dict().get("created_by") == user_id:
25+
group_ref.delete()
26+
return jsonify({"success": True, "message": "Group deleted successfully"}), 200
27+
else:
28+
return jsonify({"error": "Group not found or insufficient permissions"}), 403
4229
except Exception as e:
4330
return jsonify({"error": str(e)}), 500

api/auth/group/privacy.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,32 @@
55
db = firestore.client()
66

77
@privacy_group_bp.route('/auth/group/privacy', methods=['PATCH'])
8-
def set_privacy():
9-
"""
10-
Route to set a group's privacy (private or public). Only the group creator can update the privacy.
11-
"""
8+
def update_group_privacy():
129
id_token = request.headers.get('Authorization')
13-
1410
if not id_token:
1511
return jsonify({"error": "Missing Firebase ID token"}), 401
1612

1713
try:
1814
decoded_token = auth.verify_id_token(id_token)
1915
user_id = decoded_token['uid']
2016

21-
group_data = request.json
22-
if not group_data or 'group_id' not in group_data or 'privacy' not in group_data:
23-
return jsonify({"error": "Missing 'group_id' or 'privacy' in request body"}), 400
17+
data = request.json
18+
group_id = data.get("group_id")
19+
privacy = data.get("privacy")
2420

25-
group_id = group_data['group_id']
26-
privacy = group_data['privacy'].lower()
21+
if not group_id or not privacy:
22+
return jsonify({"error": "Missing 'group_id' or 'privacy' in request body"}), 400
2723

28-
if privacy not in ['private', 'public']:
29-
return jsonify({"error": "Invalid privacy value. Must be 'private' or 'public'"}), 400
24+
if privacy not in ["private", "public"]:
25+
return jsonify({"error": "Invalid privacy setting"}), 400
3026

3127
group_ref = db.collection('groups').document(group_id)
32-
group = group_ref.get()
33-
34-
if not group.exists:
35-
return jsonify({"error": "Group not found"}), 404
36-
37-
group_data = group.to_dict()
38-
39-
if group_data['created_by'] != user_id:
40-
return jsonify({"error": "Only the group creator can update the privacy"}), 403
41-
42-
group_ref.update({"privacy": privacy})
43-
return jsonify({"success": True, "message": f"Group privacy set to {privacy}"}), 200
28+
group_data = group_ref.get()
4429

45-
except auth.InvalidIdTokenError:
46-
return jsonify({"error": "Invalid Firebase ID token"}), 401
30+
if group_data.exists and group_data.to_dict().get("created_by") == user_id:
31+
group_ref.update({"privacy": privacy})
32+
return jsonify({"success": True, "message": "Group privacy updated"}), 200
33+
else:
34+
return jsonify({"error": "Group not found or insufficient permissions"}), 403
4735
except Exception as e:
4836
return jsonify({"error": str(e)}), 500

0 commit comments

Comments
 (0)