90 lines
5.6 KiB
Clojure
90 lines
5.6 KiB
Clojure
(ns auto-ap.graphql.clients
|
|
(:require [auto-ap.datomic.clients :as d-clients]
|
|
[datomic.api :as d]
|
|
[auto-ap.datomic :refer [uri remove-nils]]
|
|
[auto-ap.graphql.utils :refer [->graphql assert-admin can-see-client?]]
|
|
[clojure.string :as str]))
|
|
|
|
#_(def role->datomic-role {":none" :user-role/none
|
|
":admin" :user-role/admin
|
|
":user" :user-role/user})
|
|
|
|
(defn assert-client-code-is-unique [code]
|
|
(when (seq (d/query {:query {:find '[?id]
|
|
:in ['$ '?code]
|
|
:where ['[?id :client/code ?code]]}
|
|
:args [(d/db (d/connect uri)) code]}))
|
|
(throw (ex-info "Client is not unique" {:validation-error (str "Client code '" code "' is not unique.")}))))
|
|
|
|
(defn edit-client [context {:keys [edit_client new_bank_accounts] :as args} value]
|
|
(assert-admin (:id context))
|
|
(when-not (:id edit_client)
|
|
(assert-client-code-is-unique (:code edit_client)))
|
|
|
|
(let [client (when (:id edit_client) (d-clients/get-by-id (:id edit_client)))
|
|
id (or (:db/id client) "new-client")
|
|
_ (println id)
|
|
_ (println edit_client)
|
|
_ (when client
|
|
@(d/transact (d/connect uri)
|
|
(into
|
|
(mapv (fn [lm] [:db/retractEntity (:db/id lm)]) (:client/location-matches client))
|
|
(mapv (fn [m] [:db/retract (:db/id client) :client/matches m]) (:client/matches client)))))
|
|
transactions [(remove-nils {:db/id id
|
|
:client/code (if (str/blank? (:client/code client))
|
|
(:code edit_client)
|
|
(:client/code client))
|
|
:client/name (:name edit_client)
|
|
:client/matches (:matches edit_client)
|
|
:client/email (:email edit_client)
|
|
:client/locations (filter identity (:locations edit_client))
|
|
:client/location-matches (->> (:location_matches edit_client)
|
|
(filter (fn [lm] (and (:location lm) (:match lm))))
|
|
(map (fn [lm] {:location-match/location (:location lm)
|
|
:location-match/matches [(:match lm)]})))
|
|
:client/address (remove-nils {
|
|
:address/street1 (:street1 (:address edit_client))
|
|
:address/street2 (:street2 (:address edit_client))
|
|
:address/city (:city (:address edit_client))
|
|
:address/state (:state (:address edit_client))
|
|
:address/zip (:zip (:address edit_client))})
|
|
|
|
:client/bank-accounts (map #(remove-nils
|
|
{:db/id (:id %)
|
|
:bank-account/code (:code %)
|
|
:bank-account/bank-name (:bank_name %)
|
|
:bank-account/bank-code (:bank_code %)
|
|
:bank-account/routing (:routing %)
|
|
|
|
:bank-account/name (:name %)
|
|
:bank-account/visible (:visible %)
|
|
:bank-account/number (:number %)
|
|
:bank-account/check-number (:check_number %)
|
|
:bank-account/sort-order (:sort_order %)
|
|
:bank-account/locations (:locations %)
|
|
|
|
:bank-account/yodlee-account-id (:yodlee_account_id %)
|
|
:bank-account/type (keyword "bank-account-type" (name (:type %)))
|
|
}
|
|
) (:bank_accounts edit_client))
|
|
|
|
})]
|
|
result @(d/transact (d/connect uri) transactions)]
|
|
(println result "ID" id)
|
|
(-> result :tempids (get id) (or id) d-clients/get-by-id
|
|
(update :client/location-matches
|
|
(fn [lms]
|
|
(mapcat (fn [lm]
|
|
(map (fn [m]
|
|
{:location-match/match m
|
|
:location-match/location (:location-match/location lm)})
|
|
(:location-match/matches lm)))
|
|
lms)))
|
|
->graphql)))
|
|
|
|
|
|
(defn get-client [context args value]
|
|
(->graphql
|
|
(filter #(can-see-client? (:id context) %)
|
|
(d-clients/get-all))))
|