Refactor direction

parent 43022514
Pipeline #41965118 failed with stages
in 1 minute and 34 seconds
......@@ -201,48 +201,32 @@
(defn put-edit-note
"Edit the given note for the (organisation, project, logged_in_user) combination."
[{:keys [form-params route-params authenticated-user-id] :as req}]
(let [organisation-slug (:organisation-slug route-params)
project-slug (:project-slug route-params)
note-id (:note-id route-params)
note-date (get form-params "note_date")
note-title (get form-params "title")
note-body (get form-params "body")]
(f/attempt-all [valid-organisation-id (org/validate-organisation-by-slug
organisation-slug
authenticated-user-id)
valid-project-id (proj/validate-project-by-slug
project-slug
valid-organisation-id)
valid-user-id (proj/get-project-owner-or-member
valid-project-id
authenticated-user-id)]
(f/attempt-all [new-note (proj/edit-note note-id (note note-date
note-title
note-body
valid-project-id
valid-user-id))]
(helper/create-response :project-note-edited 200)
(f/when-failed [e]
(f/fail (f/message e))))
(f/when-failed [{:keys [message] :as e}]
(create-error-response message)))))
(let [{:keys [note-id project-slug organisation-slug]} route-params
{:strs [note_date title body]} form-params]
(f/attempt-all [new-note
(proj/edit-note note-id
user-id
(note note-date
note-title
note-body
valid-project-id
valid-user-id))]
(helper/create-response :project-note-edited 200)
(f/when-failed [e]
(f/fail (f/message e))))))
(defn validate-note-date
[date]
(s/valid? :nnts.models.project/date date))
(defn get-all-notes
"Retrieve all notes of all users for the given project."
[{:keys [query-params authenticated-user-id] :as req}]
"Retrieve all notes of all users for the given project for a given date."
[{:keys [query-params user-id] :as req}]
(let [{:strs [note-date project-id]} query-params]
(if-not (validate-note-date note-date)
(helper/create-response :invalid-note-date 400)
(f/attempt-all [_ (proj/get-project-owner-or-member
(ch/string->uuid project-id)
authenticated-user-id)
notes (proj/get-all-notes-for-project-by-note-date note-date
project-id)]
(-> (helper/create-response "Ok" 200)
(assoc-in [:body :notes] notes))
(f/attempt-all [_ (proj/has-access? (ch/string->uuid project-id) user-id)
notes (proj/notes-for-note-date note-date project-id)]
(helper/ok-response {:notes notes})
(f/when-failed [{:keys [message] :as e}]
(create-error-response message))))))
......@@ -19,8 +19,4 @@
"Middleware for handling authentication."
[handler]
(fn [req]
(if-let [session_id (get-in (:cookies req) ["session_id" :value])]
(if-let [id (authenticate-user session_id)]
(handler (assoc req :authenticated-user-id id))
(resp-helper/create-response "USER-UNAUTHENTICATED" 401))
(resp-helper/create-response "USER-UNAUTHENTICATED" 401))))
(assoc req :authenticated-user-id "dev-user-id")))
......@@ -89,7 +89,8 @@
(f/fail (case (:error message)
(helper/construct-unknown-error))))))
(defn get-project-owner-or-member
(defn has-access?
"Returns truthy value if the user has access to this project. The owner and members have access to projects."
[project-id user-id]
(f/attempt-all [owner (db-project/get-owner-of-project (db-helper/get-conn) project-id)]
(if (= owner user-id)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment