Skip to content

Commit 6e5740d

Browse files
authored
Merge pull request #10 from ChingEnLin/fix/update_doc
Fix/update doc
2 parents 9eb1fd1 + 8e9f57e commit 6e5740d

File tree

3 files changed

+831
-3
lines changed

3 files changed

+831
-3
lines changed

backend/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pytest
1414
pytest-asyncio
1515
pytest-cov
1616
httpx
17+
mongomock
18+
pytest-mock
19+
freezegun
1720

1821
# Static code analysis
1922
flake8

backend/services/data_documents_service.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,18 @@ def update_document(
207207
except Exception:
208208
pass
209209
before_doc = collection.find_one({"_id": ObjectId(document_id)})
210-
result = collection.update_one(
211-
{"_id": ObjectId(document_id)}, {"$set": content}
212-
)
210+
if not before_doc:
211+
return None
212+
213+
# Preserve datetime_creation from the existing document
214+
if "datetime_creation" in before_doc:
215+
content["datetime_creation"] = before_doc["datetime_creation"]
216+
217+
# Always update datetime_last_modified to current time
218+
content["datetime_last_modified"] = datetime.now(timezone.utc)
219+
220+
# Use replace_one to completely replace the document while preserving system fields
221+
result = collection.replace_one({"_id": ObjectId(document_id)}, content)
213222
if result.matched_count == 0:
214223
return None
215224
updated_doc = collection.find_one({"_id": ObjectId(document_id)})

0 commit comments

Comments
 (0)