Class: Quote::RepPreviewTokenService

Inherits:
Object
  • Object
show all
Defined in:
app/services/quote/rep_preview_token_service.rb

Overview

Mints and decodes quote rep-preview tokens.

Signed with ActiveSupport::MessageVerifier under the namespaced verifier
:quote_rep_preview (mirrors BlogPreviewTokenService). Lets a rep open the
customer-facing /ql/<token> view from the "Customer link" / "Preview as
customer" controls on select_pdf_template before the quote has reached
the complete state — MyQuotesController#quote_viewer normally renders a
"not ready" page for every earlier state. 1h TTL so a forwarded link goes
stale quickly if it ever leaves the rep's browser.

Constant Summary collapse

TOKEN_EXPIRY =

Token expiry.

1.hour
PURPOSE =

Purpose.

'quote_rep_preview/v1'

Class Method Summary collapse

Class Method Details

.generate_token(quote_id) ⇒ Object



19
20
21
# File 'app/services/quote/rep_preview_token_service.rb', line 19

def generate_token(quote_id)
  verifier.generate({ quote_id: quote_id, iat: Time.current.to_i }, expires_in: TOKEN_EXPIRY, purpose: PURPOSE)
end

.valid_for_quote?(token, quote_id) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'app/services/quote/rep_preview_token_service.rb', line 23

def valid_for_quote?(token, quote_id)
  return false if token.blank?

  payload = verifier.verified(token, purpose: PURPOSE)&.symbolize_keys
  payload.present? && payload[:quote_id].to_i == quote_id.to_i
end

.verifierObject



30
31
32
# File 'app/services/quote/rep_preview_token_service.rb', line 30

def verifier
  Rails.application.message_verifier(:quote_rep_preview)
end