From a923d396030e82fdcb1df169ed008894bfc33078 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 9 Dec 2024 09:48:35 +0100 Subject: [PATCH] :bug: Fix incorrect teams query on profile deletion The current approach prevents profile deletion when there are some extra (soft)deleted teams where the profile is owner --- backend/src/app/rpc/commands/profile.clj | 7 +++++- .../test/backend_tests/rpc_profile_test.clj | 23 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/backend/src/app/rpc/commands/profile.clj b/backend/src/app/rpc/commands/profile.clj index 57034c461..7c7ca3339 100644 --- a/backend/src/app/rpc/commands/profile.clj +++ b/backend/src/app/rpc/commands/profile.clj @@ -422,7 +422,9 @@ :deleted-at deleted-at :id profile-id}}) - (rph/with-transform {} (session/delete-fn cfg))))) + + (-> (rph/wrap nil) + (rph/with-transform (session/delete-fn cfg)))))) ;; --- HELPERS @@ -431,8 +433,11 @@ "WITH owner_teams AS ( SELECT tpr.team_id AS id FROM team_profile_rel AS tpr + JOIN team AS t ON (t.id = tpr.team_id) WHERE tpr.is_owner IS TRUE AND tpr.profile_id = ? + AND (t.deleted_at IS NULL OR + t.deleted_at > now()) ) SELECT tpr.team_id AS id, count(tpr.profile_id) - 1 AS participants diff --git a/backend/test/backend_tests/rpc_profile_test.clj b/backend/test/backend_tests/rpc_profile_test.clj index 1bd49db48..47e58adba 100644 --- a/backend/test/backend_tests/rpc_profile_test.clj +++ b/backend/test/backend_tests/rpc_profile_test.clj @@ -203,7 +203,24 @@ edata (ex-data error)] (t/is (th/ex-info? error)) (t/is (= (:type edata) :validation)) - (t/is (= (:code edata) :owner-teams-with-people)))))) + (t/is (= (:code edata) :owner-teams-with-people))) + + (let [params {::th/type :delete-team + ::rpc/profile-id (:id prof1) + :id (:id team1)} + out (th/command! params)] + ;; (th/print-result! out) + + (let [team (th/db-get :team {:id (:id team1)} {::db/remove-deleted false})] + (t/is (dt/instant? (:deleted-at team))))) + + ;; Request profile to be deleted + (let [params {::th/type :delete-profile + ::rpc/profile-id (:id prof1)} + out (th/command! params)] + ;; (th/print-result! out) + (t/is (nil? (:result out))) + (t/is (nil? (:error out))))))) (t/deftest profile-deletion-3 (let [prof1 (th/create-profile* 1) @@ -291,7 +308,7 @@ out (th/command! params)] ;; (th/print-result! out) - (t/is (= {} (:result out))) + (t/is (nil? (:result out))) (t/is (nil? (:error out)))) ;; query files after profile soft deletion @@ -336,7 +353,7 @@ ::rpc/profile-id (:id prof1)} out (th/command! params)] ;; (th/print-result! out) - (t/is (= {} (:result out))) + (t/is (nil? (:result out))) (t/is (nil? (:error out)))) (th/run-pending-tasks!)