Fix template ID mismatch - Support both 'default' and 'default-audit-checklist' IDs - Use existing template in database - All CRUD operations now work with existing template
Browse files
main.py
CHANGED
|
@@ -745,8 +745,13 @@ async def ensure_default_template_exists():
|
|
| 745 |
"""
|
| 746 |
try:
|
| 747 |
logger.info("Checking if default template exists...")
|
| 748 |
-
# Check if template exists
|
| 749 |
-
template = await db.checklist_templates.find_one({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 750 |
|
| 751 |
if not template:
|
| 752 |
logger.info("Default template not found, creating it now...")
|
|
@@ -1031,11 +1036,8 @@ async def add_template_item(item_data: dict):
|
|
| 1031 |
try:
|
| 1032 |
logger.info(f"Adding new item to shared template")
|
| 1033 |
|
| 1034 |
-
# Ensure template exists first
|
| 1035 |
-
await ensure_default_template_exists()
|
| 1036 |
-
|
| 1037 |
-
# Get the template
|
| 1038 |
-
template = await db.checklist_templates.find_one({"templateId": "default"})
|
| 1039 |
|
| 1040 |
if not template:
|
| 1041 |
raise HTTPException(
|
|
@@ -1043,6 +1045,8 @@ async def add_template_item(item_data: dict):
|
|
| 1043 |
detail="Template not found"
|
| 1044 |
)
|
| 1045 |
|
|
|
|
|
|
|
| 1046 |
# Find the section and add the new item
|
| 1047 |
section_id = item_data.get('sectionId')
|
| 1048 |
section_index = next((i for i, s in enumerate(template['sections']) if s['id'] == section_id), None)
|
|
@@ -1060,7 +1064,7 @@ async def add_template_item(item_data: dict):
|
|
| 1060 |
|
| 1061 |
# Update the template
|
| 1062 |
result = await db.checklist_templates.update_one(
|
| 1063 |
-
{"templateId":
|
| 1064 |
{"$push": {"sections.$.items": new_item}}
|
| 1065 |
)
|
| 1066 |
|
|
@@ -1104,8 +1108,9 @@ async def update_template_item(item_id: str, item_data: dict):
|
|
| 1104 |
try:
|
| 1105 |
logger.info(f"Updating item {item_id} in shared template")
|
| 1106 |
|
| 1107 |
-
# Ensure template exists first
|
| 1108 |
-
await ensure_default_template_exists()
|
|
|
|
| 1109 |
|
| 1110 |
# Prepare update fields
|
| 1111 |
update_fields = {}
|
|
@@ -1118,9 +1123,9 @@ async def update_template_item(item_id: str, item_data: dict):
|
|
| 1118 |
detail="No valid fields to update"
|
| 1119 |
)
|
| 1120 |
|
| 1121 |
-
# Update the item in template
|
| 1122 |
result = await db.checklist_templates.update_one(
|
| 1123 |
-
{"templateId":
|
| 1124 |
{"$set": update_fields},
|
| 1125 |
array_filters=[{"item.id": item_id}]
|
| 1126 |
)
|
|
@@ -1164,12 +1169,13 @@ async def delete_template_item(item_id: str):
|
|
| 1164 |
try:
|
| 1165 |
logger.info(f"Deleting item {item_id} from shared template")
|
| 1166 |
|
| 1167 |
-
# Ensure template exists first
|
| 1168 |
-
await ensure_default_template_exists()
|
|
|
|
| 1169 |
|
| 1170 |
# Remove the item from the template
|
| 1171 |
result = await db.checklist_templates.update_one(
|
| 1172 |
-
{"templateId":
|
| 1173 |
{"$pull": {"sections.$[].items": {"id": item_id}}}
|
| 1174 |
)
|
| 1175 |
|
|
@@ -1211,11 +1217,8 @@ async def add_template_section(section_data: dict):
|
|
| 1211 |
try:
|
| 1212 |
logger.info(f"Adding new section to shared template")
|
| 1213 |
|
| 1214 |
-
# Ensure template exists first
|
| 1215 |
-
await ensure_default_template_exists()
|
| 1216 |
-
|
| 1217 |
-
# Get the template
|
| 1218 |
-
template = await db.checklist_templates.find_one({"templateId": "default"})
|
| 1219 |
|
| 1220 |
if not template:
|
| 1221 |
raise HTTPException(
|
|
@@ -1223,6 +1226,8 @@ async def add_template_section(section_data: dict):
|
|
| 1223 |
detail="Template not found"
|
| 1224 |
)
|
| 1225 |
|
|
|
|
|
|
|
| 1226 |
# Create new section
|
| 1227 |
new_section = {
|
| 1228 |
"id": section_data.get('id', f"S{len(template['sections'])+1}"),
|
|
@@ -1233,7 +1238,7 @@ async def add_template_section(section_data: dict):
|
|
| 1233 |
|
| 1234 |
# Add the new section
|
| 1235 |
result = await db.checklist_templates.update_one(
|
| 1236 |
-
{"templateId":
|
| 1237 |
{"$push": {"sections": new_section}}
|
| 1238 |
)
|
| 1239 |
|
|
@@ -1276,12 +1281,13 @@ async def delete_template_section(section_id: str):
|
|
| 1276 |
try:
|
| 1277 |
logger.info(f"Deleting section {section_id} from shared template")
|
| 1278 |
|
| 1279 |
-
# Ensure template exists first
|
| 1280 |
-
await ensure_default_template_exists()
|
|
|
|
| 1281 |
|
| 1282 |
# Remove the section from the template
|
| 1283 |
result = await db.checklist_templates.update_one(
|
| 1284 |
-
{"templateId":
|
| 1285 |
{"$pull": {"sections": {"id": section_id}}}
|
| 1286 |
)
|
| 1287 |
|
|
|
|
| 745 |
"""
|
| 746 |
try:
|
| 747 |
logger.info("Checking if default template exists...")
|
| 748 |
+
# Check if template exists (check both old and new IDs)
|
| 749 |
+
template = await db.checklist_templates.find_one({
|
| 750 |
+
"$or": [
|
| 751 |
+
{"templateId": "default"},
|
| 752 |
+
{"templateId": "default-audit-checklist"}
|
| 753 |
+
]
|
| 754 |
+
})
|
| 755 |
|
| 756 |
if not template:
|
| 757 |
logger.info("Default template not found, creating it now...")
|
|
|
|
| 1036 |
try:
|
| 1037 |
logger.info(f"Adding new item to shared template")
|
| 1038 |
|
| 1039 |
+
# Ensure template exists first and get it
|
| 1040 |
+
template = await ensure_default_template_exists()
|
|
|
|
|
|
|
|
|
|
| 1041 |
|
| 1042 |
if not template:
|
| 1043 |
raise HTTPException(
|
|
|
|
| 1045 |
detail="Template not found"
|
| 1046 |
)
|
| 1047 |
|
| 1048 |
+
template_id = template.get("templateId", "default")
|
| 1049 |
+
|
| 1050 |
# Find the section and add the new item
|
| 1051 |
section_id = item_data.get('sectionId')
|
| 1052 |
section_index = next((i for i, s in enumerate(template['sections']) if s['id'] == section_id), None)
|
|
|
|
| 1064 |
|
| 1065 |
# Update the template
|
| 1066 |
result = await db.checklist_templates.update_one(
|
| 1067 |
+
{"templateId": template_id, "sections.id": section_id},
|
| 1068 |
{"$push": {"sections.$.items": new_item}}
|
| 1069 |
)
|
| 1070 |
|
|
|
|
| 1108 |
try:
|
| 1109 |
logger.info(f"Updating item {item_id} in shared template")
|
| 1110 |
|
| 1111 |
+
# Ensure template exists first and get it
|
| 1112 |
+
template = await ensure_default_template_exists()
|
| 1113 |
+
template_id = template.get("templateId") if template else "default"
|
| 1114 |
|
| 1115 |
# Prepare update fields
|
| 1116 |
update_fields = {}
|
|
|
|
| 1123 |
detail="No valid fields to update"
|
| 1124 |
)
|
| 1125 |
|
| 1126 |
+
# Update the item in template (use the template ID we found)
|
| 1127 |
result = await db.checklist_templates.update_one(
|
| 1128 |
+
{"templateId": template_id},
|
| 1129 |
{"$set": update_fields},
|
| 1130 |
array_filters=[{"item.id": item_id}]
|
| 1131 |
)
|
|
|
|
| 1169 |
try:
|
| 1170 |
logger.info(f"Deleting item {item_id} from shared template")
|
| 1171 |
|
| 1172 |
+
# Ensure template exists first and get it
|
| 1173 |
+
template = await ensure_default_template_exists()
|
| 1174 |
+
template_id = template.get("templateId") if template else "default"
|
| 1175 |
|
| 1176 |
# Remove the item from the template
|
| 1177 |
result = await db.checklist_templates.update_one(
|
| 1178 |
+
{"templateId": template_id},
|
| 1179 |
{"$pull": {"sections.$[].items": {"id": item_id}}}
|
| 1180 |
)
|
| 1181 |
|
|
|
|
| 1217 |
try:
|
| 1218 |
logger.info(f"Adding new section to shared template")
|
| 1219 |
|
| 1220 |
+
# Ensure template exists first and get it
|
| 1221 |
+
template = await ensure_default_template_exists()
|
|
|
|
|
|
|
|
|
|
| 1222 |
|
| 1223 |
if not template:
|
| 1224 |
raise HTTPException(
|
|
|
|
| 1226 |
detail="Template not found"
|
| 1227 |
)
|
| 1228 |
|
| 1229 |
+
template_id = template.get("templateId", "default")
|
| 1230 |
+
|
| 1231 |
# Create new section
|
| 1232 |
new_section = {
|
| 1233 |
"id": section_data.get('id', f"S{len(template['sections'])+1}"),
|
|
|
|
| 1238 |
|
| 1239 |
# Add the new section
|
| 1240 |
result = await db.checklist_templates.update_one(
|
| 1241 |
+
{"templateId": template_id},
|
| 1242 |
{"$push": {"sections": new_section}}
|
| 1243 |
)
|
| 1244 |
|
|
|
|
| 1281 |
try:
|
| 1282 |
logger.info(f"Deleting section {section_id} from shared template")
|
| 1283 |
|
| 1284 |
+
# Ensure template exists first and get it
|
| 1285 |
+
template = await ensure_default_template_exists()
|
| 1286 |
+
template_id = template.get("templateId") if template else "default"
|
| 1287 |
|
| 1288 |
# Remove the section from the template
|
| 1289 |
result = await db.checklist_templates.update_one(
|
| 1290 |
+
{"templateId": template_id},
|
| 1291 |
{"$pull": {"sections": {"id": section_id}}}
|
| 1292 |
)
|
| 1293 |
|