From 1dc7ce526eda5410d14f1cb6e2fb9b68bede953e Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Tue, 19 Jul 2022 02:03:06 +0700 Subject: [PATCH] chore: Add ee helper, custom_attributes to account (#5058) --- .env.example | 4 ++++ Gemfile | 3 +++ Gemfile.lock | 4 +++- app/javascript/dashboard/api/ApiClient.js | 5 +++++ app/javascript/dashboard/store/modules/inboxes.js | 7 +------ app/javascript/dashboard/store/utils/api.js | 5 +++++ .../dashboard/store/utils/specs/api.spec.js | 12 ++++++++++++ app/models/account.rb | 1 + app/views/api/v1/accounts/show.json.jbuilder | 9 +-------- app/views/api/v1/accounts/update.json.jbuilder | 8 +------- app/views/api/v1/models/_account.json.jbuilder | 10 ++++++++++ config/initializers/stripe.rb | 3 +++ ...0220718123938_add_custom_attributes_to_account.rb | 5 +++++ db/schema.rb | 3 ++- 14 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 app/views/api/v1/models/_account.json.jbuilder create mode 100644 config/initializers/stripe.rb create mode 100644 db/migrate/20220718123938_add_custom_attributes_to_account.rb diff --git a/.env.example b/.env.example index d46acac9f..5291be660 100644 --- a/.env.example +++ b/.env.example @@ -195,3 +195,7 @@ USE_INBOX_AVATAR_FOR_BOT=true # If you want to use official mobile app, # the notifications would be relayed via a Chatwoot server ENABLE_PUSH_RELAY_SERVER=true + +# Stripe API key +STRIPE_SECRET_KEY= +STRIPE_WEBHOOK_SECRET= diff --git a/Gemfile b/Gemfile index 270e6527c..935db42e2 100644 --- a/Gemfile +++ b/Gemfile @@ -127,6 +127,9 @@ gem 'working_hours' # full text search for articles gem 'pg_search' +# Subscriptions, Billing +gem 'stripe' + group :production, :staging do # we dont want request timing out in development while using byebug gem 'rack-timeout' diff --git a/Gemfile.lock b/Gemfile.lock index 7e0583757..17e021bf2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -616,6 +616,7 @@ GEM sprockets (>= 3.0.0) squasher (0.6.2) statsd-ruby (1.5.0) + stripe (6.5.0) telephone_number (1.4.16) thor (1.2.1) tilt (2.0.10) @@ -769,6 +770,7 @@ DEPENDENCIES spring spring-watcher-listen squasher + stripe telephone_number time_diff twilio-ruby (~> 5.66) @@ -787,4 +789,4 @@ RUBY VERSION ruby 3.0.4p208 BUNDLED WITH - 2.3.16 + 2.3.14 diff --git a/app/javascript/dashboard/api/ApiClient.js b/app/javascript/dashboard/api/ApiClient.js index 097caa906..25083dc99 100644 --- a/app/javascript/dashboard/api/ApiClient.js +++ b/app/javascript/dashboard/api/ApiClient.js @@ -15,6 +15,11 @@ class ApiClient { baseUrl() { let url = this.apiVersion; + + if (this.options.enterprise) { + url = `/enterprise${url}`; + } + if (this.options.accountScoped) { const isInsideAccountScopedURLs = window.location.pathname.includes( '/app/accounts' diff --git a/app/javascript/dashboard/store/modules/inboxes.js b/app/javascript/dashboard/store/modules/inboxes.js index 7821e6c36..8d97f088a 100644 --- a/app/javascript/dashboard/store/modules/inboxes.js +++ b/app/javascript/dashboard/store/modules/inboxes.js @@ -5,7 +5,7 @@ import InboxesAPI from '../../api/inboxes'; import WebChannel from '../../api/channel/webChannel'; import FBChannel from '../../api/channel/fbChannel'; import TwilioChannel from '../../api/channel/twilioChannel'; -import { parseAPIErrorResponse } from '../utils/api'; +import { throwErrorMessage } from '../utils/api'; const buildInboxData = inboxParams => { const formData = new FormData(); @@ -43,11 +43,6 @@ export const state = { }, }; -const throwErrorMessage = error => { - const errorMessage = parseAPIErrorResponse(error); - throw new Error(errorMessage); -}; - export const getters = { getInboxes($state) { return $state.records; diff --git a/app/javascript/dashboard/store/utils/api.js b/app/javascript/dashboard/store/utils/api.js index 55e43fe2f..292ebca7e 100644 --- a/app/javascript/dashboard/store/utils/api.js +++ b/app/javascript/dashboard/store/utils/api.js @@ -55,3 +55,8 @@ export const parseAPIErrorResponse = error => { } return error; }; + +export const throwErrorMessage = error => { + const errorMessage = parseAPIErrorResponse(error); + throw new Error(errorMessage); +}; diff --git a/app/javascript/dashboard/store/utils/specs/api.spec.js b/app/javascript/dashboard/store/utils/specs/api.spec.js index b076b7be3..62a59dde5 100644 --- a/app/javascript/dashboard/store/utils/specs/api.spec.js +++ b/app/javascript/dashboard/store/utils/specs/api.spec.js @@ -2,6 +2,7 @@ import { getLoadingStatus, parseAPIErrorResponse, setLoadingStatus, + throwErrorMessage, } from '../api'; describe('#getLoadingStatus', () => { @@ -37,3 +38,14 @@ describe('#parseAPIErrorResponse', () => { ); }); }); + +describe('#throwErrorMessage', () => { + it('throws correct error', () => { + const errorFn = function throwErrorMessageFn() { + throwErrorMessage({ + response: { data: { message: 'Error Message [message]' } }, + }); + }; + expect(errorFn).toThrow('Error Message [message]'); + }); +}); diff --git a/app/models/account.rb b/app/models/account.rb index 5ece265bb..678cc3445 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -4,6 +4,7 @@ # # id :integer not null, primary key # auto_resolve_duration :integer +# custom_attributes :jsonb # domain :string(100) # feature_flags :integer default(0), not null # limits :jsonb diff --git a/app/views/api/v1/accounts/show.json.jbuilder b/app/views/api/v1/accounts/show.json.jbuilder index 39ca92955..2bbdda576 100644 --- a/app/views/api/v1/accounts/show.json.jbuilder +++ b/app/views/api/v1/accounts/show.json.jbuilder @@ -1,9 +1,2 @@ -json.id @account.id -json.name @account.name -json.locale @account.locale -json.domain @account.domain -json.custom_email_domain_enabled @account.custom_email_domain_enabled -json.support_email @account.support_email -json.features @account.all_features -json.auto_resolve_duration @account.auto_resolve_duration +json.partial! 'api/v1/models/account.json.jbuilder', resource: @account json.latest_chatwoot_version @latest_chatwoot_version diff --git a/app/views/api/v1/accounts/update.json.jbuilder b/app/views/api/v1/accounts/update.json.jbuilder index 68acc570a..c1905c972 100644 --- a/app/views/api/v1/accounts/update.json.jbuilder +++ b/app/views/api/v1/accounts/update.json.jbuilder @@ -1,7 +1 @@ -json.id @account.id -json.name @account.name -json.locale @account.locale -json.domain @account.domain -json.custom_email_domain_enabled @account.custom_email_domain_enabled -json.support_email @account.support_email -json.features @account.enabled_features +json.partial! 'api/v1/models/account.json.jbuilder', resource: @account diff --git a/app/views/api/v1/models/_account.json.jbuilder b/app/views/api/v1/models/_account.json.jbuilder new file mode 100644 index 000000000..bc28f7a27 --- /dev/null +++ b/app/views/api/v1/models/_account.json.jbuilder @@ -0,0 +1,10 @@ +json.auto_resolve_duration resource.auto_resolve_duration +json.created_at resource.created_at +json.custom_attributes resource.custom_attributes +json.custom_email_domain_enabled @account.custom_email_domain_enabled +json.domain @account.domain +json.features @account.enabled_features +json.id @account.id +json.locale @account.locale +json.name @account.name +json.support_email @account.support_email diff --git a/config/initializers/stripe.rb b/config/initializers/stripe.rb new file mode 100644 index 000000000..585d9dad4 --- /dev/null +++ b/config/initializers/stripe.rb @@ -0,0 +1,3 @@ +require 'stripe' + +Stripe.api_key = ENV.fetch('STRIPE_SECRET_KEY', nil) diff --git a/db/migrate/20220718123938_add_custom_attributes_to_account.rb b/db/migrate/20220718123938_add_custom_attributes_to_account.rb new file mode 100644 index 000000000..39e9abcea --- /dev/null +++ b/db/migrate/20220718123938_add_custom_attributes_to_account.rb @@ -0,0 +1,5 @@ +class AddCustomAttributesToAccount < ActiveRecord::Migration[6.1] + def change + add_column :accounts, :custom_attributes, :jsonb, default: {} + end +end diff --git a/db/schema.rb b/db/schema.rb index cd7d4eac7..67de25ec9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_07_06_085458) do +ActiveRecord::Schema.define(version: 2022_07_18_123938) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -53,6 +53,7 @@ ActiveRecord::Schema.define(version: 2022_07_06_085458) do t.integer "feature_flags", default: 0, null: false t.integer "auto_resolve_duration" t.jsonb "limits", default: {} + t.jsonb "custom_attributes", default: {} end create_table "action_mailbox_inbound_emails", force: :cascade do |t|