diff --git a/backend/src/app/rpc/commands/webhooks.clj b/backend/src/app/rpc/commands/webhooks.clj index 13a5d0210..2649a73a4 100644 --- a/backend/src/app/rpc/commands/webhooks.clj +++ b/backend/src/app/rpc/commands/webhooks.clj @@ -8,7 +8,7 @@ (:require [app.common.data.macros :as dm] [app.common.exceptions :as ex] - [app.common.spec :as us] + [app.common.schema :as sm] [app.common.uri :as u] [app.common.uuid :as uuid] [app.db :as db] @@ -19,7 +19,6 @@ [app.rpc.doc :as-alias doc] [app.util.services :as sv] [app.util.time :as dt] - [clojure.spec.alpha :as s] [cuerdas.core :as str])) (defn decode-row @@ -29,18 +28,6 @@ ;; --- Mutation: Create Webhook -(s/def ::team-id ::us/uuid) -(s/def ::uri ::us/uri) -(s/def ::is-active ::us/boolean) -(s/def ::mtype - #{"application/json" - "application/transit+json"}) - -(s/def ::create-webhook - (s/keys :req [::rpc/profile-id] - :req-un [::team-id ::uri ::mtype] - :opt-un [::is-active])) - ;; NOTE: for now the quote is hardcoded but this need to be solved in ;; a more universal way for handling properly object quotes (def max-hooks-for-team 8) @@ -99,31 +86,49 @@ {::db/return-keys true}) (decode-row))) + +(def valid-mtypes + #{"application/json" + "application/transit+json"}) + +(def ^:private schema:create-webhook + [:map {:title "create-webhook"} + [:team-id ::sm/uuid] + [:uri ::sm/uri] + [:mtype [::sm/one-of {:format "string"} valid-mtypes]]]) + (sv/defmethod ::create-webhook - {::doc/added "1.17"} + {::doc/added "1.17" + ::sm/params schema:create-webhook} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id] :as params}] (check-edition-permissions! pool profile-id team-id) (validate-quotes! cfg params) (validate-webhook! cfg nil params) (insert-webhook! cfg params)) -(s/def ::update-webhook - (s/keys :req-un [::id ::uri ::mtype ::is-active])) +(def ^:private schema:update-webhook + [:map {:title "update-webhook"} + [:id ::sm/uuid] + [:uri ::sm/uri] + [:mtype [::sm/one-of {:format "string"} valid-mtypes]] + [:is-active :boolean]]) (sv/defmethod ::update-webhook - {::doc/added "1.17"} + {::doc/added "1.17" + ::sm/params schema:update-webhook} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id id] :as params}] (let [whook (-> (db/get pool :webhook {:id id}) (decode-row))] (check-edition-permissions! pool profile-id (:team-id whook)) (validate-webhook! cfg whook params) (update-webhook! cfg whook params))) -(s/def ::delete-webhook - (s/keys :req [::rpc/profile-id] - :req-un [::id])) +(def ^:private schema:delete-webhook + [:map {:title "delete-webhook"} + [:id ::sm/uuid]]) (sv/defmethod ::delete-webhook - {::doc/added "1.17"} + {::doc/added "1.17" + ::sm/params schema:delete-webhook} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id id]}] (db/with-atomic [conn pool] (let [whook (-> (db/get conn :webhook {:id id}) decode-row)] @@ -133,16 +138,17 @@ ;; --- Query: Webhooks -(s/def ::team-id ::us/uuid) -(s/def ::get-webhooks - (s/keys :req [::rpc/profile-id] - :req-un [::team-id])) - (def sql:get-webhooks "select id, uri, mtype, is_active, error_code, error_count from webhook where team_id = ? order by uri") +(def ^:private schema:get-webhooks + [:map {:title "get-webhooks"} + [:team-id ::sm/uuid]]) + (sv/defmethod ::get-webhooks + {::doc/added "1.17" + ::sm/params schema:get-webhooks} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id]}] (dm/with-open [conn (db/open pool)] (check-read-permissions! conn profile-id team-id) diff --git a/backend/test/backend_tests/rpc_webhooks_test.clj b/backend/test/backend_tests/rpc_webhooks_test.clj index 76c3de763..f47472a73 100644 --- a/backend/test/backend_tests/rpc_webhooks_test.clj +++ b/backend/test/backend_tests/rpc_webhooks_test.clj @@ -39,6 +39,8 @@ (t/is (nil? (:error out))) (t/is (= 1 (:call-count @http-mock))) + ;; (th/print-result! out) + (let [result (:result out)] (t/is (contains? result :id)) (t/is (contains? result :team-id))