Skip to content

Commit 65a32cc

Browse files
authored
Merge pull request #1761 from frappe/mergify/bp/main-hotfix/pr-1760
fix: reverted last response time cal based on customer reply (backport #1760)
2 parents 7efb3b5 + 3857d75 commit 65a32cc

File tree

2 files changed

+14
-65
lines changed

2 files changed

+14
-65
lines changed

crm/fcrm/doctype/crm_service_level_agreement/crm_service_level_agreement.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ def set_first_response_time(self, doc: Document):
9898
end_at = doc.first_responded_on
9999
if not start_at or not end_at:
100100
return
101-
doc.first_response_time = self.calc_elapsed_time(start_at, end_at)
102-
if not doc.last_response_time:
101+
if not doc.first_response_time:
102+
doc.first_response_time = self.calc_elapsed_time(start_at, end_at)
103+
104+
if not doc.last_response_time and doc.first_response_time:
103105
doc.last_response_time = doc.first_response_time
104106

105107
def set_rolling_responses(self, doc: Document):
@@ -116,8 +118,7 @@ def set_rolling_responses(self, doc: Document):
116118
)
117119
elif doc.communication_status != self.get_default_priority():
118120
current_time = now_datetime()
119-
customer_reply_time = self.get_last_customer_reply(doc) or doc.last_responded_on
120-
doc.last_response_time = self.calc_elapsed_time(customer_reply_time, current_time)
121+
doc.last_response_time = self.calc_elapsed_time(doc.last_responded_on, current_time)
121122
doc.last_responded_on = current_time
122123
is_failed = self.is_rolling_response_failed(doc)
123124
doc.append(
@@ -188,15 +189,15 @@ def handle_rolling_sla_status(self, doc: Document):
188189
if not self.rolling_responses or len(doc.rolling_responses) == 0:
189190
return
190191

191-
# Check both the current cycle and past rolling response entries
192-
has_past_failure = any(r.status == "Failed" for r in doc.rolling_responses)
193-
194-
if has_past_failure or self.is_rolling_response_failed(doc):
195-
doc.sla_status = "Failed"
196-
elif doc.communication_status == self.get_default_priority():
197-
doc.sla_status = "Rolling Response Due"
198-
else:
199-
doc.sla_status = "Fulfilled"
192+
is_failed = self.is_rolling_response_failed(doc)
193+
options = {
194+
"Fulfilled": True,
195+
"Rolling Response Due": doc.communication_status == self.get_default_priority(),
196+
"Failed": is_failed,
197+
}
198+
for status in options:
199+
if options[status]:
200+
doc.sla_status = status
200201

201202
def is_rolling_response_failed(self, doc: Document):
202203
if not doc.response_by:
@@ -290,24 +291,6 @@ def calc_elapsed_time(self, start_time, end_time) -> float:
290291

291292
return total_seconds
292293

293-
def get_last_customer_reply(self, doc: Document):
294-
"""Get the communication_date of the last received (customer) message"""
295-
result = frappe.get_all(
296-
"Communication",
297-
filters={
298-
"reference_doctype": doc.doctype,
299-
"reference_name": doc.name,
300-
"sent_or_received": "Received",
301-
"communication_type": "Communication",
302-
},
303-
fields=["communication_date"],
304-
order_by="communication_date desc",
305-
limit=1,
306-
)
307-
if result:
308-
return get_datetime(result[0].communication_date)
309-
return None
310-
311294
def get_priorities(self):
312295
"""
313296
Return priorities related info as a dict. With `priority` as key

crm/fcrm/doctype/crm_service_level_agreement/test_crm_service_level_agreement.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -619,40 +619,6 @@ def test_rolling_response_correct_with_customer_reply_gap(self):
619619
self.assertEqual(len(doc.rolling_responses), 2)
620620
self.assertEqual(doc.rolling_responses[1].status, "Fulfilled")
621621

622-
def test_handle_rolling_sla_status_past_failure_persists(self):
623-
"""Test that a past Failed rolling response keeps sla_status as Failed,
624-
even if the current response_by is in the future."""
625-
sla = create_test_sla_with_priorities(rolling_responses=True)
626-
627-
doc = frappe.get_doc(
628-
{
629-
"doctype": "CRM Lead",
630-
"first_name": "Test Past Failure Persists",
631-
"response_by": add_to_date(now_datetime(), hours=1), # Current deadline in future
632-
"last_responded_on": now_datetime(),
633-
"communication_status": "High", # Non-default (agent replied)
634-
"sla_status": None,
635-
}
636-
)
637-
# Past rolling response that failed
638-
doc.append(
639-
"rolling_responses",
640-
{
641-
"status": "Failed",
642-
"response_time": 50000,
643-
"responded_on": add_to_date(now_datetime(), hours=-2),
644-
},
645-
)
646-
# Current rolling response that was fulfilled
647-
doc.append(
648-
"rolling_responses", {"status": "Fulfilled", "response_time": 100, "responded_on": now_datetime()}
649-
)
650-
651-
sla.handle_rolling_sla_status(doc)
652-
653-
# Should still be Failed because of the past failure
654-
self.assertEqual(doc.sla_status, "Failed")
655-
656622
def test_set_rolling_responses_stale_deadline_marks_failed(self):
657623
"""Test that when response_by is a stale past deadline (from a previous cycle),
658624
the agent's response is correctly marked as Failed."""

0 commit comments

Comments
 (0)