Skip to content

Commit

Permalink
Create RBAC contexts for new plastic strategies
Browse files Browse the repository at this point in the history
[Re #1611]

* We'll need the RBAC contexts as we'll use permissions to control
access of users to plastic strategy's API resources.
  • Loading branch information
lucassousaf committed Oct 6, 2023
1 parent 1047541 commit 6c6e0e9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
31 changes: 29 additions & 2 deletions backend/src/gpml/db/plastic_strategy.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(ns gpml.db.plastic-strategy
{:ns-tracker/resource-deps ["plastic_strategy.sql"]}
(:require [hugsql.core :as hugsql]))
(:require [gpml.db.jdbc-util :as jdbc-util]
[hugsql.core :as hugsql]))

(declare get-plastic-strategies*
update-plastic-strategy*
create-plastic-strategies*)
create-plastic-strategies*
create-plastic-strategy*
delete-plastic-strategy*)

(hugsql/def-db-fns "gpml/db/plastic_strategy.sql")

Expand Down Expand Up @@ -64,3 +67,27 @@
{:success? false
:reason :exception
:error-details {:msg (ex-message t)}})))

(defn create-plastic-strategy
[conn plastic-strategy]
(jdbc-util/with-constraint-violation-check
[{:type :unique
:name "plastic_strategy_country_id_key"
:error-reason :already-exists}]
{:success? true
:id (:id (create-plastic-strategy* conn plastic-strategy))}))

(defn delete-plastic-strategy
[conn plastic-strategy-id]
(try
(let [affected (delete-plastic-strategy* conn {:id plastic-strategy-id})]
(if (= affected 1)
{:success? true}
{:success? false
:reason :unexpected-number-of-affected-rows
:error-details {:expected-affected-rows 1
:actual-affected-rows affected}}))
(catch Throwable t
{:success? false
:reason :exception
:error-details {:msg (ex-message t)}})))
8 changes: 8 additions & 0 deletions backend/src/gpml/db/plastic_strategy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ WHERE id = :id;
-- :name create-plastic-strategies* :execute :affected
INSERT INTO plastic_strategy(country_id)
VALUES :t*:plastic-strategy;

-- :name create-plastic-strategy* :returning-execute :one
INSERT INTO plastic_strategy(country_id)
VALUES (:country-id) RETURNING id;

-- :name delete-plastic-strategy* :execute :affected
DELETE FROM plastic_strategy
WHERE id = :id;
10 changes: 5 additions & 5 deletions backend/src/gpml/handler/programmatic/plastic_strategy.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns gpml.handler.programmatic.plastic-strategy
(:require [gpml.db.plastic-strategy :as db.ps]
[gpml.handler.responses :as r]
(:require [gpml.handler.responses :as r]
[gpml.service.plastic-strategy :as srv.ps]
[integrant.core :as ig]))

(def ^:private create-plastic-strategies-params-schema
Expand All @@ -11,10 +11,10 @@
[:int {:min 1}]])

(defn- create-plastic-strategies
[{:keys [db]} req]
[config req]
(let [countries-ids (get-in req [:parameters :body])
plastic-strategies (map vector countries-ids)
result (db.ps/create-plastic-strategies (:spec db) plastic-strategies)]
plastic-strategies (map #(zipmap [:country-id] [%]) countries-ids)
result (srv.ps/create-plastic-strategies config plastic-strategies)]
(if (:success? result)
(r/ok {})
(r/server-error (dissoc result :success?)))))
Expand Down
43 changes: 42 additions & 1 deletion backend/src/gpml/service/plastic_strategy.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns gpml.service.plastic-strategy
(:require [gpml.db.plastic-strategy :as db.ps]))
(:require [gpml.db.plastic-strategy :as db.ps]
[gpml.service.permissions :as srv.permissions]
[gpml.util.thread-transactions :as tht]))

(defn get-plastic-strategies
[{:keys [db]} search-opts]
Expand All @@ -17,3 +19,42 @@
result
(db.ps/update-plastic-strategy (:spec db) {:id (-> result :plastic-strategy :id)
:updates {:steps steps}}))))

(defn create-plastic-strategy
[{:keys [db logger]} plastic-strategy]
(let [transactions
[{:txn-fn
(fn tx-create-plastic-strategy
[{:keys [plastic-strategy]}]
(db.ps/create-plastic-strategy (:spec db) plastic-strategy))
:rollback-fn
(fn rollback-create-plastic-strategy
[{:keys [id] :as context}]
(db.ps/delete-plastic-strategy (:spec db) {:id id})
context)}
{:txn-fn
(fn tx-create-plastic-strategy-rbac-context
[{:keys [id] :as context}]
(let [result (srv.permissions/create-resource-context {:conn (:spec db)
:logger logger}
{:context-type :plastic-strategy
:resource-id id})]
(if (:success? result)
{:success? true}
(assoc context
:success? false
:reason :failed-to-create-plastic-strategy-rbac-context
:error-details result))))}]
context {:success? true
:plastic-strategy plastic-strategy}]
(tht/thread-transactions logger transactions context)))

(defn create-plastic-strategies
[config plastic-strategies]
(let [results (map (partial create-plastic-strategy config) plastic-strategies)]
(if (every? :success? results)
{:success? true}
{:success? false
:reason :failed-to-create-all-plastic-strategies
:error-details {:msg "Partial failure"
:failed-results (filter (comp not :success?) results)}})))

0 comments on commit 6c6e0e9

Please sign in to comment.