Skip to content
Snippets Groups Projects
Unverified Commit 4b58eb98 authored by bryan's avatar bryan Committed by GitHub
Browse files

Return info about the proper billing status on the license page (#44855)

* return data about the proper billing status

* fix copy

* fix the tests
parent 95817d08
No related branches found
No related tags found
No related merge requests found
......@@ -4,10 +4,13 @@
[cheshire.core :as json]
[clj-http.client :as http]
[clojure.core.memoize :as memoize]
[clojure.string :as str]
[compojure.core :as compojure :refer [GET]]
[java-time.api :as t]
[metabase.api.common :as api]
[metabase.public-settings.premium-features :as premium-features]
[metabase.util :as u]
[metabase.util.date-2.parse :as u.date.parse]
[metabase.util.i18n :as i18n]
[toucan2.core :as t2])
(:import
......@@ -31,6 +34,26 @@
{:content nil})))
:ttl/threshold (u/hours->ms 5)))
(defn- valid-thru []
(some->> (premium-features/premium-embedding-token)
premium-features/fetch-token-status
:valid-thru
u.date.parse/parse
(t/format "MMMM d, YYYY")))
(defn billing-status
"Returns content that powers the billing page in certain circumstances."
[]
(let [max-users (premium-features/max-users-allowed)
;; There is a defsetting for user count, but it is only updated every 5 minutes, and this should be exactly up
;; to date here:
total-users (t2/count :model/User :is_active true, :type :personal)]
{:version "v1"
:content [{:name "Users included in your plan" :value max-users :format "integer" :display "value"}
{:name "Users available" :value (- max-users total-users) :format "integer" :display "value"}
{:name "Token expiration date" :value (valid-thru) :format "string" :display "value"}
{:name "Plan" :value "Metabase Enterprise Airgap" :format "string" :display "value"}]}))
(api/defendpoint GET "/"
"Get billing information. This acts as a proxy between `metabase-billing-info-url` and the client,
using the embedding token and signed in user's email to fetch the billing information."
......@@ -38,6 +61,8 @@
(let [token (premium-features/premium-embedding-token)
email (t2/select-one-fn :email :model/User :id api/*current-user-id*)
language (i18n/user-locale-string)]
(fetch-billing-status* token email language)))
(if (and token (str/starts-with? token "airgap_"))
(billing-status)
(fetch-billing-status* token email language))))
(api/define-routes)
......@@ -5,17 +5,19 @@
(deftest fetch-billing-status-test
(testing "Passes through billing status fetched from server"
(binding [http/request (fn [& _]
{:status 200
:body "{\"version\":\"v1\",\"content\":null}"})]
(is (= {:version "v1"
:content nil}
(mt/user-http-request :rasta :get 200 "/ee/billing"))))))
(mt/with-temporary-setting-values [premium-embedding-token nil]
(binding [http/request (fn [& _]
{:status 200
:body "{\"version\":\"v1\",\"content\":null}"})]
(is (= {:version "v1"
:content nil}
(mt/user-http-request :rasta :get 200 "/ee/billing")))))))
(deftest fetch-billing-status-error-test
(testing "When receiving a non json result consume the error and return an empty content blob"
(binding [http/request (fn [& _]
{:status 404
:body "error"})]
(is (= {:content nil}
(mt/user-http-request :crowberto :get 200 "/ee/billing"))))))
(mt/with-temporary-setting-values [premium-embedding-token nil]
(binding [http/request (fn [& _]
{:status 404
:body "error"})]
(is (= {:content nil}
(mt/user-http-request :crowberto :get 200 "/ee/billing")))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment