Skip to content

Commit

Permalink
Sync tags to Airtable
Browse files Browse the repository at this point in the history
Closes #137
  • Loading branch information
miharekar committed Feb 1, 2025
1 parent a5cb845 commit 1765f17
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/models/airtable/coffee_bags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 12 additions & 9 deletions app/models/airtable/communication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/airtable/roasters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions app/models/airtable/shots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"})} }
Expand All @@ -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

0 comments on commit 1765f17

Please sign in to comment.