FIX: Partial reply key search in email sent logs.

FIX: Partial reply key search in email sent logs.

Follow up to c85b9c6ed3a847ce0df7d58340d6680536d92b90

diff --git a/app/controllers/admin/email_controller.rb b/app/controllers/admin/email_controller.rb
index 476f0af..b1f28d4 100644
--- a/app/controllers/admin/email_controller.rb
+++ b/app/controllers/admin/email_controller.rb
@@ -32,7 +32,7 @@ class Admin::EmailController < Admin::AdminController
 
     if params[:reply_key].present?
       email_logs = email_logs.where(
-        "CAST (post_reply_keys.reply_key AS VARCHAR) ILIKE ?", "%#{params[:reply_key]}%"
+        "post_reply_keys.reply_key::TEXT ILIKE ?", "%#{params[:reply_key]}%"
       )
     end
 
diff --git a/app/models/email_log.rb b/app/models/email_log.rb
index 769436e..0aeae60 100644
--- a/app/models/email_log.rb
+++ b/app/models/email_log.rb
@@ -73,10 +73,6 @@ class EmailLog < ActiveRecord::Base
       .first
   end
 
-  def bounce_key
-    super&.delete('-')
-  end
-
 end
 
 # == Schema Information
diff --git a/app/models/post_reply_key.rb b/app/models/post_reply_key.rb
index 3f1479f..2c85058 100644
--- a/app/models/post_reply_key.rb
+++ b/app/models/post_reply_key.rb
@@ -8,12 +8,8 @@ class PostReplyKey < ActiveRecord::Base
   validates :user_id, presence: true
   validates :reply_key, presence: true
 
-  def reply_key
-    super&.delete('-')
-  end
-
   def self.generate_reply_key
-    SecureRandom.hex(16)
+    SecureRandom.uuid
   end
 end
 
diff --git a/app/serializers/email_log_serializer.rb b/app/serializers/email_log_serializer.rb
index 75c7010..e4d120a 100644
--- a/app/serializers/email_log_serializer.rb
+++ b/app/serializers/email_log_serializer.rb
@@ -12,6 +12,6 @@ class EmailLogSerializer < ApplicationSerializer
   end
 
   def reply_key
-    @options[:reply_keys][[object.post_id, object.user_id]].delete("-")
+    @options[:reply_keys][[object.post_id, object.user_id]]
   end
 end
diff --git a/spec/models/email_log_spec.rb b/spec/models/email_log_spec.rb
index f0f60da..6124d32 100644
--- a/spec/models/email_log_spec.rb
+++ b/spec/models/email_log_spec.rb
@@ -103,8 +103,7 @@ describe EmailLog do
         .pluck("bounce_key::text")
         .first
 
-      expect(raw_key).to_not eq(hex)
-      expect(raw_key.delete('-')).to eq(hex)
+      expect(raw_key).to eq(hex)
       expect(EmailLog.find(email_log.id).bounce_key).to eq(hex)
     end
   end
diff --git a/spec/models/post_reply_key_spec.rb b/spec/models/post_reply_key_spec.rb
index 631e6cd..45f6d5e 100644
--- a/spec/models/post_reply_key_spec.rb
+++ b/spec/models/post_reply_key_spec.rb
@@ -3,7 +3,7 @@ require 'rails_helper'
 RSpec.describe PostReplyKey do
   describe "#reply_key" do
     it "should format the reply_key correctly" do
-      hex = SecureRandom.hex
+      hex = SecureRandom.uuid
       post_reply_key = Fabricate(:post_reply_key,
         reply_key: hex
       )
@@ -12,8 +12,7 @@ RSpec.describe PostReplyKey do
         .pluck("reply_key::text")
         .first
 
-      expect(raw_key).to_not eq(hex)
-      expect(raw_key.delete('-')).to eq(hex)
+      expect(raw_key).to eq(hex)
       expect(PostReplyKey.find(post_reply_key.id).reply_key).to eq(hex)
     end
   end
diff --git a/spec/requests/admin/email_controller_spec.rb b/spec/requests/admin/email_controller_spec.rb
index 34b2faf..8bee246 100644
--- a/spec/requests/admin/email_controller_spec.rb
+++ b/spec/requests/admin/email_controller_spec.rb
@@ -58,6 +58,32 @@ describe Admin::EmailController do
       expect(log["id"]).to eq(email_log.id)
       expect(log["reply_key"]).to eq(post_reply_key.reply_key)
     end
+
+    it 'should be able to filter by reply key' do
+      email_log_2 = Fabricate(:email_log, post: post)
+
+      post_reply_key_2 = Fabricate(:post_reply_key,
+        post: post,
+        user: email_log_2.user,
+        reply_key: "2d447423-c625-4fb9-8717-ff04ac60eee8"
+      )
+
+      [
+        "17-ff04",
+        "2d447423-c625-4fb9-8717-ff04ac60eee8"
+      ].each do |reply_key|
+        get "/admin/email/sent.json", params: {
+          reply_key: reply_key
+        }
+
+        expect(response.status).to eq(200)
+
+        logs = JSON.parse(response.body)
+
+        expect(logs.size).to eq(1)
+        expect(logs.first["reply_key"]).to eq(post_reply_key_2.reply_key)
+      end
+    end
   end
 
   describe '#skipped' do

GitHub sha: e9b2018b

1 Like

Revert "FIX: Partial reply key search in email sent logs."