Skip to content

Commit d764ec6

Browse files
committed
Update user airtable sync
1 parent fc83538 commit d764ec6

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

app/models/airtable_sync.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def self.mark_synced(record, airtable_id)
3434
)
3535
end
3636

37-
def self.sync!(classname, limit: nil, sync_all: false)
37+
def self.sync!(classname, limit: nil, sync_all: false, no_upload: false)
3838
batch = false
3939
klass = resolve_class(classname)
4040
validate_sync_methods!(klass)
@@ -50,14 +50,16 @@ def self.sync!(classname, limit: nil, sync_all: false)
5050
airtable_ids = []
5151

5252
if batch
53-
batch_sync!(table_id, records, klass.airtable_sync_sync_id, field_mappings)
53+
batch_sync!(table_id, records, klass.airtable_sync_sync_id, field_mappings, no_upload:)
5454
else
5555
records.each do |record|
5656
old_airtable_id = find_by(record_identifier: build_identifier(record))&.airtable_id
5757
airtable_ids << individual_sync!(table_id, record, field_mappings, old_airtable_id)
5858
end
5959
end
6060

61+
return records if no_upload
62+
6163
sync_data = records.map do |record|
6264
data = {
6365
record_identifier: build_identifier(record),
@@ -76,7 +78,7 @@ def self.sync!(classname, limit: nil, sync_all: false)
7678
records
7779
end
7880

79-
def self.batch_sync!(table_id, records, sync_id, mappings)
81+
def self.batch_sync!(table_id, records, sync_id, mappings, no_upload: false)
8082
csv_string = CSV.generate do |csv|
8183
csv << mappings.keys
8284

@@ -86,6 +88,14 @@ def self.batch_sync!(table_id, records, sync_id, mappings)
8688
end
8789
end
8890

91+
if no_upload
92+
filename = "airtable_sync_#{table_id}_#{Time.current.strftime('%Y%m%d_%H%M%S')}.csv"
93+
filepath = Rails.root.join("tmp", filename)
94+
File.write(filepath, csv_string)
95+
Rails.logger.info("Airtable batch sync saved locally: #{filepath}")
96+
return filepath
97+
end
98+
8999
response = Faraday.post("https://api.airtable.com/v0/#{ENV['AIRTABLE_BASE_ID']}/#{table_id}/sync/#{sync_id}") do |req|
90100
req.headers = {
91101
"Authorization" => "Bearer #{ENV['AIRTABLE_PAT']}",

app/models/user.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ def self.airtable_sync_field_mappings
9696
"GitHub Username" => :github_username,
9797
"Email" => :email,
9898
"Display Name" => lambda { |user| user.display_name },
99-
"Avatar" => :avatar,
10099
"Is Banned" => :is_banned,
101-
"Is MCG" => :is_mcg,
100+
"Birthday" => :birthday,
102101
"Role" => lambda { |user|
103102
roles = []
104103
roles << "Admin" if user.admin?

0 commit comments

Comments
 (0)