-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.rb
48 lines (40 loc) · 1.1 KB
/
handler.rb
1
2
3
4
5
6
7
8
9
10
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
require "aws-sdk-sns"
require "json"
require "sync"
SIMPLE_PRODUCT_TOPIC = "sync-simple-product"
VARIABLE_PRODUCT_TOPIC = "sync-variable-product"
def sync_products(event:, context:)
sns = Aws::SNS::Client.new
topics = sns.list_topics.topics.map { |t|
[
t.topic_arn.split(":").last,
t.topic_arn
]
}.to_h
sync = Synchroniser.new
sync.variable_products_for_sync.each do |product|
sns.publish(
topic_arn: topics.fetch(VARIABLE_PRODUCT_TOPIC),
message: { ids: product.airtable_ids }.to_json,
)
end
sync.simple_products_for_sync.each do |product|
sns.publish(
topic_arn: topics.fetch(SIMPLE_PRODUCT_TOPIC),
message: { id: product.airtable_id }.to_json,
)
end
nil
end
def sync_simple_product(event:, context:)
event["Records"].each do |record|
id = JSON.parse(record["Sns"]["Message"])["id"]
Synchroniser.new.sync_simple_product(id)
end
end
def sync_variable_product(event:, context:)
event["Records"].each do |record|
ids = JSON.parse(record["Sns"]["Message"])["ids"]
Synchroniser.new.sync_variable_product(ids)
end
end