Skip to content

Commit

Permalink
chore(CE): Add data_app_token generation (#381)
Browse files Browse the repository at this point in the history
Co-authored-by: afthab vp <[email protected]>
  • Loading branch information
RafaelOAiSquared and afthabvp authored Oct 1, 2024
1 parent bbd9a78 commit 31ec315
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 13 deletions.
5 changes: 5 additions & 0 deletions server/app/models/connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def connector_definition
@connector_definition ||= connector_client.new.meta_data.with_indifferent_access
end

def icon
@connector_definition ||= connector_client.new.meta_data.with_indifferent_access
@connector_definition.dig(:data, :icon)
end

# TODO: move the method to integration gem
def execute_query(query, limit: 50)
connection_config = configuration.with_indifferent_access
Expand Down
13 changes: 13 additions & 0 deletions server/app/models/data_app.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

class DataApp < ApplicationRecord
before_validation :generate_data_app_token, on: :create
validates :workspace_id, presence: true
validates :status, presence: true
validates :name, presence: true
validates :data_app_token, presence: true, uniqueness: true

enum :status, %i[inactive active draft]

Expand All @@ -18,4 +20,15 @@ class DataApp < ApplicationRecord
def set_default_status
self.status ||= :draft
end

def generate_data_app_token
self.data_app_token = generate_unique_token
end

def generate_unique_token
loop do
token = Devise.friendly_token
break token unless DataApp.exists?(data_app_token: token)
end
end
end
2 changes: 1 addition & 1 deletion server/app/models/visual_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class VisualComponent < ApplicationRecord
validates :model_id, presence: true
validates :data_app_id, presence: true

enum :component_type, %i[pie bar data_table]
enum component_type: { doughnut: 0, bar: 1, data_table: 2 }

belongs_to :workspace
belongs_to :data_app
Expand Down
26 changes: 26 additions & 0 deletions server/spec/factories/data_apps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

FactoryBot.define do
factory :data_app do
name { "Sales Dashboard" }
description { "A dashboard for visualizing sales data" }
status { "active" }
meta_data do
{
author: "John Doe",
version: "1.0"
}
end
association :workspace

transient do
visual_components_count { 1 }
end

after(:create) do |data_app, evaluator|
if evaluator.visual_components_count > 0
create_list(:visual_component, evaluator.visual_components_count, data_app:)
end
end
end
end
9 changes: 6 additions & 3 deletions server/spec/factories/roles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"connector" => { "read" => true, "create" => true, "delete" => true, "update" => true },
"workspace" => { "read" => true, "create" => true, "delete" => true, "update" => true },
"sync_record" => { "read" => true, "create" => true, "delete" => true, "update" => true },
"connector_definition" => { "read" => true, "create" => true, "delete" => true, "update" => true }
"connector_definition" => { "read" => true, "create" => true, "delete" => true, "update" => true },
"data_app" => { "read" => true, "create" => true, "delete" => true, "update" => true }
}
}
end
Expand All @@ -50,7 +51,8 @@
"connector" => { "read" => true, "create" => true, "delete" => true, "update" => true },
"workspace" => { "read" => true, "create" => false, "delete" => false, "update" => false },
"sync_record" => { "read" => true, "create" => true, "delete" => true, "update" => true },
"connector_definition" => { "read" => true, "create" => true, "delete" => true, "update" => true }
"connector_definition" => { "read" => true, "create" => true, "delete" => true, "update" => true },
"data_app" => { "read" => true, "create" => true, "delete" => true, "update" => true }
}
}
end
Expand All @@ -70,7 +72,8 @@
"connector" => { "read" => true, "create" => false, "delete" => false, "update" => false },
"workspace" => { "read" => true, "create" => false, "delete" => false, "update" => false },
"sync_record" => { "read" => true, "create" => false, "delete" => false, "update" => false },
"connector_definition" => { "read" => true, "create" => false, "delete" => false, "update" => false }
"connector_definition" => { "read" => true, "create" => false, "delete" => false, "update" => false },
"data_app" => { "read" => true, "create" => false, "delete" => false, "update" => false }
}
}
end
Expand Down
22 changes: 22 additions & 0 deletions server/spec/factories/visual_components.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

FactoryBot.define do
factory :visual_component do
component_type { "doughnut" }
name { "Sales Chart" }
model_id { "1" }
properties do
{
color: "blue"
}
end
feedback_config do
{
enabled: true
}
end
association :model
association :workspace
association :data_app
end
end
41 changes: 33 additions & 8 deletions server/spec/models/data_app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
require "rails_helper"

RSpec.describe DataApp, type: :model do
it { should validate_presence_of(:workspace_id) }
it { should validate_presence_of(:status) }
it { should validate_presence_of(:name) }
subject { create(:data_app) }

it { should define_enum_for(:status).with_values(inactive: 0, active: 1, draft: 2) }

it { should belong_to(:workspace) }
it { should have_many(:visual_components).dependent(:destroy) }
it { should have_many(:models).through(:visual_components) }
context "validations" do
it { should validate_presence_of(:workspace_id) }
it { should validate_presence_of(:status) }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:data_app_token) }
it { should define_enum_for(:status).with_values(inactive: 0, active: 1, draft: 2) }
it { should belong_to(:workspace) }
it { should have_many(:visual_components).dependent(:destroy) }
it { should have_many(:models).through(:visual_components) }
end

describe "#set_default_status" do
let(:data_app) { DataApp.new }
Expand All @@ -20,4 +23,26 @@
expect(data_app.status).to eq("draft")
end
end

describe "#generate_data_app_token" do
let(:workspace) { create(:workspace) }
let(:data_app) { create(:data_app, workspace:) }

it "generates a unique token before creation" do
expect(data_app.data_app_token).to be_present
end

it "ensures the token is unique" do
second_data_app = create(:data_app, workspace:)
expect(second_data_app.data_app_token).not_to eq(data_app.data_app_token)
end

it "should raise an error if token is not unique" do
allow(DataApp).to receive(:exists?).and_return(true, false)

unique_token = data_app.send(:generate_unique_token)
expect(unique_token).to be_present
expect(unique_token).not_to eq(data_app.data_app_token)
end
end
end
2 changes: 1 addition & 1 deletion server/spec/models/visual_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
it { should validate_presence_of(:model_id) }
it { should validate_presence_of(:data_app_id) }

it { should define_enum_for(:component_type).with_values(pie: 0, bar: 1, data_table: 2) }
it { should define_enum_for(:component_type).with_values(doughnut: 0, bar: 1, data_table: 2) }

it { should belong_to(:workspace) }
it { should belong_to(:data_app) }
Expand Down

0 comments on commit 31ec315

Please sign in to comment.