diff --git a/app/models/airtable/coffee_bags.rb b/app/models/airtable/coffee_bags.rb index b9c3f165..d4699fef 100644 --- a/app/models/airtable/coffee_bags.rb +++ b/app/models/airtable/coffee_bags.rb @@ -41,10 +41,10 @@ def prepare_record(coffee_bag) {fields: fields.compact} end - def local_record_attributes(record) + def update_local_record(coffee_bag, record, updated_at) attributes = record["fields"].slice(*STANDARD_FIELDS.keys).transform_keys { |k| STANDARD_FIELDS[k] } attributes[:name] = record["fields"]["Name"] - attributes + coffee_bag.update!(attributes.merge(skip_airtable_sync: true, updated_at:)) end def upload_roaster_to_airtable(coffee_bag) diff --git a/app/models/airtable/communication.rb b/app/models/airtable/communication.rb index 1418aa9d..abc92c4e 100644 --- a/app/models/airtable/communication.rb +++ b/app/models/airtable/communication.rb @@ -15,8 +15,8 @@ def upload(record) def upload_multiple(records) return unless records.exists? - prepared_records = records.with_attached_image.map { |record| prepare_record(record) } - update_records(prepared_records) do |response| + data = records.with_attached_image.map { |record| prepare_record(record) } + update_records(data) do |response| response["records"].each do |record_data| record = records.find_by(id: record_data["fields"]["ID"]) next unless record @@ -35,8 +35,7 @@ def download(minutes: 60, timestamps: {}) next unless local_record updated_at = timestamps[record["id"]].presence || request_time - attributes = local_record_attributes(record) - local_record.update!(attributes.merge(skip_airtable_sync: true, updated_at:)) + update_local_record(local_record, record, updated_at) end end @@ -54,13 +53,17 @@ def webhook_refresh private - def update_record(record_id, fields) - api_request("/#{airtable_info.base_id}/#{airtable_info.tables[self.class::TABLE_NAME]["id"]}/#{record_id}", fields, method: :patch) + def update_record(record_id, data) + data = data.except(:id) + api_request("/#{airtable_info.base_id}/#{airtable_info.tables[self.class::TABLE_NAME]["id"]}/#{record_id}", data, method: :patch) end - def update_records(records) - records.each_slice(10).map do |batch| - data = {performUpsert: {fieldsToMergeOn: ["ID"]}, records: batch} + def update_records(data_array) + has_typecast = data_array.any? { |data| data.key?(:typecast) } + data_array.each_slice(10).map do |batch| + records = has_typecast ? batch.map { |data| data.except(:typecast) } : batch + data = {performUpsert: {fieldsToMergeOn: ["ID"]}, records:} + data[:typecast] = true if has_typecast response = api_request("/#{airtable_info.base_id}/#{airtable_info.tables[self.class::TABLE_NAME]["id"]}", data, method: :patch) yield response if block_given? end diff --git a/app/models/airtable/roasters.rb b/app/models/airtable/roasters.rb index 698f129f..354f2cf5 100644 --- a/app/models/airtable/roasters.rb +++ b/app/models/airtable/roasters.rb @@ -37,10 +37,10 @@ def prepare_record(roaster) {fields: fields.compact} end - def local_record_attributes(record) + def update_local_record(roaster, record, updated_at) attributes = record["fields"].slice(*STANDARD_FIELDS.keys).transform_keys { |k| STANDARD_FIELDS[k] } attributes[:name] = record["fields"]["Name"] - attributes + roaster.update!(attributes.merge(skip_airtable_sync: true, updated_at:)) end end end diff --git a/app/models/airtable/shots.rb b/app/models/airtable/shots.rb index 493257b7..8c0c6c89 100644 --- a/app/models/airtable/shots.rb +++ b/app/models/airtable/shots.rb @@ -29,7 +29,8 @@ def table_fields {name: "ID", type: "singleLineText"}, {name: "URL", type: "url"}, {name: "Image", type: "multipleAttachments"}, - {name: "Start time", type: "dateTime", options: {timeZone: "client", dateFormat: {name: "local"}, timeFormat: {name: "24hour"}}} + {name: "Start time", type: "dateTime", options: {timeZone: "client", dateFormat: {name: "local"}, timeFormat: {name: "24hour"}}}, + {name: "Tags", type: "multipleSelects", options: {choices: user.tags.pluck("name").map { {name: it} }}} ] coffee_management = user.coffee_management_enabled? ? [{name: "Coffee Bag", type: "multipleRecordLinks", options: {linkedTableId: airtable_info.tables[CoffeeBags::TABLE_NAME]["id"]}}] : [] standard = STANDARD_FIELDS.map { |name, attribute| {name:, **(FIELD_OPTIONS[attribute] || {type: "singleLineText"})} } @@ -43,20 +44,24 @@ def prepare_record(shot) fields = { "ID" => shot.id, "URL" => shot_url(shot), - "Start time" => shot.start_time + "Start time" => shot.start_time, + "Tags" => shot.tags.pluck(:name) } fields["Coffee Bag"] = [shot.coffee_bag&.airtable_id].compact if user.coffee_management_enabled? STANDARD_FIELDS.each { |name, attribute| fields[name] = shot.public_send(attribute) } user.metadata_fields.each { |field| fields[field] = shot.metadata[field].to_s } fields["Image"] = [{url: shot.image.url(disposition: "attachment"), filename: shot.image.filename.to_s}] if shot.image.attached? - {fields: fields.compact} + data = {fields: fields.compact} + data[:typecast] = true if fields["Tags"].present? + data end - def local_record_attributes(record) + def update_local_record(shot, record, updated_at) attributes = record["fields"].slice(*STANDARD_FIELDS.keys).transform_keys { |k| STANDARD_FIELDS[k] } attributes[:metadata] = user.metadata_fields.index_with { |f| record["fields"][f] } - attributes + shot.tag_list = record["fields"]["Tags"].join(",") + shot.update!(attributes.merge(skip_airtable_sync: true, updated_at:)) end end end