From 71d48854b1b798f86e496221af6a94b0bbd333ec Mon Sep 17 00:00:00 2001 From: Arief Luthfi Date: Tue, 17 Mar 2020 22:39:20 +0700 Subject: [PATCH] Chore: Refactor Facebook send reply service Refactor Facebook send reply service --- app/services/facebook/send_reply_service.rb | 25 +++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/app/services/facebook/send_reply_service.rb b/app/services/facebook/send_reply_service.rb index ab535fd0a..ebc469149 100644 --- a/app/services/facebook/send_reply_service.rb +++ b/app/services/facebook/send_reply_service.rb @@ -48,19 +48,26 @@ class Facebook::SendReplyService end def twenty_four_hour_window_over? - last_incoming_message = conversation.messages.incoming.last - - is_after_24_hours = (Time.current - last_incoming_message.created_at) / 3600 >= 24 - - return false unless is_after_24_hours - - return false if last_incoming_message && sent_first_outgoing_message_after_24_hours?(last_incoming_message.id) + return false unless after_24_hours? + return false if last_incoming_and_outgoing_message_after_one_day? true end - def sent_first_outgoing_message_after_24_hours?(last_incoming_message_id) + def last_incoming_and_outgoing_message_after_one_day? + last_incoming_message && sent_first_outgoing_message_after_24_hours? + end + + def after_24_hours? + (Time.current - last_incoming_message.created_at) / 3600 >= 24 + end + + def sent_first_outgoing_message_after_24_hours? # we can send max 1 message after 24 hour window - conversation.messages.outgoing.where('id > ?', last_incoming_message_id).count == 1 + conversation.messages.outgoing.where('id > ?', last_incoming_message.id).count == 1 + end + + def last_incoming_message + conversation.messages.incoming.last end end