Skip to content
Merged
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
51 changes: 51 additions & 0 deletions backend/controller_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,57 @@ def get_group_control(
}
return groups

def get_group_members_control(
self,
user_id: str,
group_id: str,
password: str,
) -> list[dict]:
"""Gets all members of a specific group."""
if not Validator().check_user_exists(user_id):
raise BackendError("Backend Error: User does not exist", "304")
if not Validator().check_password(user_id, password):
raise BackendError("Backend Error: Password is incorrect", "305")
if not Validator().check_group_exists(group_id):
raise BackendError("Backend Error: Group does not exist", "306")
if not Validator().check_user_in_group(user_id, group_id):
raise BackendError("Backend Error: User not in group", "308")

with db_operation() as data_cursor:
# Get all users in the group
data_cursor.execute(
"""
SELECT user_id
FROM group_user
WHERE group_id = ?
""",
(group_id,),
)
user_ids = [row[0] for row in data_cursor.fetchall()]

if not user_ids:
return []

# Get user information for each member
placeholders = ",".join("?" for _ in user_ids)
data_cursor.execute(
f"""
SELECT uuid, username, email
FROM user
WHERE uuid IN ({placeholders})
""",
user_ids,
)
members_data = data_cursor.fetchall()

# Create a list of member dictionaries
members = [
{"user_id": member_id, "username": member_name, "email": member_email}
for member_id, member_name, member_email in members_data
]

return members


if __name__ == "__main__":
print("This is a module and should not be run directly.")
56 changes: 39 additions & 17 deletions backend/controller_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_invite_control(
with db_operation() as data_cursor:
data_cursor.execute(
"INSERT INTO group_invites VALUES (?, ?, ?, ?, ?);",
(invite_id, group_id, invitee_id, inviter_id, day_created),
(invite_id, group_id, inviter_id, invitee_id, day_created),
)
return invite_id

Expand All @@ -59,37 +59,59 @@ def get_pending_control(
raise BackendError("Backend Error: Password is incorrect", "305")
invites: dict[str, dict] = {}
with db_operation() as data_cursor:
# 1. Fetch all raw invite data
data_cursor.execute(
(
"SELECT invite_id, inviter_id group_id, created_at "
"FROM group_invites WHERE invitee_id = ?;"
),
"SELECT invite_id, inviter_id, group_id "
"FROM group_invites WHERE invitee_id = ?;",
(user_id,),
)
invites_data: list[tuple] = data_cursor.fetchall()
for invite_item in invites_data:
invite_id: str = invite_item[0]
inviter_id: str = invite_item[1]
group_id: str = invite_item[2]
created_at: str = invite_item[3]

if not invites_data:
return {} # Return early if no invites

# 2. Collect unique IDs
group_ids = {item[2] for item in invites_data}
inviter_ids = {item[1] for item in invites_data}

# 3. Fetch group names
group_names: dict[str, str] = {}
if group_ids:
group_placeholders = ",".join("?" * len(group_ids))
data_cursor.execute(
"SELECT name FROM task_group WHERE uuid = ?;",
(group_id),
f"SELECT uuid, name FROM task_group WHERE uuid IN ({group_placeholders});",
list(group_ids),
)
group_name: str = data_cursor.fetchone()[0]
group_names = dict(data_cursor.fetchall())

# 4. Fetch inviter names
inviter_names: dict[str, str] = {}
if inviter_ids:
inviter_placeholders = ",".join("?" * len(inviter_ids))
data_cursor.execute(
"SELECT username FROM user WHERE uuid = ?;",
(inviter_id),
f"SELECT uuid, username FROM user WHERE uuid IN ({inviter_placeholders});",
list(inviter_ids),
)
inviter_name: str = data_cursor.fetchone()[0]
inviter_names = dict(data_cursor.fetchall())

# 5. Build the final result dictionary
for invite_item in invites_data:
invite_id: str = invite_item[0]
inviter_id: str = invite_item[1]
group_id: str = invite_item[2]

# Look up names from the fetched dictionaries
group_name: str = group_names.get(group_id, "Unknown Group")
inviter_name: str = inviter_names.get(inviter_id, "Unknown User")

invites[invite_id] = {
"invite_id": invite_id,
"group_id": group_id,
"group_name": group_name,
"inviter_id": inviter_id,
"inviter_name": inviter_name,
"created_at": created_at,
}

return invites

def sent_invite_control(
Expand Down
79 changes: 66 additions & 13 deletions backend/controller_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def edit_task_control(
request_data["group_id"]
):
raise BackendError("Backend Error: Group does not exist", "306")
if Validator().check_password(
user_id=request_data["assigner_id"], password=request_data["password"]
):
if not Validator().check_password(user_id=assigner_id, password=password):
raise BackendError("Backend Error: Password is incorrect", "305")
image_path = "TODO CHANGE HERE"
task_due: float = datetime(
Expand All @@ -99,8 +97,8 @@ def edit_task_control(
with db_operation() as data_cursor:
data_cursor.execute(
"UPDATE task SET name = ?, description = ?, due = ?, est_day = ?, "
"est_hour = ?, est_min = ?, assigner_uuid = ?, assign_uuid = ?, group_id = ? "
"recursive = ?, priority = ?, image_path = ? completed = ? "
"est_hour = ?, est_min = ?, assigner_uuid = ?, assign_uuid = ?, group_id = ?, "
"recursive = ?, priority = ?, image_path = ?, completed = ? "
"WHERE uuid = ?;",
(
request_data["task_name"],
Expand Down Expand Up @@ -146,17 +144,35 @@ def get_user_task_control(self, user_id: str, password: str) -> dict[str, dict]:
task_list: list[tuple] = data_cursor.fetchall()
new_task_list: dict[str, dict] = {}
for task in task_list:
# Get assigner username
assigner_username = "Unknown"
with db_operation() as username_cursor:
username_cursor.execute("SELECT username FROM user WHERE uuid = ?;", (task[7],))
username_result = username_cursor.fetchone()
if username_result:
assigner_username = username_result[0]

new_task_list[task[0]] = {
"name": task[1],
"description": task[2],
"due": datetime.fromtimestamp(float(task[3])),
"due_timestamp": float(task[3]),
"est_day": int(task[4]),
"est_hour": int(task[5]),
"est_min": int(task[6]),
"assigner_id": task[7],
"assigner_username": assigner_username,
"assign_id": task[8],
"group_id": task[9],
"completed": bool(task[10]),
<<<<<<< Updated upstream
"priority": int(task[11]) if len(task) > 11 else 0,
"recursive": int(task[12]) if len(task) > 12 else 0,
"image_path": task[13] if len(task) > 13 else "",
=======
"priority": int(task[11]),
"recursive": int(task[12]),
"image_path": task[13],
>>>>>>> Stashed changes
}
return new_task_list

Expand All @@ -183,17 +199,35 @@ def get_group_task_control(
task_list: list[tuple] = data_cursor.fetchall()
new_task_list: dict[str, dict] = {}
for task in task_list:
# Get assigner username
assigner_username = "Unknown"
with db_operation() as username_cursor:
username_cursor.execute("SELECT username FROM user WHERE uuid = ?;", (task[7],))
username_result = username_cursor.fetchone()
if username_result:
assigner_username = username_result[0]

new_task_list[task[0]] = {
"name": task[1],
"description": task[2],
"due": datetime.fromtimestamp(float(task[3])),
"due_timestamp": float(task[3]),
"est_day": int(task[4]),
"est_hour": int(task[5]),
"est_min": int(task[6]),
"assigner_id": task[7],
"assigner_username": assigner_username,
"assign_id": task[8],
"group_id": task[9],
"completed": bool(task[10]),
<<<<<<< Updated upstream
"priority": int(task[11]) if len(task) > 11 else 0,
"recursive": int(task[12]) if len(task) > 12 else 0,
"image_path": task[13] if len(task) > 13 else "",
=======
"priority": int(task[11]),
"recursive": int(task[12]),
"image_path": task[13],
>>>>>>> Stashed changes
}
return new_task_list

Expand Down Expand Up @@ -225,25 +259,42 @@ def get_completed_task_control(
task_list: list[tuple] = data_cursor.fetchall()
new_task_list: dict[str, dict] = {}
for task in task_list:
# Get assigner username
assigner_username = "Unknown"
with db_operation() as username_cursor:
username_cursor.execute("SELECT username FROM user WHERE uuid = ?;", (task[7],))
username_result = username_cursor.fetchone()
if username_result:
assigner_username = username_result[0]

new_task_list[task[0]] = {
"name": task[1],
"description": task[2],
"due": datetime.fromtimestamp(float(task[3])),
"due_timestamp": float(task[3]),
"est_day": int(task[4]),
"est_hour": int(task[5]),
"est_min": int(task[6]),
"assigner_id": task[7],
"assigner_username": assigner_username,
"assign_id": task[8],
"group_id": task[9],
"completed": bool(task[10]),
<<<<<<< Updated upstream
"priority": int(task[11]) if len(task) > 11 else 0,
"recursive": int(task[12]) if len(task) > 12 else 0,
"image_path": task[13] if len(task) > 13 else "",
=======
"priority": int(task[11]),
"recursive": int(task[12]),
"image_path": task[13],
>>>>>>> Stashed changes
}
return new_task_list

def toggle_complete_task_control(
self,
task_id: str,
user_id: str,
group_id: str,
password: str,
completed: int,
) -> None:
Expand All @@ -252,10 +303,6 @@ def toggle_complete_task_control(
raise BackendError("Backend Error: User does not exist", "304")
if not Validator().check_password(user_id=user_id, password=password):
raise BackendError("Backend Error: Password is incorrect", "305")
if not Validator().check_group_exists(group_id=group_id):
raise BackendError("Backend Error: Group does not exist", "306")
if not Validator().check_user_in_group(user_id=user_id, group_id=group_id):
raise BackendError("Backend Error: User is not in the group", "310")
if not Validator().check_task_exists(task_id=task_id):
raise BackendError("Backend Error: Task does not exist", "309")
with db_operation() as data_cursor:
Expand All @@ -267,6 +314,12 @@ def toggle_complete_task_control(
),
)

def get_image_control(self, image_path: str) -> str:
"""This will get the image."""
if not exists(image_path):
raise BackendError("Backend Error: Image does not exist", "313")
return image_path


if __name__ == "__main__":
print("This module is not intended to be run directly.")
13 changes: 8 additions & 5 deletions backend/controller_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ def login_user_control(
self,
email: str,
password: str,
) -> str:
) -> dict[str, str]:
"""This will login a user."""
if Validator().check_login(email=email, password=password):
if not Validator().check_login(email=email, password=password):
raise BackendError("Backend Error: Email or Password is incorrect", "303")
with db_operation() as data_cursor:
data_cursor.execute(
"SELECT uuid FROM user WHERE email = ?;",
"SELECT uuid, username FROM user WHERE email = ?;",
(email,),
)
user_id = data_cursor.fetchone()[0]
return user_id
user_data = data_cursor.fetchone()
if user_data:
return {"user_id": user_data[0], "username": user_data[1]}
else:
raise BackendError("Backend Error: User not found after successful check", "500")

def edit_user_control(
self,
Expand Down
14 changes: 14 additions & 0 deletions backend/handler_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def delete_group_request(self) -> None:
user_id=user_id, group_id=group_id, password=password
)

@handle_backend_exceptions
def get_group_members_request(self) -> list[dict]:
"""Get all members of a specific group."""
request_data = extract_request_data(
request=self.user_request,
required_fields=["user_id", "group_id", "password"],
)
user_id = request_data["user_id"]
group_id = request_data["group_id"]
password = request_data["password"]
return GroupController().get_group_members_control(
user_id=user_id, group_id=group_id, password=password
)


if __name__ == "__main__":
print("This is a module and should not be run directly.")
Loading
Loading