Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions audit_management/audit_management/doctype/my_audits/my_audits.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,27 @@ def sync_old_to_new(self):
)

if self.status != "Draft" and old_status and row.status != old_status:
# Prevent fake Responded sync
if old_status == "Responded":
# Never downgrade responded rows if legacy field is lagging
if row.status == "Responded" and old_status in ["Pending", "No Response"]:
pass

# Only sync Responded from legacy if a valid response exists
elif old_status == "Responded":
if old_resp and str(old_resp).strip():
row.status = old_status

# Normal sync for other cases (e.g., Pending -> No Response)
else:
row.status = old_status

# Ensure we only sync response if it doesn't already exist in child table
if (
old_resp
and str(old_resp).strip()
and not row.response
and (
not row.response
or not str(row.response).strip()
)
):
row.response = old_resp

Expand Down Expand Up @@ -277,8 +286,8 @@ def sync_new_to_old(self):
self.set(f"{prefix}_name", row.employee_name)
self.set(f"{prefix}_mail", row.email)

# Update status only if it's currently empty in legacy fields
if not self.get(f"{prefix}_user_status"):
# Update status if different
if self.get(f"{prefix}_user_status") != row.status:
self.set(f"{prefix}_user_status", row.status)

self.set(f"{prefix}_response_box", row.response)
Expand Down
3 changes: 2 additions & 1 deletion audit_management/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ audit_management.patches.migrate_production_data_v4
audit_management.patches.migrate_hardcoded_to_child_table
audit_management.patches.update_audit_level_branch_fields
audit_management.patches.fix_status_mispelling
audit_management.patches.fix_fake_responded_status
audit_management.patches.fix_fake_responded_status
audit_management.patches.fix_incorrect_responded_status
111 changes: 111 additions & 0 deletions audit_management/patches/fix_incorrect_responded_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import frappe


def execute():
"""
Patch to fix incorrect Pending / No Response statuses.

Converts:
- Pending -> Responded if valid response exists.

"""

mapping = {
"BM": "bm",
"DH": "dh",
"COM": "com",
"RM": "rm",
"ROM": "rom",
"ZM": "zm",
"ZOM": "zom",
"GM": "gm",
"HR": "hr",
"CHRO": "chro",
"COO": "coo",
"CFO": "cfo",
"CEO": "ceo",
}

audit_docs = frappe.get_all(
"My Audits",
pluck="name"
)

count = 0

for docname in audit_docs:

try:
doc = frappe.get_doc("My Audits", docname)

for row in doc.audit_stages:

response_text = (row.response or "").strip()

is_valid_response = (
response_text
and response_text not in [
"<p><br></p>",
"<div><br></div>",
"<p></p>",
"<div></div>",
]
)

if (
row.status == "Pending"
and is_valid_response
):

# -----------------------------
# Update Child Table
# -----------------------------
frappe.db.set_value(
"Audit Items",
row.name,
{
"status": "Responded",
},
update_modified=False,
)

# -----------------------------
# Sync Legacy Fields
# -----------------------------
prefix = mapping.get(
(row.stage_name or "").strip().upper()
)

if prefix:
frappe.db.set_value(
"My Audits",
doc.name,
{
f"{prefix}_user_status": "Responded",
},
update_modified=False,
)

count += 1

frappe.log_error(
title="Patch Fix: Pending To Responded",
message=(
f"Audit: {docname}\n"
f"Stage: {row.stage_name}\n"
f"User: {row.user_id}\n"
f"Row ID: {row.name}"
),
)

except Exception:
frappe.log_error(
frappe.get_traceback(),
f"Patch Failed For Audit: {docname}"
)

frappe.db.commit()

frappe.logger().info(
f"Patch complete. {count} audit stage records updated."
)
Loading