Skip to content

Commit

Permalink
insurance: when activating policy, retract the removed instruments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramblurr committed May 10, 2024
1 parent e5cf201 commit dc5849f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/clj/app/insurance/controller.clj
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,17 @@

(defn mark-coverages-as! [req]
(let [{:keys [policy-id coverage-ids workflow-status change-status] :as p} (s/decode MarkAsSchema (:params req))]
;; (tap> {:mark-as p :valid? (s/valid? MarkAsSchema p)})
(if (s/valid? MarkAsSchema p)
(let [workflow-status-txs (if workflow-status (map (fn [cid]
[:db/add [:instrument.coverage/coverage-id cid] :instrument.coverage/status workflow-status]) coverage-ids)
[])
change-status-txs (if change-status (map (fn [cid]
[:db/add [:instrument.coverage/coverage-id cid] :instrument.coverage/change change-status]) coverage-ids)
[])
{:keys [db-after]} (d/transact-wrapper! req {:tx-data (concat change-status-txs workflow-status-txs)})]
txs (concat change-status-txs workflow-status-txs)
;; _ (tap> [:txs txs])
{:keys [db-after]} (d/transact-wrapper! req {:tx-data txs})]
{:policy (q/retrieve-policy db-after policy-id)
:db-after db-after})
(s/throw-error "Invalid arguments" nil MarkAsSchema p))))
Expand All @@ -613,14 +616,9 @@
:body (java.io.ByteArrayInputStream. file-bytes)}))

(defn- confirm-and-activate-policy [{:keys [db] :as req} policy-id]
(let [policy-txs [{:insurance.policy/policy-id policy-id
:insurance.policy/status :insurance.policy.status/active}]
policy (q/retrieve-policy db policy-id)
coverage-txs (mapcat (fn [{:instrument.coverage/keys [coverage-id]}]
[[:db/add [:instrument.coverage/coverage-id coverage-id] :instrument.coverage/status :instrument.coverage.status/coverage-active]
[:db/add [:instrument.coverage/coverage-id coverage-id] :instrument.coverage/change :instrument.coverage.change/none]])
(:insurance.policy/covered-instruments policy))
txs (concat policy-txs coverage-txs)]
(let [policy (q/retrieve-policy db policy-id)
txs (domain/txns-confirm-and-activate-policy policy)]
;; (tap> {:confirm-txs txs})
(d/transact-wrapper! req {:tx-data txs})))

(defn confirm-changes! [{:keys [db] :as req}]
Expand Down
12 changes: 12 additions & 0 deletions src/clj/app/insurance/domain.clj
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,15 @@
[[:db/retract (response-ref r) :insurance.survey.response/completed-at (t/inst completed-at)]]
(mapcat txns-uncomplete-survey-report coverage-reports))
[[:db/add (response-ref r) :insurance.survey.response/completed-at (t/inst)]]))

(defn txns-confirm-and-activate-policy-coverages [{:instrument.coverage/keys [coverage-id status change] :as coverage}]
(if (= change :instrument.coverage.change/removed)
[[:db/retractEntity (coverage-ref coverage)]]
[[:db/add [:instrument.coverage/coverage-id coverage-id] :instrument.coverage/status :instrument.coverage.status/coverage-active]
[:db/add [:instrument.coverage/coverage-id coverage-id] :instrument.coverage/change :instrument.coverage.change/none]]))

(defn txns-confirm-and-activate-policy [{:as policy :insurance.policy/keys [policy-id covered-instruments]}]
(concat
[{:insurance.policy/policy-id policy-id
:insurance.policy/status :insurance.policy.status/active}]
(mapcat txns-confirm-and-activate-policy-coverages covered-instruments)))

0 comments on commit dc5849f

Please sign in to comment.