Module: Flapjack::Gateways::JSONAPI::Methods::ResourcePatch

Defined in:
lib/flapjack/gateways/jsonapi/methods/resource_patch.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/flapjack/gateways/jsonapi/methods/resource_patch.rb', line 11

def self.registered(app)
  app.helpers Flapjack::Gateways::JSONAPI::Helpers::Headers
  app.helpers Flapjack::Gateways::JSONAPI::Helpers::Miscellaneous
  app.helpers Flapjack::Gateways::JSONAPI::Helpers::Resources
  app.helpers Flapjack::Gateways::JSONAPI::Helpers::Serialiser

  Flapjack::Gateways::JSONAPI::RESOURCE_CLASSES.each do |resource_class|
    jsonapi_method = resource_class.jsonapi_methods[:patch]

    unless jsonapi_method.nil?

      jsonapi_links = resource_class.jsonapi_associations

      singular_links = jsonapi_links.select {|n, jd|
        jd.send(:patch).is_a?(TrueClass) && :singular.eql?(jd.number)
      }

      multiple_links = jsonapi_links.select {|n, jd|
        jd.send(:post).is_a?(TrueClass) && :multiple.eql?(jd.number)
      }

      resource = resource_class.short_model_name.plural

      app.class_eval do
        single = resource_class.short_model_name.singular

        model_type = resource_class.short_model_name.name
        model_type_plural = model_type.pluralize

        model_type_update = "#{model_type}Update".to_sym

        swagger_path "/#{resource}/{#{single}_id}" do
          operation :patch do
            key :description, jsonapi_method.descriptions[:singular]
            key :operationId, "update_#{single}"
            key :consumes, [JSONAPI_MEDIA_TYPE]
            parameter do
              key :name, "#{single}_id".to_sym
              key :in, :path
              key :description, "Id of a #{single}"
              key :required, true
              key :type, :string
              key :format, :uuid
            end
            parameter do
              key :name, :data
              key :in, :body
              key :description, "#{model_type} to update"
              key :required, true
              schema do
                key :"$ref", model_type_update
              end
            end
            response 204 do
              key :description, "No Content; #{model_type} update success"
            end
            response 403 do
              key :description, "Forbidden; invalid data"
              schema do
                key :'$ref', :Errors
              end
            end
            response 404 do
              key :description, "Not Found"
              schema do
                key :'$ref', :Errors
              end
            end
            response 409 do
              key :description, "Conflict; request id mismatch"
              schema do
                key :'$ref', :Errors
              end
            end
          end
        end

        swagger_path "/#{resource}" do
          operation :patch do
            key :description, jsonapi_method.descriptions[:multiple]
            key :operationId, "update_#{resource}"
            key :consumes, [JSONAPI_MEDIA_TYPE_BULK]
            parameter do
              key :name, :data
              key :in, :body
              key :description, "#{resource} to update"
              key :required, true
              schema do
                key :type, :array
                items do
                  key :"$ref", model_type_update
                end
              end
            end
            response 204 do
              key :description, "No Content; #{resource} update succeeded"
            end
            response 403 do
              key :description, "Forbidden; invalid data"
              schema do
                key :'$ref', :Errors
              end
            end
            response 404 do
              key :description, "Not Found"
              schema do
                key :'$ref', :Errors
              end
            end
            response 409 do
              key :description, "Conflict; request id mismatch"
              schema do
                key :'$ref', :Errors
              end
            end
          end
        end
      end

      app.patch %r{^/#{resource}(?:/(.+))?$} do
        resource_id = params[:captures].nil? ? nil :
                        params[:captures].first
        status 204

        resources_data, _ = wrapped_params

        attributes = jsonapi_method.attributes || []

        validate_data(resources_data, :attributes => attributes,
          :singular_links => singular_links,
          :multiple_links => multiple_links)

        ids = resources_data.map {|d| d['id']}

        klasses_to_lock = resources_data.inject([]) do |memo, d|
          next memo unless d.has_key?('relationships')
          d['relationships'].each_pair do |k, v|
            assoc = jsonapi_links[k.to_sym]
            next if assoc.nil?
            memo |= assoc.lock_klasses
          end
          memo
        end

        jsonapi_type = resource_class.short_model_name.singular

        resource_class.jsonapi_lock_method(:patch, klasses_to_lock) do

          resources = if resource_id.nil?
            resources = resource_class.find_by_ids!(*ids)
          else
            halt(err(409, "Id path/data mismatch")) unless ids.nil? || ids.eql?([resource_id])
            [resource_class.find_by_id!(resource_id)]
          end

          resources_by_id = resources.each_with_object({}) {|r, o| o[r.id] = r }

          attribute_types = resource_class.attribute_types

          resource_links = resources_data.each_with_object({}) do |d, memo|
            r = resources_by_id[d['id']]
            rd = normalise_json_data(attribute_types, d['attributes'] || {})

            type = d['type']
            halt(err(409, "Resource missing data type")) if type.nil?
            halt(err(409, "Resource data type '#{type}' does not match endpoint type '#{jsonapi_type}'")) unless jsonapi_type.eql?(type)

            rd.each_pair do |att, value|
              next unless attributes.include?(att.to_sym)
              r.send("#{att}=".to_sym, value)
            end
            halt(err(403, "Validation failed, " + r.errors.full_messages.join(', '))) if r.invalid?

            links = d['relationships']
            next if links.nil?

            singular_links.each_pair do |assoc, assoc_klass|
              next unless links.has_key?(assoc.to_s)
              memo[r.id] ||= {}
              memo[r.id][assoc.to_s] = assoc_klass.data_klass.find_by_id!(links[assoc.to_s]['data']['id'])
            end

            multiple_links.each_pair do |assoc, assoc_klass|
              next unless links.has_key?(assoc.to_s)
              current_assoc_ids = r.send(assoc.to_sym).ids.to_a
              memo[r.id] ||= {}

              link_ids = links[assoc.to_s]['data'].map {|l| l['id']}

              to_remove = current_assoc_ids - link_ids
              to_add    = link_ids - current_assoc_ids

              memo[r.id][assoc] = [
                to_remove.empty? ? [] : assoc_klass.data_klass.find_by_ids!(*to_remove),
                to_add.empty?    ? [] : assoc_klass.data_klass.find_by_ids!(*to_add)
              ]
            end
          end

          resources_by_id.each do |r_id, r|
            r.save!
            rl = resource_links[r_id]
            next if rl.nil?
            rl.each_pair do |assoc, value|
              case value
              when Array
                to_remove = value.first
                to_add    = value.last

                r.send(assoc.to_sym).remove(*to_remove) unless to_remove.empty?
                r.send(assoc.to_sym).add(*to_add) unless to_add.empty?
              else
                r.send("#{assoc}=".to_sym, value)
              end
            end
          end

        end

        true
      end
    end
  end
end