|
| 1 | +# Import the necessary modules. |
| 2 | + |
1 | 3 | from models.task_model import Task
|
2 | 4 | from database.__init__ import conn
|
3 | 5 | from bson.objectid import ObjectId
|
4 | 6 | import app_config as config
|
5 | 7 |
|
6 |
| -# TODO: Manpreet Kaur |
7 | 8 | def create_task(task_info):
|
| 9 | + """ |
| 10 | + Create a new task in the database. |
| 11 | +
|
| 12 | + Args: |
| 13 | + task_info (dict): A dictionary containing the task information. |
| 14 | + Required keys: |
| 15 | + - 'created_by_uid': The ID of the user who created the task. |
| 16 | + - 'assigned_to_uid': The ID of the user to whom the task is assigned. |
| 17 | + - 'description': The description of the task. |
| 18 | +
|
| 19 | + Returns: |
| 20 | + pymongo.results.InsertOneResult: The result of the insert operation. |
| 21 | +
|
| 22 | + Raises: |
| 23 | + ValueError: If the user information is invalid. |
| 24 | + """ |
8 | 25 | try:
|
9 |
| - # Access the database to get user information |
10 |
| - created_by_user = conn.database[config.CONST_USER_COLLECTION].find_one({'_id': ObjectId(task_info['created_by_uid'])}) |
11 |
| - assigned_to_user = conn.database[config.CONST_USER_COLLECTION].find_one({'_id': ObjectId(task_info['assigned_to_uid'])}) |
12 |
| - |
13 |
| - # print("created_by_user", created_by_user) |
14 |
| - # print("assigned_to_user", assigned_to_user) |
| 26 | + # Find the users by their IDs |
| 27 | + created_by_user = conn.database[config.CONST_USER_COLLECTION].find_one( |
| 28 | + {'_id': ObjectId(task_info['created_by_uid'])}) |
| 29 | + assigned_to_user = conn.database[config.CONST_USER_COLLECTION].find_one( |
| 30 | + {'_id': ObjectId(task_info['assigned_to_uid'])}) |
15 | 31 |
|
| 32 | + # Check if the user information is valid |
16 | 33 | if not created_by_user or not assigned_to_user:
|
17 | 34 | raise ValueError('Invalid user information')
|
18 | 35 |
|
19 | 36 | # Create a new task object
|
20 | 37 | new_task = Task()
|
21 |
| - new_task.createdByUid = task_info['created_by_uid'] |
22 |
| - new_task.createdByName = created_by_user['name'] |
23 |
| - new_task.assignedToUid = task_info['assigned_to_uid'] |
24 |
| - new_task.assignedToName = assigned_to_user['name'] |
25 |
| - new_task.description = task_info['description'] |
| 38 | + new_task.createdByUid = task_info['created_by_uid'] # Set the created by user ID |
| 39 | + new_task.createdByName = created_by_user['name'] # Set the created by user name |
| 40 | + new_task.assignedToUid = task_info['assigned_to_uid'] # Set the assigned to user ID |
| 41 | + new_task.assignedToName = assigned_to_user['name'] # Set the assigned to user name |
| 42 | + new_task.description = task_info['description'] # Set the description of the task |
26 | 43 |
|
27 | 44 | # Save the task to the database
|
28 | 45 | created_task = conn.database[config.CONST_TASK_COLLECTION].insert_one(new_task.__dict__)
|
29 | 46 |
|
30 | 47 | return created_task
|
31 | 48 |
|
32 |
| - except ValueError as err: |
| 49 | + except ValueError as err: # If there is an invalid user information, raise a ValueError |
33 | 50 | raise ValueError(str(err))
|
34 |
| - except Exception as err: |
| 51 | + except Exception as err: # If there is any other exception, raise a ValueError |
35 | 52 | raise ValueError(str(err))
|
36 | 53 |
|
37 |
| -# TODO: Dil Raval |
38 | 54 | def get_task_created_by_user(user_id):
|
| 55 | + """ |
| 56 | + Get tasks created by the user. |
| 57 | +
|
| 58 | + This function retrieves tasks created by the user with the given user ID. |
| 59 | +
|
| 60 | + Args: |
| 61 | + user_id (str): The ID of the user. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + list: A list of tasks created by the user, with each task's '_id' field converted to a string. |
| 65 | +
|
| 66 | + Raises: |
| 67 | + ValueError: If there is an error in fetching the tasks. |
| 68 | + """ |
39 | 69 | try:
|
40 |
| - |
| 70 | + # Connect to the tasks collection of the database. |
41 | 71 | task_collection = conn.database.get_collection(config.CONST_TASK_COLLECTION)
|
42 | 72 |
|
| 73 | + # Retrieve tasks created by the user. |
| 74 | + # Find tasks where the 'createdByUid' field matches the given user ID. |
43 | 75 | task_list = list(task_collection.find({"createdByUid": user_id}))
|
44 |
| - # print(task_list) |
45 | 76 |
|
| 77 | + # Convert the '_id' field of each task to a string. |
| 78 | + # This is done to ensure that the '_id' field is serialized as a string when returned. |
46 | 79 | for task in task_list:
|
47 | 80 | task["_id"] = str(task["_id"])
|
48 | 81 |
|
49 |
| - return task_list |
| 82 | + return task_list # Return the list of tasks created by the user |
50 | 83 |
|
51 | 84 | except Exception as error:
|
| 85 | + # Raise a ValueError with an appropriate error message if there is an error in fetching the tasks. |
52 | 86 | raise ValueError("Error on trying to fetch tasks created by user.", error)
|
53 | 87 |
|
54 |
| -# TODO: Aryan Handa |
55 | 88 | def get_tasks_assigned_to_user(assignedToUid):
|
| 89 | + """ |
| 90 | + Get tasks assigned to the user. |
| 91 | +
|
| 92 | + This function retrieves tasks assigned to the user with the given assignedToUid. |
| 93 | +
|
| 94 | + Args: |
| 95 | + assignedToUid (str): The ID of the user. |
| 96 | +
|
| 97 | + Returns: |
| 98 | + list: A list of tasks assigned to the user, with each task's '_id' field converted to a string. |
| 99 | +
|
| 100 | + Raises: |
| 101 | + ValueError: If there is an error in fetching the tasks. |
| 102 | + """ |
56 | 103 | try:
|
57 |
| - |
58 | 104 | # Retrieve tasks assigned to the user
|
59 |
| - tasks = conn.database[config.CONST_TASK_COLLECTION].find({"assignedToUid": assignedToUid}) |
| 105 | + tasks = conn.database[config.CONST_TASK_COLLECTION].find( |
| 106 | + {"assignedToUid": assignedToUid}) # Find tasks assigned to the user |
60 | 107 |
|
| 108 | + # Convert the tasks to a list |
61 | 109 | user_tasks = list(tasks)
|
62 | 110 |
|
| 111 | + # Convert the '_id' field of each task to a string |
63 | 112 | for task in user_tasks:
|
64 |
| - task["_id"] = str(task["_id"]) |
| 113 | + task["_id"] = str(task["_id"]) # Convert task ID to string |
65 | 114 |
|
66 |
| - return user_tasks |
| 115 | + return user_tasks # Return the list of tasks assigned to the user |
67 | 116 |
|
68 | 117 | except Exception as err:
|
| 118 | + # Raise a ValueError with an appropriate error message if there is an error in fetching the tasks. |
69 | 119 | raise ValueError("Error fetching tasks assigned to user: ", err)
|
70 | 120 |
|
71 |
| -# TODO: Payal Rangra |
72 |
| -def update_task(user_info, task_id, done ): |
73 |
| - |
| 121 | +def update_task(user_info, task_id, done): |
| 122 | + """ |
| 123 | + Update the status of a task in the database. |
| 124 | +
|
| 125 | + Args: |
| 126 | + user_info (dict): A dictionary containing user-related information retrieved from the token, including the user's ID. |
| 127 | + task_id (str): The ID of the task to be updated. |
| 128 | + done (bool): The new status of the task. |
| 129 | +
|
| 130 | + Returns: |
| 131 | + pymongo.results.UpdateResult: The result of the update operation. |
| 132 | +
|
| 133 | + Raises: |
| 134 | + ValueError: If the task is not found or the user is not authorized to update the task. |
| 135 | + """ |
74 | 136 | try:
|
| 137 | + # Connect to the tasks collection of the database. |
75 | 138 | task_collection = conn.database[config.CONST_TASK_COLLECTION]
|
76 |
| - |
77 |
| - current_task = task_collection.find_one({"_id":ObjectId(task_id)}) |
78 |
| - |
79 |
| - if(current_task == None): |
| 139 | + |
| 140 | + # Find the task with _id = task_id. |
| 141 | + current_task = task_collection.find_one({"_id": ObjectId(task_id)}) |
| 142 | + |
| 143 | + # Check if the task exists. |
| 144 | + if current_task is None: |
80 | 145 | raise ValueError('Task not found')
|
81 |
| - |
| 146 | + |
| 147 | + # Check if the user is authorized to update the task. |
82 | 148 | if str(current_task['assignedToUid']) != str(user_info["id"]):
|
83 | 149 | raise ValueError('Users can only change status when task is assigned to them.')
|
84 |
| - |
85 |
| - updated_result = task_collection.update_one({"_id":ObjectId(task_id)}, {"$set": {"done": done}}) |
86 |
| - |
| 150 | + |
| 151 | + # Update the 'done' field of the task with _id = task_id to the new status. |
| 152 | + # The $set operator is used to update the value of the 'done' field. |
| 153 | + updated_result = task_collection.update_one( |
| 154 | + {"_id": ObjectId(task_id)}, # Query to find the task |
| 155 | + {"$set": {"done": done}} # Update operation to set the 'done' field |
| 156 | + ) |
| 157 | + |
| 158 | + # Return the result of the update operation. |
87 | 159 | return updated_result
|
88 |
| - |
| 160 | + |
89 | 161 | except Exception as err:
|
| 162 | + # Raise a ValueError with an appropriate error message if there is an error in updating the task. |
90 | 163 | raise ValueError('Error on updating task: ' f'{err}')
|
91 | 164 |
|
92 |
| -# TODO: Viraj Patel |
93 | 165 | def delete_task(user_information, task_id):
|
94 | 166 | """Delete a task from the database.
|
95 | 167 |
|
96 | 168 | Args:
|
97 | 169 | user_information (dict): A dictionary containing user-related information retrieved from the token, including the user's ID.
|
98 | 170 | task_id (str): The ID of the task to be deleted.
|
| 171 | +
|
| 172 | + Returns: |
| 173 | + pymongo.results.DeleteResult: The result of the delete operation. |
| 174 | +
|
| 175 | + Raises: |
| 176 | + ValueError: If the task is not found or the user is not authorized to delete the task. |
99 | 177 | """
|
100 | 178 | try:
|
101 | 179 |
|
|
0 commit comments