diff --git a/backend/src/app/rpc/commands/teams.clj b/backend/src/app/rpc/commands/teams.clj index 2cf06564c4..a9568dc571 100644 --- a/backend/src/app/rpc/commands/teams.clj +++ b/backend/src/app/rpc/commands/teams.clj @@ -114,18 +114,6 @@ ;; --- Query: Teams -(declare get-teams) - -(def ^:private schema:get-teams - [:map {:title "get-teams"}]) - -(sv/defmethod ::get-teams - {::doc/added "1.17" - ::sm/params schema:get-teams} - [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id] :as params}] - (dm/with-open [conn (db/open pool)] - (get-teams conn profile-id))) - (def sql:get-teams-with-permissions "SELECT t.*, tp.is_owner, @@ -191,6 +179,37 @@ (->> (db/exec! conn [sql (:default-team-id profile) profile-id]) (into [] xform:process-teams)))) +(def ^:private schema:get-teams + [:map {:title "get-teams"}]) + +(sv/defmethod ::get-teams + {::doc/added "1.17" + ::sm/params schema:get-teams} + [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id] :as params}] + (dm/with-open [conn (db/open pool)] + (get-teams conn profile-id))) + +(def ^:private sql:get-owned-teams + "SELECT t.id, t.name, + (SELECT count(*) FROM team_profile_rel WHERE team_id=t.id) AS total_members + FROM team AS t + JOIN team_profile_rel AS tpr ON (tpr.team_id = t.id) + WHERE t.is_default IS false + AND tpr.is_owner IS true + AND tpr.profile_id = ? + AND t.deleted_at IS NULL") + +(defn- get-owned-teams + [cfg profile-id] + (->> (db/exec! cfg [sql:get-owned-teams profile-id]) + (into [] (map decode-row)))) + +(sv/defmethod ::get-owned-teams + {::doc/added "2.8.0" + ::sm/params schema:get-teams} + [cfg {:keys [::rpc/profile-id]}] + (get-owned-teams cfg profile-id)) + ;; --- Query: Team (by ID) (declare get-team) diff --git a/backend/test/backend_tests/rpc_team_test.clj b/backend/test/backend_tests/rpc_team_test.clj index 66fa5d74ce..5925394109 100644 --- a/backend/test/backend_tests/rpc_team_test.clj +++ b/backend/test/backend_tests/rpc_team_test.clj @@ -449,6 +449,23 @@ (t/is (nil? res))))) +(t/deftest get-owned-teams + (let [profile1 (th/create-profile* 1 {:is-active true}) + profile2 (th/create-profile* 2 {:is-active true}) + team1 (th/create-team* 1 {:profile-id (:id profile1)}) + team2 (th/create-team* 2 {:profile-id (:id profile2)}) + + params {::th/type :get-owned-teams + ::rpc/profile-id (:id profile1)} + out (th/command! params)] + + (t/is (th/success? out)) + (let [result (:result out)] + (t/is (= 1 (count result))) + (t/is (= (:id team1) (-> result first :id))) + (t/is (not= (:default-team-id profile1) (-> result first :id)))))) + + (t/deftest team-deletion-1 (let [profile1 (th/create-profile* 1 {:is-active true}) team (th/create-team* 1 {:profile-id (:id profile1)})