Skip to content

Commit 374ca11

Browse files
author
Bob Strahan
committed
Fix section deletion bug in process changes resolver
1 parent 8c00be7 commit 374ca11

File tree

1 file changed

+59
-37
lines changed
  • src/lambda/process_changes_resolver

1 file changed

+59
-37
lines changed

src/lambda/process_changes_resolver/index.py

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,59 +85,81 @@ def handler(event, context):
8585
is_deleted = modified_section.get('isDeleted', False)
8686

8787
if is_deleted:
88-
# Remove the section from the document
89-
logger.info(f"Deleting section: {section_id}")
90-
91-
# Find and remove the section
92-
document.sections = [s for s in document.sections if s.section_id != section_id]
93-
94-
# Clear the extraction data from S3
95-
for section in document.sections:
96-
if section.section_id == section_id and section.extraction_result_uri:
97-
clear_extraction_data(section.extraction_result_uri)
88+
# Find section to delete BEFORE removing it
89+
section_to_delete = None
90+
for s in document.sections:
91+
if s.section_id == section_id:
92+
section_to_delete = s
9893
break
94+
95+
if section_to_delete:
96+
# Clear S3 extraction data before removing section
97+
if section_to_delete.extraction_result_uri:
98+
clear_extraction_data(section_to_delete.extraction_result_uri)
99+
logger.info(f"Cleared extraction data for deleted section: {section_id}")
100+
101+
# Remove section from document
102+
document.sections = [s for s in document.sections if s.section_id != section_id]
103+
logger.info(f"Deleted section: {section_id}")
104+
else:
105+
logger.warning(f"Section {section_id} marked for deletion but not found")
99106

100107
continue
101108

102-
# Find existing section or create new one
103-
existing_section = None
104-
for i, section in enumerate(document.sections):
105-
if section.section_id == section_id:
106-
existing_section = section
107-
break
108-
109-
if existing_section:
110-
# Update existing section
111-
logger.info(f"Updating existing section: {section_id}")
112-
existing_section.classification = classification
113-
existing_section.page_ids = [str(pid) for pid in page_ids]
114-
115-
# Clear extraction data for reprocessing
116-
if existing_section.extraction_result_uri:
117-
clear_extraction_data(existing_section.extraction_result_uri)
118-
existing_section.extraction_result_uri = None
119-
existing_section.attributes = None
120-
121-
# Clear confidence threshold alerts for modified sections
122-
existing_section.confidence_threshold_alerts = []
123-
logger.info(f"Cleared confidence alerts for modified section: {section_id}")
124-
125-
else:
126-
# Create new section
109+
elif is_new:
110+
# Create new section (don't search for existing)
127111
logger.info(f"Creating new section: {section_id}")
128112
new_section = Section(
129113
section_id=section_id,
130114
classification=classification,
131115
confidence=1.0,
132116
page_ids=[str(pid) for pid in page_ids],
133117
extraction_result_uri=None,
134-
attributes=None
118+
attributes=None,
119+
confidence_threshold_alerts=[]
135120
)
136121
document.sections.append(new_section)
122+
123+
else:
124+
# Update existing section
125+
existing_section = None
126+
for section in document.sections:
127+
if section.section_id == section_id:
128+
existing_section = section
129+
break
130+
131+
if existing_section:
132+
logger.info(f"Updating existing section: {section_id}")
133+
existing_section.classification = classification
134+
existing_section.page_ids = [str(pid) for pid in page_ids]
135+
136+
# Clear extraction data for reprocessing
137+
if existing_section.extraction_result_uri:
138+
clear_extraction_data(existing_section.extraction_result_uri)
139+
existing_section.extraction_result_uri = None
140+
existing_section.attributes = None
141+
142+
# Clear confidence threshold alerts for modified sections
143+
existing_section.confidence_threshold_alerts = []
144+
logger.info(f"Cleared confidence alerts for modified section: {section_id}")
145+
else:
146+
logger.warning(f"Section {section_id} marked as update but not found - treating as new")
147+
# Treat as new section if not found
148+
new_section = Section(
149+
section_id=section_id,
150+
classification=classification,
151+
confidence=1.0,
152+
page_ids=[str(pid) for pid in page_ids],
153+
extraction_result_uri=None,
154+
attributes=None,
155+
confidence_threshold_alerts=[]
156+
)
157+
document.sections.append(new_section)
137158

159+
# Only add to modified list if not deleted
138160
modified_section_ids.append(section_id)
139161

140-
# Update page classifications to match section classification
162+
# Update page classifications to match section classification (only if not deleted)
141163
for page_id in page_ids:
142164
page_id_str = str(page_id)
143165
if page_id_str in document.pages:

0 commit comments

Comments
 (0)