<commit_before>(ns org.akvo.flow-api.endpoint.flumenfly
  (:require [clojure.set :refer [rename-keys]]
            [clojure.spec]
            [clojure.walk]
            [compojure.core :refer :all]
            [org.akvo.flow-api.boundary.survey :as survey]
            [org.akvo.flow-api.middleware.resolve-alias]
            [org.akvo.flow-api.middleware.jdo-persistent-manager :as jdo-pm]
            [ring.util.response :refer [response]]
            [org.akvo.flow-api.endpoint.spec :as spec]))

(def survey-list-spec (clojure.spec/coll-of ::spec/full-survey-id))

(def renames {"instance_id" :instance-id
              "survey_id" :survey-id})
(def renames-revert (clojure.set/map-invert renames))

(defn rename [rename-map]
  (fn [m]
    (rename-keys m rename-map)))

(defn surveys [remote-api email body]
  (let [surveys (spec/validate-params survey-list-spec
                  (map (rename renames) body))]
    (->> remote-api
      (survey/filter-surveys email surveys)
      (map (rename renames-revert))
      response)))

(defn endpoint* [{:keys [remote-api akvo-flow-server-config]}]
  (routes
    (POST "/flumenfly" {:keys [email body]}
      (surveys remote-api email body))))

(defn endpoint [{:keys [akvo-flow-server-config] :as deps}]
  (-> (endpoint* deps)
    (jdo-pm/wrap-close-persistent-manager)))

(comment
  (->
    (clj-http.client/post "http://localhost:3000/flumenfly" {:as :json
                                                             :headers {"x-akvo-email" "<EMAIL>"}
                                                             :form-params [{:instance_id "akvoflowsandbox"
                                                                            :survey_id "152342023"}
                                                                           {:instance_id "akvoflowsandbox"
                                                                            :survey_id "148412329"}]
                                                             :content-type :json})
    ;(try (catch Exception e (ex-data e)))
    ;:body
    ;(cheshire.core/parse-string true)<commit_msg>[#146] Rename endpoint to check_permissions<commit_after>(ns org.akvo.flow-api.endpoint.flumenfly
  (:require [clojure.set :refer [rename-keys]]
            [clojure.spec]
            [clojure.walk]
            [compojure.core :refer :all]
            [org.akvo.flow-api.boundary.survey :as survey]
            [org.akvo.flow-api.middleware.resolve-alias]
            [org.akvo.flow-api.middleware.jdo-persistent-manager :as jdo-pm]
            [ring.util.response :refer [response]]
            [org.akvo.flow-api.endpoint.spec :as spec]))

(def survey-list-spec (clojure.spec/coll-of ::spec/full-survey-id))

(def renames {"instance_id" :instance-id
              "survey_id" :survey-id})
(def renames-revert (clojure.set/map-invert renames))

(defn rename [rename-map]
  (fn [m]
    (rename-keys m rename-map)))

(defn surveys [remote-api email body]
  (let [surveys (spec/validate-params survey-list-spec
                  (map (rename renames) body))]
    (->> remote-api
      (survey/filter-surveys email surveys)
      (map (rename renames-revert))
      response)))

(defn endpoint* [{:keys [remote-api akvo-flow-server-config]}]
  (routes
    (POST "/check_permissions" {:keys [email body]}
      (surveys remote-api email body))))

(defn endpoint [{:keys [akvo-flow-server-config] :as deps}]
  (-> (endpoint* deps)
    (jdo-pm/wrap-close-persistent-manager)))

(comment
  (->
    (clj-http.client/post "http://localhost:3000/check_permissions"
      {:as :json
       :headers {"x-akvo-email" "<EMAIL>"}
       :form-params [{:instance_id "akvoflowsandbox"
                      :survey_id "152342023"}
                     {:instance_id "akvoflowsandbox"
                      :survey_id "148412329"}]
       :content-type :json})
    ;(try (catch Exception e (ex-data e)))
    ;:body
    ;(cheshire.core/parse-string true)
{'fraction_non_alphanumeric': 0.1428209030948757, 'fraction_numerical': 0.011922881785895484, 'mean_word_length': 1.9403430275913498, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7056008', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1271, 'n_words': 241}