mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 13:19:39 +02:00
Remove dependency on stock exchange table (#1368)
This commit is contained in:
parent
b75b41a5e2
commit
45935db5f3
6 changed files with 37 additions and 22 deletions
|
@ -40,7 +40,8 @@ class Provider::Marketstack
|
||||||
{
|
{
|
||||||
name: ticker["name"],
|
name: ticker["name"],
|
||||||
symbol: ticker["symbol"],
|
symbol: ticker["symbol"],
|
||||||
exchange: exchange_mic || ticker.dig("stock_exchange", "mic"),
|
exchange_mic: exchange_mic || ticker.dig("stock_exchange", "mic"),
|
||||||
|
exchange_acronym: ticker.dig("stock_exchange", "acronym"),
|
||||||
country_code: ticker.dig("stock_exchange", "country_code")
|
country_code: ticker.dig("stock_exchange", "country_code")
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,8 @@ class Security < ApplicationRecord
|
||||||
|
|
||||||
has_many :trades, dependent: :nullify, class_name: "Account::Trade"
|
has_many :trades, dependent: :nullify, class_name: "Account::Trade"
|
||||||
|
|
||||||
validates :ticker, presence: true, uniqueness: { case_sensitive: false }
|
validates :ticker, presence: true
|
||||||
|
validates :ticker, uniqueness: { scope: :exchange_mic, case_sensitive: false }
|
||||||
|
|
||||||
def current_price
|
def current_price
|
||||||
@current_price ||= Security::Price.find_price(ticker:, date: Date.current)
|
@current_price ||= Security::Price.find_price(ticker:, date: Date.current)
|
||||||
|
|
|
@ -7,21 +7,24 @@ class Security::Importer
|
||||||
def import
|
def import
|
||||||
securities = @provider.fetch_tickers(exchange_mic: @stock_exchange)&.tickers
|
securities = @provider.fetch_tickers(exchange_mic: @stock_exchange)&.tickers
|
||||||
|
|
||||||
stock_exchanges = StockExchange.where(mic: securities.map { |s| s[:exchange] }).index_by(&:mic)
|
# Deduplicate securities based on ticker and exchange_mic
|
||||||
existing_securities = Security.where(ticker: securities.map { |s| s[:symbol] }, stock_exchange_id: stock_exchanges.values.map(&:id)).pluck(:ticker, :stock_exchange_id).to_set
|
securities_to_create = securities
|
||||||
|
.map do |security|
|
||||||
|
{
|
||||||
|
name: security[:name],
|
||||||
|
ticker: security[:symbol],
|
||||||
|
country_code: security[:country_code],
|
||||||
|
exchange_mic: security[:exchange_mic],
|
||||||
|
exchange_acronym: security[:exchange_acronym]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
.compact
|
||||||
|
.uniq { |security| [ security[:ticker], security[:exchange_mic] ] }
|
||||||
|
|
||||||
securities_to_create = securities.map do |security|
|
Security.upsert_all(
|
||||||
stock_exchange_id = stock_exchanges[security[:exchange]]&.id
|
securities_to_create,
|
||||||
next if existing_securities.include?([ security[:symbol], stock_exchange_id ])
|
unique_by: [ :ticker, :exchange_mic ],
|
||||||
|
update_only: [ :name, :country_code, :exchange_acronym ]
|
||||||
{
|
) unless securities_to_create.empty?
|
||||||
name: security[:name],
|
|
||||||
ticker: security[:symbol],
|
|
||||||
stock_exchange_id: stock_exchange_id,
|
|
||||||
country_code: security[:country_code]
|
|
||||||
}
|
|
||||||
end.compact
|
|
||||||
|
|
||||||
Security.insert_all(securities_to_create) unless securities_to_create.empty?
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,7 +14,7 @@ Rails.application.configure do
|
||||||
# Auth for jobs admin dashboard
|
# Auth for jobs admin dashboard
|
||||||
ActiveSupport.on_load(:good_job_application_controller) do
|
ActiveSupport.on_load(:good_job_application_controller) do
|
||||||
before_action do
|
before_action do
|
||||||
raise ActionController::RoutingError.new("Not Found") unless current_user&.super_admin?
|
raise ActionController::RoutingError.new("Not Found") unless current_user&.super_admin? || Rails.env.development?
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
|
|
10
db/migrate/20241025174650_add_mic_to_securities.rb
Normal file
10
db/migrate/20241025174650_add_mic_to_securities.rb
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
class AddMicToSecurities < ActiveRecord::Migration[7.2]
|
||||||
|
def change
|
||||||
|
add_column :securities, :exchange_mic, :string
|
||||||
|
add_column :securities, :exchange_acronym, :string
|
||||||
|
|
||||||
|
remove_column :securities, :stock_exchange_id, :uuid
|
||||||
|
|
||||||
|
add_index :securities, [ :ticker, :exchange_mic ], unique: true
|
||||||
|
end
|
||||||
|
end
|
8
db/schema.rb
generated
8
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.2].define(version: 2024_10_24_142537) do
|
ActiveRecord::Schema[7.2].define(version: 2024_10_25_174650) do
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pgcrypto"
|
enable_extension "pgcrypto"
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -479,9 +479,10 @@ ActiveRecord::Schema[7.2].define(version: 2024_10_24_142537) do
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.string "country_code"
|
t.string "country_code"
|
||||||
t.uuid "stock_exchange_id"
|
t.string "exchange_mic"
|
||||||
|
t.string "exchange_acronym"
|
||||||
t.index ["country_code"], name: "index_securities_on_country_code"
|
t.index ["country_code"], name: "index_securities_on_country_code"
|
||||||
t.index ["stock_exchange_id"], name: "index_securities_on_stock_exchange_id"
|
t.index ["ticker", "exchange_mic"], name: "index_securities_on_ticker_and_exchange_mic", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "security_prices", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
create_table "security_prices", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||||
|
@ -603,7 +604,6 @@ ActiveRecord::Schema[7.2].define(version: 2024_10_24_142537) do
|
||||||
add_foreign_key "imports", "families"
|
add_foreign_key "imports", "families"
|
||||||
add_foreign_key "institutions", "families"
|
add_foreign_key "institutions", "families"
|
||||||
add_foreign_key "merchants", "families"
|
add_foreign_key "merchants", "families"
|
||||||
add_foreign_key "securities", "stock_exchanges"
|
|
||||||
add_foreign_key "sessions", "impersonation_sessions", column: "active_impersonator_session_id"
|
add_foreign_key "sessions", "impersonation_sessions", column: "active_impersonator_session_id"
|
||||||
add_foreign_key "sessions", "users"
|
add_foreign_key "sessions", "users"
|
||||||
add_foreign_key "taggings", "tags"
|
add_foreign_key "taggings", "tags"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue