From 05c0f8d69f02b96c3e16db01c76313f9ec83c642 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 16 Apr 2025 12:48:05 +0200 Subject: [PATCH] :tada: Add update-customer-subscription prepl method --- backend/src/app/srepl/cli.clj | 76 +++++++++++++++++++++++++++++++ common/src/app/common/schema.cljc | 16 +++++++ 2 files changed, 92 insertions(+) diff --git a/backend/src/app/srepl/cli.clj b/backend/src/app/srepl/cli.clj index 5eff92bb3..c1b9e7102 100644 --- a/backend/src/app/srepl/cli.clj +++ b/backend/src/app/srepl/cli.clj @@ -148,3 +148,79 @@ :name (get profile :fullname) :email (get profile :email) :subscription (get props :subscription)}))) + +(def ^:private schema:customer-subscription + [:map {:title "CustomerSubscription"} + [:id ::sm/uuid] + [:customer-id ::sm/uuid] + [:type [:enum + "unlimited" + "professional" + "enterprise"]] + [:status [:enum + "active" + "canceled" + "incomplete" + "incomplete_expired" + "pass_due" + "paused" + "trialing" + "unpaid"]] + + [:billing-period [:enum + "month" + "day" + "week" + "year"]] + [:quantity :int] + [:description [:maybe ::sm/text]] + [:created-at ::sm/timestamp] + [:start-date [:maybe ::sm/timestamp]] + [:ended-at [:maybe ::sm/timestamp]] + [:trial-end [:maybe ::sm/timestamp]] + [:trial-start [:maybe ::sm/timestamp]] + [:cancel-at [:maybe ::sm/timestamp]] + [:canceled-at [:maybe ::sm/timestamp]] + + [:current-period-end ::sm/timestamp] + [:current-period-start ::sm/timestamp] + [:cancel-at-period-end :boolean] + + [:cancellation-details + [:map {:title "CancellationDetails"} + [:comment [:maybe ::sm/text]] + [:reason [:maybe ::sm/text]] + [:feedback [:maybe + [:enum + "customer_service" + "low_quality" + "missing_feature" + "other" + "switched_service" + "too_complex" + "too_expensive" + "unused"]]]]]]) + +(def ^:private schema:update-customer-subscription + [:map + [:id ::sm/uuid] + [:subscription schema:customer-subscription]]) + +(def coerce-update-customer-subscription-params + (coercer schema:update-customer-subscription + :type :validation + :hint "invalid data provided for `update-customer-subscription` rpc call")) + +(defmethod exec-command "update-customer-subscription" + [params] + (when-let [system (get-current-system)] + (let [{:keys [id subscription]} (coerce-update-customer-subscription-params params) + ;; FIXME: locking + {:keys [props] :as profile} (cmd.profile/get-profile system id) + props (assoc props :subscription subscription)] + + (db/update! system :profile + {:props (db/tjson props)} + {:id id} + {::db/return-keys false}) + true))) diff --git a/common/src/app/common/schema.cljc b/common/src/app/common/schema.cljc index 8f68ea051..50a317cda 100644 --- a/common/src/app/common/schema.cljc +++ b/common/src/app/common/schema.cljc @@ -907,6 +907,22 @@ ::oapi/type "string" ::oapi/format "iso"}}) +(register! + {:type ::timestamp + :pred inst? + :type-properties + {:title "inst" + :description "Satisfies Inst protocol" + :error/message "should be an instant" + :gen/gen (->> (sg/small-int) + (sg/fmap (fn [v] (tm/parse-instant v)))) + :decode/string tm/parse-instant + :encode/string inst-ms + :decode/json tm/parse-instant + :encode/json inst-ms + ::oapi/type "string" + ::oapi/format "number"}}) + (register! {:type ::fn :pred fn?})