diff --git a/backend/src/app/nitrate.clj b/backend/src/app/nitrate.clj index d0d8c3e219..af1e915903 100644 --- a/backend/src/app/nitrate.clj +++ b/backend/src/app/nitrate.clj @@ -398,6 +398,20 @@ (generate-nitrate-uri "api/subscriptions/" profile-id) schema:subscription params)) +(def ^:private schema:profile-subscription + [:map {:title "ProfileSubscription"} + [:profile-id ::sm/uuid] + [:type ::sm/text] + [:status ::sm/text]]) + +(defn- get-subscriptions-api + [cfg {:keys [profile-ids] :as params}] + (let [params (assoc params :request-params {:profile-ids profile-ids})] + (request-to-nitrate cfg :post + (generate-nitrate-uri "api/subscriptions/bulk") + [:vector schema:profile-subscription] + params))) + (def ^:private schema:subscription-warning [:maybe [:map {:title "SubscriptionWarning"} @@ -533,6 +547,7 @@ :delete-team (partial delete-team-api cfg) :remove-team-from-organization (partial remove-team-from-organization-api cfg) :get-subscription (partial get-subscription-api cfg) + :get-subscriptions (partial get-subscriptions-api cfg) :get-subscription-warning (partial get-subscription-warning-api cfg) :connectivity (partial get-connectivity-api cfg) :get-identity (partial get-identity-api cfg) @@ -699,6 +714,47 @@ team)) teams)))) +(def ^:private valid-licence-statuses + "The statuses `app.main.data.nitrate/is-valid-license?` accepts on the + frontend." + #{"active" "past_due" "trialing"}) + +(defn- get-valid-licence-statuses + "Returns a map of profile id to the status of its valid Nitrate licence. + Returns an empty map when Nitrate does not answer: the licences of team + owners only decide which plan a team shows." + [cfg profile-ids params] + (try + (let [params (assoc (or params {}) :profile-ids (vec profile-ids)) + subscriptions (call cfg :get-subscriptions params)] + (into {} + (comp (filter #(contains? valid-licence-statuses (:status %))) + (map (juxt :profile-id :status))) + subscriptions)) + (catch Throwable cause + (l/wrn :hint "failed to get team owner licences" + :cause cause) + {}))) + +(defn add-owner-licence-to-teams + "Sets type \"nitrate\" in the `:subscription` of each team outside an + organization whose owner has a valid Nitrate licence, so every member + sees the plan of the owner, not only the owner. The database only knows + the Stripe subscription of the owner, which teams keep when Nitrate does + not answer. Teams in an organization take their plan from it." + [cfg teams params] + (let [owner-id #(when (nil? (:organization %)) + (get-in % [:subscription :owner-id])) + owner-ids (into #{} (keep owner-id) teams)] + (if (empty? owner-ids) + teams + (let [statuses (get-valid-licence-statuses cfg owner-ids params)] + (mapv (fn [team] + (if-let [status (get statuses (owner-id team))] + (update team :subscription assoc :type "nitrate" :status status) + team)) + teams))))) + (defn set-team-organization "Associates a team with an organization in Nitrate. Requires organization-id and is-default in params. diff --git a/backend/src/app/rpc/commands/teams.clj b/backend/src/app/rpc/commands/teams.clj index 9138eabb29..4ab171e08d 100644 --- a/backend/src/app/rpc/commands/teams.clj +++ b/backend/src/app/rpc/commands/teams.clj @@ -161,7 +161,8 @@ WHEN 'professional' THEN 'active' ELSE COALESCE(p.props->'~:subscription'->>'~:status', 'incomplete') END, - '~:seats', p.props->'~:subscription'->'~:quantity' + '~:seats', p.props->'~:subscription'->'~:quantity', + '~:owner-id', '~u' || p.id::text ) AS subscription FROM team_profile_rel AS tp JOIN team AS t ON (t.id = tp.team_id) @@ -211,8 +212,9 @@ (dm/with-open [conn (db/open pool)] (let [teams (get-teams conn profile-id)] (if (contains? cf/flags :admin-console) - (->> (nitrate/add-organization-info-to-teams cfg teams params) - (remove #(get-in % [:organization :expired-license]))) + (as-> (nitrate/add-organization-info-to-teams cfg teams params) teams + (remove #(get-in % [:organization :expired-license]) teams) + (nitrate/add-owner-licence-to-teams cfg teams params)) teams)))) (def ^:private sql:get-owned-teams @@ -258,7 +260,8 @@ [cfg {:keys [::rpc/profile-id id file-id] :as params}] (let [team (get-team cfg :profile-id profile-id :team-id id :file-id file-id)] (if (contains? cf/flags :admin-console) - (nitrate/add-organization-info-to-team cfg team params) + (let [team (nitrate/add-organization-info-to-team cfg team params)] + (first (nitrate/add-owner-licence-to-teams cfg [team] params))) team))) (defn- get-organization-owner-viewer-team diff --git a/backend/test/backend_tests/rpc_team_test.clj b/backend/test/backend_tests/rpc_team_test.clj index f562fc5980..6e4dd6fd4a 100644 --- a/backend/test/backend_tests/rpc_team_test.clj +++ b/backend/test/backend_tests/rpc_team_test.clj @@ -1210,6 +1210,127 @@ (t/is (= :get-teams-organizations (ffirst @calls))))))) +(defn- find-team + [teams team-id] + (d/seek #(= team-id (:id %)) teams)) + +(t/deftest get-teams-shows-owner-nitrate-licence-to-every-member + (let [member (th/create-profile* 1 {:is-active true}) + owner (th/create-profile* 2 {:is-active true}) + org-owner (th/create-profile* 3 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + organization-team (th/create-team* 2 {:profile-id (:id org-owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + _ (th/create-team-role* {:team-id (:id organization-team) + :profile-id (:id member) + :role :editor}) + calls (atom []) + nitrate-call (fn [_cfg method params] + (swap! calls conj [method params]) + (case method + :get-teams-organizations + [{:id (:id organization-team) + :is-your-penpot false + :organization {:id (uuid/random) + :name "Acme" + :slug "acme" + :owner-id (:id org-owner)}}] + :get-subscriptions + [{:profile-id (:id owner) :type "nitrate" :status "trialing"} + {:profile-id (:id org-owner) :type "nitrate" :status "active"}])) + params {::th/type :get-teams + ::rpc/profile-id (:id member)}] + (with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions) + nitrate/call nitrate-call] + (let [out (th/command! params) + teams (:result out)] + (t/is (th/success? out)) + (t/is (= [:get-teams-organizations :get-subscriptions] (mapv first @calls))) + ;; One request with the owners of the teams outside an organization, + ;; the member included as owner of its default team. + (t/is (= #{(:id owner) (:id member)} + (-> @calls second second :profile-ids set))) + (t/is (= {:type "nitrate" :status "trialing"} + (-> (find-team teams (:id team)) + :subscription + (select-keys [:type :status])))) + ;; A team in an organization keeps the Stripe plan of its owner. + (t/is (= "professional" + (-> (find-team teams (:id organization-team)) :subscription :type))) + (t/is (= "professional" + (-> (find-team teams (:default-team-id member)) :subscription :type))))))) + +(t/deftest get-teams-ignores-owner-nitrate-licence-that-is-not-valid + (let [member (th/create-profile* 1 {:is-active true}) + owner (th/create-profile* 2 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + nitrate-call (fn [_cfg method _params] + (case method + :get-teams-organizations [] + :get-subscriptions [{:profile-id (:id owner) + :type "nitrate" + :status "canceled"}])) + params {::th/type :get-teams + ::rpc/profile-id (:id member)}] + (with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions) + nitrate/call nitrate-call] + (let [out (th/command! params)] + (t/is (th/success? out)) + (t/is (= "professional" + (-> (find-team (:result out) (:id team)) :subscription :type))))))) + +(t/deftest get-teams-keeps-stripe-plan-when-owner-licences-fail + (let [member (th/create-profile* 1 {:is-active true}) + owner (th/create-profile* 2 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + nitrate-call (fn [_cfg method _params] + (case method + :get-teams-organizations [] + :get-subscriptions (throw (ex-info "nitrate is down" + {:type :nitrate-unavailable})))) + params {::th/type :get-teams + ::rpc/profile-id (:id member)}] + (with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions) + nitrate/call nitrate-call] + (let [out (th/command! params)] + (t/is (th/success? out)) + (t/is (= "professional" + (-> (find-team (:result out) (:id team)) :subscription :type))))))) + +(t/deftest get-team-shows-owner-nitrate-licence-to-a-member + (let [member (th/create-profile* 1 {:is-active true}) + owner (th/create-profile* 2 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + calls (atom []) + nitrate-call (fn [_cfg method params] + (swap! calls conj [method params]) + (case method + :get-team-organization nil + :get-subscriptions [{:profile-id (:id owner) + :type "nitrate" + :status "active"}])) + params {::th/type :get-team + ::rpc/profile-id (:id member) + :id (:id team)}] + (with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions) + nitrate/call nitrate-call] + (let [out (th/command! params)] + (t/is (th/success? out)) + (t/is (= [(:id owner)] (-> (last @calls) second :profile-ids))) + (t/is (= "nitrate" (-> out :result :subscription :type))))))) + + (t/deftest team-deletion-1 (let [profile1 (th/create-profile* 1 {:is-active true}) team (th/create-team* 1 {:profile-id (:id profile1)}) diff --git a/frontend/src/app/main/ui/dashboard/subscription.cljs b/frontend/src/app/main/ui/dashboard/subscription.cljs index 61c96506bc..d983f789ac 100644 --- a/frontend/src/app/main/ui/dashboard/subscription.cljs +++ b/frontend/src/app/main/ui/dashboard/subscription.cljs @@ -42,11 +42,13 @@ (get-subscription-type (dm/get-in profile [:props :subscription])))) (defn show-team-plan? - [is-owner team profile] - (not (and is-owner - (nil? (dm/get-in team [:organization :id])) - (contains? #{"enterprise" "nitrate"} - (get-profile-subscription-type profile))))) + "The plan of a team outside an organization is the plan of its owner, + which the backend puts in the team `:subscription`. It is hidden from + every member when the owner has an Enterprise or Nitrate plan." + [team] + (or (some? (dm/get-in team [:organization :id])) + (not (contains? #{"enterprise" "nitrate"} + (get-subscription-type (:subscription team)))))) (mf/defc cta-power-up* [{:keys [top-title top-description bottom-description bottom-button bottom-button-href has-dropdown is-highlighted]}] @@ -330,7 +332,7 @@ "enterprise" (tr "subscription.current-plan.enterprise"))]])) (mf/defc team* - [{:keys [is-owner team profile]}] + [{:keys [is-owner team]}] (let [subscription (:subscription team) subscription-type (get-subscription-type subscription) team-plan-type (get-team-plan-type team) @@ -348,7 +350,7 @@ href (str "payments/subscriptions/show?returnUrl=" href)] (st/emit! (rt/nav-raw :href href)))))] - (when (show-team-plan? is-owner team profile) + (when (show-team-plan? team) [:div {:class (stl/css :team)} [:div {:class (stl/css :team-label)} (tr "subscription.dashboard.team-plan")] diff --git a/frontend/src/app/main/ui/dashboard/team.cljs b/frontend/src/app/main/ui/dashboard/team.cljs index 0055548533..43aa9994e3 100644 --- a/frontend/src/app/main/ui/dashboard/team.cljs +++ b/frontend/src/app/main/ui/dashboard/team.cljs @@ -1780,5 +1780,4 @@ (when (contains? cfg/flags :subscriptions) [:> team* {:is-owner (:is-owner permissions) - :team team - :profile profile}])]]])) + :team team}])]]])) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index a0b6bce8a7..0603e05f5a 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -379,6 +379,7 @@ 'frontend-tests.ui.sortable-auto-scroll-test 'frontend-tests.ui.static-test 'frontend-tests.ui.stroke-menu-test + 'frontend-tests.ui.subscription-test 'frontend-tests.ui.team-container-test 'frontend-tests.ui.text-attrs-multiple-test 'frontend-tests.ui.viewport-top-bar-test diff --git a/frontend/test/frontend_tests/ui/subscription_test.cljs b/frontend/test/frontend_tests/ui/subscription_test.cljs index f85628dd15..8724d47c31 100644 --- a/frontend/test/frontend_tests/ui/subscription_test.cljs +++ b/frontend/test/frontend_tests/ui/subscription_test.cljs @@ -6,7 +6,6 @@ (ns frontend-tests.ui.subscription-test (:require - [app.main.data.nitrate :as dnt] [app.main.ui.dashboard.subscription :as ds] [cljs.test :as t :include-macros true])) @@ -23,45 +22,26 @@ (ds/get-team-plan-type {:subscription {:type owner-plan :status "active"}}))))) -(t/deftest owner-enterprise-plan-is-hidden-for-team-outside-organization - (t/is (false? (ds/show-team-plan? true - {:subscription {:type "enterprise" - :status "active"}} - {:props {:subscription {:type "enterprise" - :status "active"}}})))) - -(t/deftest team-plan-is-visible-for-other-owner-and-organization-cases +(t/deftest team-plan-is-visible-when-owner-has-professional-or-unlimited-plan (doseq [owner-plan ["professional" "unlimited"]] - (t/is (true? (ds/show-team-plan? true - {:subscription {:type owner-plan - :status "active"}} - {:props {:subscription {:type owner-plan - :status "active"}}})))) - (t/is (true? (ds/show-team-plan? true - {:subscription {:type "enterprise" - :status "active"} - :organization {:id "org-1"}} - {:props {:subscription {:type "enterprise" - :status "active"}}}))) - (t/is (true? (ds/show-team-plan? false - {:subscription {:type "enterprise" - :status "active"}} - {:props {:subscription {:type "enterprise" - :status "active"}}})))) + (t/is (true? (ds/show-team-plan? {:subscription {:type owner-plan + :status "active"}}))))) -(t/deftest owner-enterprise-plan-is-detected-even-when-team-subscription-falls-back - (t/is (false? (ds/show-team-plan? true - {:subscription {:type "professional" - :status "active"}} - {:props {:subscription {:type "enterprise" - :status "trialing"}}})))) +(t/deftest team-plan-is-hidden-when-owner-of-team-outside-organization-has-enterprise-plan + ;; The team `:subscription` carries the plan of the owner, so the result + ;; is the same for the owner and for any other member. + (doseq [owner-plan ["enterprise" "nitrate"] + status ["active" "trialing" "past_due"]] + (t/is (false? (ds/show-team-plan? {:subscription {:type owner-plan + :status status}}))))) -(t/deftest nitrate-owner-plan-hides-team-plan-when-team-is-outside-organization - (with-redefs [dnt/is-valid-license? (constantly true)] - (t/is (false? (ds/show-team-plan? true - {:subscription {:type "professional" - :status "active"}} - {:subscription {:type "nitrate" - :status "active"} - :props {:subscription {:type "professional" - :status "active"}}}))))) +(t/deftest team-plan-is-visible-for-team-in-organization-whatever-the-owner-plan + (doseq [owner-plan ["professional" "unlimited" "enterprise" "nitrate"]] + (t/is (true? (ds/show-team-plan? {:subscription {:type owner-plan + :status "active"} + :organization {:id "org-1"}}))))) + +(t/deftest team-plan-is-visible-when-owner-enterprise-plan-is-no-longer-paid + (doseq [status ["unpaid" "canceled"]] + (t/is (true? (ds/show-team-plan? {:subscription {:type "enterprise" + :status status}})))))