From 8e9ae59287f0a7a8d41cbf114c92fafc5b5c5906 Mon Sep 17 00:00:00 2001 From: Lyna Date: Wed, 13 Nov 2024 18:05:45 +0100 Subject: [PATCH] setup sidekiq and add new job to get dwolla_id from dwolla api --- api/Gemfile | 5 ++-- api/Gemfile.lock | 23 +++++++++++++++++++ api/config.ru | 10 +------- api/config/database.rb | 10 ++++++++ api/config/initializers/schedule.yml | 4 ++++ api/config/initializers/sidekiq.rb | 12 ++++++++++ api/jobs/set_costumer_id_job.rb | 20 ++++++++++++++++ api/modal/dwolla.rb | 10 +++++++- api/modal/dwolla_reponse.rb | 4 ++++ api/modal/plaid.rb | 2 +- api/transactions/api/user/save_transaction.rb | 2 +- 11 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 api/config/database.rb create mode 100644 api/config/initializers/schedule.yml create mode 100644 api/config/initializers/sidekiq.rb create mode 100644 api/jobs/set_costumer_id_job.rb create mode 100644 api/modal/dwolla_reponse.rb diff --git a/api/Gemfile b/api/Gemfile index f246e9e..4f34c31 100644 --- a/api/Gemfile +++ b/api/Gemfile @@ -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" diff --git a/api/Gemfile.lock b/api/Gemfile.lock index d2948e2..07a1d03 100644 --- a/api/Gemfile.lock +++ b/api/Gemfile.lock @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -132,6 +153,8 @@ DEPENDENCIES pg plaid puma (~> 6.0) + sidekiq (~> 7.3) + sidekiq-cron sinatra RUBY VERSION diff --git a/api/config.ru b/api/config.ru index c0ca73c..0501a16 100644 --- a/api/config.ru +++ b/api/config.ru @@ -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 diff --git a/api/config/database.rb b/api/config/database.rb new file mode 100644 index 0000000..f34b8f2 --- /dev/null +++ b/api/config/database.rb @@ -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 +) \ No newline at end of file diff --git a/api/config/initializers/schedule.yml b/api/config/initializers/schedule.yml new file mode 100644 index 0000000..f20256c --- /dev/null +++ b/api/config/initializers/schedule.yml @@ -0,0 +1,4 @@ +# schedule.yml +set_costumer_id_job: + cron: "*/15 * * * *" + class: "SetCostumerIdJob" \ No newline at end of file diff --git a/api/config/initializers/sidekiq.rb b/api/config/initializers/sidekiq.rb new file mode 100644 index 0000000..fa9d77a --- /dev/null +++ b/api/config/initializers/sidekiq.rb @@ -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 diff --git a/api/jobs/set_costumer_id_job.rb b/api/jobs/set_costumer_id_job.rb new file mode 100644 index 0000000..7c62d8d --- /dev/null +++ b/api/jobs/set_costumer_id_job.rb @@ -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 \ No newline at end of file diff --git a/api/modal/dwolla.rb b/api/modal/dwolla.rb index 523e4b3..56930d8 100644 --- a/api/modal/dwolla.rb +++ b/api/modal/dwolla.rb @@ -14,4 +14,12 @@ def init() def create_verifed_user(request_body) new_customer = @@dwolla.post "customers", request_body end -end \ No newline at end of file + + def get_costomers + @@dwolla.get "customers" + end + + def get_costumer(request_body) + @@dwolla.get "customers", request_body + end +end diff --git a/api/modal/dwolla_reponse.rb b/api/modal/dwolla_reponse.rb new file mode 100644 index 0000000..eb6567b --- /dev/null +++ b/api/modal/dwolla_reponse.rb @@ -0,0 +1,4 @@ + +class dwolla_reponse + +end \ No newline at end of file diff --git a/api/modal/plaid.rb b/api/modal/plaid.rb index 48a4597..8249665 100644 --- a/api/modal/plaid.rb +++ b/api/modal/plaid.rb @@ -27,7 +27,7 @@ region: "IL", street: "3200 W Armitage Ave", postal_code: "60657", - country: "US" + country: "US" } } } diff --git a/api/transactions/api/user/save_transaction.rb b/api/transactions/api/user/save_transaction.rb index 21e8b86..a93c25c 100644 --- a/api/transactions/api/user/save_transaction.rb +++ b/api/transactions/api/user/save_transaction.rb @@ -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