Skip to content

Commit

Permalink
add situation of the modules of the bling
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Oct 23, 2024
1 parent 9cc31e1 commit 7ba49aa
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/models/bling_module_situation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: bling_module_situations
#
# id :bigint not null, primary key
# color :string
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# inherited_id :integer
# module_id :integer not null
# situation_id :integer not null
#
# Indexes
#
# index_bling_module_situations_on_situation_id (situation_id) UNIQUE
#
class BlingModuleSituation < ApplicationRecord
validates :situation_id, presence: true, uniqueness: true
validates :name, presence: true
validates :module_id, presence: true
end
74 changes: 74 additions & 0 deletions app/models/services/bling/fetch_module_situations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

module Services
module Bling
class FetchModuleSituations < ApplicationService
attr_accessor :tenant, :module_id

def initialize(tenant:, module_id:)
@tenant = tenant
@module_id = module_id
end

def call
fetch_and_save_module_situations
end

private

def fetch_and_save_module_situations
response = fetch_module_situations
return response if response['error']

save_module_situations(response['data'])
response
end

def fetch_module_situations
token = bling_token
url = URI("https://www.bling.com.br/Api/v3/situacoes/modulos/#{@module_id}")

headers = {
'Accept' => 'application/json',
'Authorization' => "Bearer #{token}"
}

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Get.new(url, headers)

response = http.request(request)

parse_response(response)
rescue StandardError => e
{ 'error' => { 'type' => 'REQUEST_ERROR', 'message' => e.message } }
end

def parse_response(response)
body = JSON.parse(response.body)
if response.is_a?(Net::HTTPSuccess)
{ 'data' => body['data'] }
else
{ 'error' => { 'type' => 'API_ERROR', 'message' => body || 'Unknown error' } }
end
end

def save_module_situations(situations)
situations.each do |situation|
BlingModuleSituation.find_or_initialize_by(situation_id: situation['id']).tap do |s|
s.name = situation['nome']
s.inherited_id = situation['idHerdado']
s.color = situation['cor']
s.module_id = @module_id
s.save!
end
end
end

def bling_token
@bling_token ||= BlingDatum.find_by(account_id: tenant).access_token
end
end
end
end
53 changes: 53 additions & 0 deletions app/models/services/bling/fetch_modules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

module Services
module Bling
class FetchModules < ApplicationService
attr_accessor :tenant

def initialize(tenant:)
@tenant = tenant
end

def call
fetch_modules
end

private

def fetch_modules
token = bling_token
url = URI("https://www.bling.com.br/Api/v3/situacoes/modulos")

headers = {
'Accept' => 'application/json',
'Authorization' => "Bearer #{token}"
}

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Get.new(url, headers)

response = http.request(request)

parse_response(response)
rescue StandardError => e
{ 'error' => { 'type' => 'REQUEST_ERROR', 'message' => e.message } }
end

def parse_response(response)
body = JSON.parse(response.body)
if response.is_a?(Net::HTTPSuccess)
{ 'data' => body['data'] }
else
{ 'error' => { 'type' => 'API_ERROR', 'message' => body || 'Unknown error' } }
end
end

def bling_token
@bling_token ||= BlingDatum.find_by(account_id: tenant).access_token
end
end
end
end
15 changes: 15 additions & 0 deletions db/migrate/20241023183743_create_bling_module_situations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateBlingModuleSituations < ActiveRecord::Migration[7.0]
def change
create_table :bling_module_situations do |t|
t.integer :situation_id, null: false
t.string :name, null: false
t.integer :inherited_id
t.string :color
t.integer :module_id, null: false

t.timestamps
end

add_index :bling_module_situations, :situation_id, unique: true
end
end

0 comments on commit 7ba49aa

Please sign in to comment.