Skip to content

Commit

Permalink
setup sidekiq and add new job to get dwolla_id from dwolla api
Browse files Browse the repository at this point in the history
  • Loading branch information
lyna-azerouk committed Nov 13, 2024
1 parent 15b49d0 commit 8e9ae59
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 15 deletions.
5 changes: 2 additions & 3 deletions api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ gem 'bcrypt', '~> 3.1', '>= 3.1.11'
gem 'enumerize'
gem 'plaid'
gem "dwolla_v2", "~> 3.1"



gem "sidekiq-cron"
gem "sidekiq", "~> 7.3"
23 changes: 23 additions & 0 deletions api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ GEM
bcrypt (3.1.20)
coderay (1.1.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
dry-core (1.0.1)
concurrent-ruby (~> 1.0)
zeitwerk (~> 2.6)
Expand All @@ -41,6 +42,8 @@ GEM
simpleidn
enumerize (2.8.1)
activesupport (>= 3.2)
et-orbi (1.2.11)
tzinfo
faraday (1.2.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords
Expand All @@ -50,6 +53,11 @@ GEM
faraday (~> 1.0)
ffi (1.17.0-arm64-darwin)
formatador (1.1.0)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
globalid (1.2.1)
activesupport (>= 6.1)
guard (2.18.1)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
Expand All @@ -72,6 +80,7 @@ GEM
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.6.1)
lumberjack (1.2.10)
method_source (1.1.0)
minitest (5.25.1)
Expand All @@ -92,6 +101,7 @@ GEM
method_source (~> 1.0)
puma (6.4.3)
nio4r (~> 2.0)
raabro (1.4.0)
rack (3.1.8)
rack-protection (4.0.0)
base64 (>= 0.1.0)
Expand All @@ -101,8 +111,19 @@ GEM
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
ffi (~> 1.0)
redis-client (0.22.2)
connection_pool
ruby2_keywords (0.0.5)
shellany (0.0.1)
sidekiq (7.3.4)
connection_pool (>= 2.3.0)
logger
rack (>= 2.2.4)
redis-client (>= 0.22.2)
sidekiq-cron (1.12.0)
fugit (~> 1.8)
globalid (>= 1.0.1)
sidekiq (>= 6)
simpleidn (0.2.3)
sinatra (4.0.0)
mustermann (~> 3.0)
Expand Down Expand Up @@ -132,6 +153,8 @@ DEPENDENCIES
pg
plaid
puma (~> 6.0)
sidekiq (~> 7.3)
sidekiq-cron
sinatra

RUBY VERSION
Expand Down
10 changes: 1 addition & 9 deletions api/config.ru
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
require './routes'

ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'ecommerce_development',
username: 'ecomerce_user',
password: 'postgres',
pool: 5
)
require './config/database.rb'

run Sinatra::Application
10 changes: 10 additions & 0 deletions api/config/database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'active_record'

ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'ecommerce_development',
username: 'ecomerce_user',
password: 'postgres',
pool: 5
)
4 changes: 4 additions & 0 deletions api/config/initializers/schedule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# schedule.yml
set_costumer_id_job:
cron: "*/15 * * * *"
class: "SetCostumerIdJob"
12 changes: 12 additions & 0 deletions api/config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'sidekiq'
require 'sidekiq-cron'
require 'yaml'
require 'active_record'
require_relative '../../jobs/set_costumer_id_job'
require_relative '../database'

schedule_file = './schedule.yml'

if File.exist?(schedule_file)
Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
end
20 changes: 20 additions & 0 deletions api/jobs/set_costumer_id_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'sidekiq'
require 'sidekiq-cron'
require 'json'
require_relative '../modal/dwolla'
require_relative '../modal/user'

class SetCostumerIdJob
include Sidekiq::Worker

def perform
dwolla = Dwolla.new()
dwolla.init()
costumers = dwolla.get_costomers()

costumers["_embedded"]["customers"].each do |customer|
user = ::User.find_by(email: customer["email"] )
user.update(dwolla_id: customer["id"] ) if user.present?
end
end
end
10 changes: 9 additions & 1 deletion api/modal/dwolla.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ def init()
def create_verifed_user(request_body)
new_customer = @@dwolla.post "customers", request_body
end
end

def get_costomers
@@dwolla.get "customers"
end

def get_costumer(request_body)
@@dwolla.get "customers", request_body
end
end
4 changes: 4 additions & 0 deletions api/modal/dwolla_reponse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

class dwolla_reponse

end
2 changes: 1 addition & 1 deletion api/modal/plaid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
region: "IL",
street: "3200 W Armitage Ave",
postal_code: "60657",
country: "US"
country: "US"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/transactions/api/user/save_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def save(input)

def create_dwolla_user(input)
if @user.create_verfied_user_dowlla_api
Success(input.merge(errors: @user))
Success(input.merge(user: @user))
else
Failure(input.merge(errors: "error while creating user in dwolla"))
end
Expand Down

0 comments on commit 8e9ae59

Please sign in to comment.