Class: Spree::Api::V2::Operator::GuestJsonGzipsController

Inherits:
ResourceController
  • Object
show all
Includes:
ActiveStorage::SetCurrent
Defined in:
app/controllers/spree/api/v2/operator/guest_json_gzips_controller.rb

Instance Method Summary collapse

Instance Method Details

#collection_serializer ⇒ Object

override



74
75
76
# File 'app/controllers/spree/api/v2/operator/guest_json_gzips_controller.rb', line 74

def collection_serializer
  SpreeCmCommissioner::V2::Operator::ExportSerializer
end

#create ⇒ Object

POST /api/v2/operator/guest_json_gzips

  • taxon_id=1


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/spree/api/v2/operator/guest_json_gzips_controller.rb', line 22

def create
  spree_authorize! :create, SpreeCmCommissioner::Exports::OperatorGuestJsonGzip

  exportable = Spree::Taxon.find(params[:taxon_id])

  result = SpreeCmCommissioner::OperatorGuestJsonGzips::Create.call(
    exportable: exportable,
    # Scope the offline file to the requesting operator's check-in role (nil = unrestricted).
    role: spree_current_user.check_in_role_for(exportable),
    force_create: false,
    options: {
      created_by_id: spree_current_user.id.to_s,
      created_by_login: spree_current_user.,
      ip_address: request.remote_ip,
      user_agent: request.user_agent,
      request_id: request.request_id,
      client_version: request.headers['X-Cm-App-Version'],
      client_os: request.headers['X-Cm-OperatingSystem'],
      client_platform: request.headers['X-Cm-App-Platform']
    }
  )

  if result.success?
    status = result.value.created_at < 1.minute.ago ? 200 : 201
    @export = result.value
    render_serialized_payload(status) { serialize_resource(@export) }
  else
    render_error_payload(result.error)
  end
end

#index ⇒ Object

Override index to disable server-side caching. We always want to show the latest exports and their download status. The app will cache files on its side to avoid unnecessary server requests once files are ready.



14
15
16
17
18
# File 'app/controllers/spree/api/v2/operator/guest_json_gzips_controller.rb', line 14

def index
  render_serialized_payload do
    serialize_collection(paginated_collection)
  end
end

#require_event_user ⇒ Object

Raises:

  • (CanCan::AccessDenied)


53
54
55
# File 'app/controllers/spree/api/v2/operator/guest_json_gzips_controller.rb', line 53

def require_event_user
  raise CanCan::AccessDenied unless spree_current_user.events.exists?(id: params[:taxon_id])
end

#resource_serializer ⇒ Object

override



79
80
81
# File 'app/controllers/spree/api/v2/operator/guest_json_gzips_controller.rb', line 79

def resource_serializer
  SpreeCmCommissioner::V2::Operator::ExportSerializer
end

#scope ⇒ Object

override Scope the list to the requesting operator's check-in group, the same way the create cache does — otherwise a restricted crew could list (and download via exported_file_url) other groups' files for the event.



61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/spree/api/v2/operator/guest_json_gzips_controller.rb', line 61

def scope
  exportable = Spree::Taxon.find(params[:taxon_id])

  SpreeCmCommissioner::Exports::OperatorGuestJsonGzip
    .where(
      exportable_type: 'Spree::Taxon',
      exportable_id: params[:taxon_id],
      role_id: spree_current_user.check_in_role_for(exportable)&.id
    )
    .order(created_at: :desc)
end