-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add situation of the modules of the bling
- Loading branch information
Showing
4 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
db/migrate/20241023183743_create_bling_module_situations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |