Ali2206 commited on
Commit
26a92dc
·
1 Parent(s): bce5673

Convert to shared checklist system - All users can edit the same template - Add template-specific CRUD endpoints - Remove user-specific checklist requirement - Collaborative editing for all users

Browse files
Files changed (1) hide show
  1. main.py +277 -0
main.py CHANGED
@@ -883,6 +883,283 @@ async def get_default_checklist_template():
883
  detail=f"Failed to retrieve checklist template: {str(e)}"
884
  )
885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
886
  @app.post("/api/checklist/{user_id}/item", response_model=Dict[str, Any])
887
  async def add_checklist_item(user_id: str, item_data: dict):
888
  """
 
883
  detail=f"Failed to retrieve checklist template: {str(e)}"
884
  )
885
 
886
+ @app.post("/api/checklist-template/item", response_model=Dict[str, Any])
887
+ async def add_template_item(item_data: dict):
888
+ """
889
+ Add a new item to the shared checklist template
890
+
891
+ Args:
892
+ item_data: Dictionary containing item information (sectionId, requirement, etc.)
893
+
894
+ Returns:
895
+ Dictionary confirming the item was added
896
+
897
+ Raises:
898
+ HTTPException: If database operation fails
899
+ """
900
+ try:
901
+ logger.info(f"Adding new item to shared template")
902
+
903
+ # Get or create the default template
904
+ template = await db.checklist_templates.find_one({"templateId": "default"})
905
+
906
+ if not template:
907
+ raise HTTPException(
908
+ status_code=status.HTTP_404_NOT_FOUND,
909
+ detail="Template not found"
910
+ )
911
+
912
+ # Find the section and add the new item
913
+ section_id = item_data.get('sectionId')
914
+ section_index = next((i for i, s in enumerate(template['sections']) if s['id'] == section_id), None)
915
+
916
+ if section_index is None:
917
+ raise HTTPException(
918
+ status_code=status.HTTP_404_NOT_FOUND,
919
+ detail=f"Section {section_id} not found"
920
+ )
921
+
922
+ new_item = {
923
+ "id": item_data.get('id', f"I{section_id[-1]}.{len(template['sections'][section_index]['items'])+1}"),
924
+ "requirement": item_data.get('requirement', ''),
925
+ }
926
+
927
+ # Update the template
928
+ result = await db.checklist_templates.update_one(
929
+ {"templateId": "default", "sections.id": section_id},
930
+ {"$push": {"sections.$.items": new_item}}
931
+ )
932
+
933
+ if result.modified_count > 0:
934
+ logger.info(f"Successfully added item to template")
935
+ return {
936
+ "success": True,
937
+ "message": "Item added successfully",
938
+ "data": new_item
939
+ }
940
+ else:
941
+ raise HTTPException(
942
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
943
+ detail="Failed to add item"
944
+ )
945
+
946
+ except HTTPException:
947
+ raise
948
+ except Exception as e:
949
+ logger.error(f"Error adding item to template: {e}")
950
+ raise HTTPException(
951
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
952
+ detail=f"Failed to add item: {str(e)}"
953
+ )
954
+
955
+ @app.put("/api/checklist-template/item/{item_id}", response_model=Dict[str, Any])
956
+ async def update_template_item(item_id: str, item_data: dict):
957
+ """
958
+ Update an existing item in the shared checklist template
959
+
960
+ Args:
961
+ item_id: ID of the item to update
962
+ item_data: Dictionary containing updated item information
963
+
964
+ Returns:
965
+ Dictionary confirming the item was updated
966
+
967
+ Raises:
968
+ HTTPException: If database operation fails
969
+ """
970
+ try:
971
+ logger.info(f"Updating item {item_id} in shared template")
972
+
973
+ # Prepare update fields
974
+ update_fields = {}
975
+ if 'requirement' in item_data:
976
+ update_fields["sections.$[].items.$[item].requirement"] = item_data['requirement']
977
+
978
+ if not update_fields:
979
+ raise HTTPException(
980
+ status_code=status.HTTP_400_BAD_REQUEST,
981
+ detail="No valid fields to update"
982
+ )
983
+
984
+ # Update the item in template
985
+ result = await db.checklist_templates.update_one(
986
+ {"templateId": "default"},
987
+ {"$set": update_fields},
988
+ array_filters=[{"item.id": item_id}]
989
+ )
990
+
991
+ if result.modified_count > 0:
992
+ logger.info(f"Successfully updated item {item_id} in template")
993
+ return {
994
+ "success": True,
995
+ "message": "Item updated successfully",
996
+ "data": item_data
997
+ }
998
+ else:
999
+ raise HTTPException(
1000
+ status_code=status.HTTP_404_NOT_FOUND,
1001
+ detail="Item not found or no changes made"
1002
+ )
1003
+
1004
+ except HTTPException:
1005
+ raise
1006
+ except Exception as e:
1007
+ logger.error(f"Error updating item {item_id} in template: {e}")
1008
+ raise HTTPException(
1009
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
1010
+ detail=f"Failed to update item: {str(e)}"
1011
+ )
1012
+
1013
+ @app.delete("/api/checklist-template/item/{item_id}", response_model=Dict[str, Any])
1014
+ async def delete_template_item(item_id: str):
1015
+ """
1016
+ Delete an item from the shared checklist template
1017
+
1018
+ Args:
1019
+ item_id: ID of the item to delete
1020
+
1021
+ Returns:
1022
+ Dictionary confirming the item was deleted
1023
+
1024
+ Raises:
1025
+ HTTPException: If database operation fails
1026
+ """
1027
+ try:
1028
+ logger.info(f"Deleting item {item_id} from shared template")
1029
+
1030
+ # Remove the item from the template
1031
+ result = await db.checklist_templates.update_one(
1032
+ {"templateId": "default"},
1033
+ {"$pull": {"sections.$[].items": {"id": item_id}}}
1034
+ )
1035
+
1036
+ if result.modified_count > 0:
1037
+ logger.info(f"Successfully deleted item {item_id} from template")
1038
+ return {
1039
+ "success": True,
1040
+ "message": "Item deleted successfully"
1041
+ }
1042
+ else:
1043
+ raise HTTPException(
1044
+ status_code=status.HTTP_404_NOT_FOUND,
1045
+ detail="Item not found"
1046
+ )
1047
+
1048
+ except HTTPException:
1049
+ raise
1050
+ except Exception as e:
1051
+ logger.error(f"Error deleting item {item_id} from template: {e}")
1052
+ raise HTTPException(
1053
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
1054
+ detail=f"Failed to delete item: {str(e)}"
1055
+ )
1056
+
1057
+ @app.post("/api/checklist-template/section", response_model=Dict[str, Any])
1058
+ async def add_template_section(section_data: dict):
1059
+ """
1060
+ Add a new section to the shared checklist template
1061
+
1062
+ Args:
1063
+ section_data: Dictionary containing section information
1064
+
1065
+ Returns:
1066
+ Dictionary confirming the section was added
1067
+
1068
+ Raises:
1069
+ HTTPException: If database operation fails
1070
+ """
1071
+ try:
1072
+ logger.info(f"Adding new section to shared template")
1073
+
1074
+ # Get the template
1075
+ template = await db.checklist_templates.find_one({"templateId": "default"})
1076
+
1077
+ if not template:
1078
+ raise HTTPException(
1079
+ status_code=status.HTTP_404_NOT_FOUND,
1080
+ detail="Template not found"
1081
+ )
1082
+
1083
+ # Create new section
1084
+ new_section = {
1085
+ "id": section_data.get('id', f"S{len(template['sections'])+1}"),
1086
+ "title": section_data.get('title', 'New Section'),
1087
+ "icon": section_data.get('icon', 'Settings'),
1088
+ "items": []
1089
+ }
1090
+
1091
+ # Add the new section
1092
+ result = await db.checklist_templates.update_one(
1093
+ {"templateId": "default"},
1094
+ {"$push": {"sections": new_section}}
1095
+ )
1096
+
1097
+ if result.modified_count > 0:
1098
+ logger.info(f"Successfully added section to template")
1099
+ return {
1100
+ "success": True,
1101
+ "message": "Section added successfully",
1102
+ "data": new_section
1103
+ }
1104
+ else:
1105
+ raise HTTPException(
1106
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
1107
+ detail="Failed to add section"
1108
+ )
1109
+
1110
+ except HTTPException:
1111
+ raise
1112
+ except Exception as e:
1113
+ logger.error(f"Error adding section to template: {e}")
1114
+ raise HTTPException(
1115
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
1116
+ detail=f"Failed to add section: {str(e)}"
1117
+ )
1118
+
1119
+ @app.delete("/api/checklist-template/section/{section_id}", response_model=Dict[str, Any])
1120
+ async def delete_template_section(section_id: str):
1121
+ """
1122
+ Delete a section from the shared checklist template
1123
+
1124
+ Args:
1125
+ section_id: ID of the section to delete
1126
+
1127
+ Returns:
1128
+ Dictionary confirming the section was deleted
1129
+
1130
+ Raises:
1131
+ HTTPException: If database operation fails
1132
+ """
1133
+ try:
1134
+ logger.info(f"Deleting section {section_id} from shared template")
1135
+
1136
+ # Remove the section from the template
1137
+ result = await db.checklist_templates.update_one(
1138
+ {"templateId": "default"},
1139
+ {"$pull": {"sections": {"id": section_id}}}
1140
+ )
1141
+
1142
+ if result.modified_count > 0:
1143
+ logger.info(f"Successfully deleted section {section_id} from template")
1144
+ return {
1145
+ "success": True,
1146
+ "message": "Section deleted successfully"
1147
+ }
1148
+ else:
1149
+ raise HTTPException(
1150
+ status_code=status.HTTP_404_NOT_FOUND,
1151
+ detail="Section not found"
1152
+ )
1153
+
1154
+ except HTTPException:
1155
+ raise
1156
+ except Exception as e:
1157
+ logger.error(f"Error deleting section {section_id} from template: {e}")
1158
+ raise HTTPException(
1159
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
1160
+ detail=f"Failed to delete section: {str(e)}"
1161
+ )
1162
+
1163
  @app.post("/api/checklist/{user_id}/item", response_model=Dict[str, Any])
1164
  async def add_checklist_item(user_id: str, item_data: dict):
1165
  """