Class: Spree::DataRequests::Create

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/data_requests/create.rb

Overview

Opens a data subject request and queues the work.

An in-flight request is returned rather than duplicated. That is the rate limit: building an export is an unbounded read across a person's whole history, and a customer holding the button down should not be able to queue a hundred of them. It is also the honest answer — the request they already made is still being worked on.

A request the customer raised themselves waits for confirmation from its email address instead of being queued, because it reaches guest orders placed at that address. Asking again while it waits sends the email again.

Constant Summary collapse

CONFIRMATION_EVENT =
'data_request.confirmation_requested'.freeze

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(store:, customer:, kind:, requested_by: nil, process: true, confirm_by_email: false, redirect_url: nil) ⇒ Spree::ServiceModule::Result

Parameters:

  • store (Spree::Store)
  • customer (Spree::Customer)
  • kind (String) —

    Spree::DataRequest::ACCESS or ERASURE

  • requested_by (Object, nil) (defaults to: nil) —

    the staff actor; nil when the subject asked for it themselves

  • process (Boolean) (defaults to: true) —

    whether to queue the work. False where the caller fulfils the request itself — the admin export renders the file inline for a waiting staffer, and queueing would additionally email the customer a copy of something they did not ask for

  • confirm_by_email (Boolean) (defaults to: false) —

    hold the request until it is confirmed from its email address; true for the customer's own requests

  • redirect_url (String, nil) (defaults to: nil) —

    the storefront page the confirmation link opens, already checked against the store's allowed origins

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/services/spree/data_requests/create.rb', line 33

def call(store:, customer:, kind:, requested_by: nil, process: true, confirm_by_email: false, redirect_url: nil)
  # Nothing useful is left to answer with: an export would return the
  # tombstone and email a link to an address that no longer receives,
  # and a second erasure has nothing to erase.
  if customer.anonymized?
    data_request = Spree::DataRequest.new(customer: customer)
    data_request.errors.add(:base, I18n.t('spree.data_request_errors.already_anonymized'))
    return failure(data_request)
  end

  data_request = nil
  existing = nil

  # Serialized on the customer row. A check-then-create leaves a window
  # where two callers both see nothing in flight and both queue an export
  # of the same person's whole history; taking the lock closes it without
  # a partial unique index, which MySQL does not support.
  customer.with_lock do
    lapse_unconfirmed_requests(store: store, customer: customer, kind: kind, staff_request: !confirm_by_email)
    existing = in_flight_request(store: store, customer: customer, kind: kind,
                                 include_unconfirmed: confirm_by_email)

    unless existing
      data_request = Spree::DataRequest.new(
        store: store,
        customer: customer,
        kind: kind.to_s,
        email: customer.email,
        requested_by: requested_by,
        status: confirm_by_email ? 'awaiting_confirmation' : 'pending'
      )
      data_request.save
    end
  end

  return failure(data_request) if data_request && !data_request.persisted?

  request = existing || data_request

  # Both happen after the lock is released: the job may start at once and
  # should not contend for the row this flow holds, and the email must
  # not go out before the row is committed.
  if request.awaiting_confirmation?
    request_confirmation(request, redirect_url: redirect_url)
  elsif data_request && process
    Spree::DataRequests::ProcessJob.perform_later(data_request.prefixed_id)
  end

  success(request)
end