Files
integreat/src/clj/auto_ap/graphql/accounts.clj

110 lines
5.5 KiB
Clojure

(ns auto-ap.graphql.accounts
(:require
[auto-ap.datomic :refer [audit-transact conn remove-nils]]
[auto-ap.datomic.accounts :as d-accounts]
[auto-ap.graphql.utils
:refer [->graphql
<-graphql
assert-admin
assert-can-see-client
enum->keyword
result->page]]
[datomic.api :as d]))
(defn get-graphql [context args _]
(assert-admin (:id context))
(let [args (assoc args :id (:id context))
[accounts accounts-count ] (d-accounts/get-graphql (<-graphql args))]
(result->page accounts accounts-count :accounts args)))
(defn default-for-vendor [context args _]
(assert-can-see-client (:id context) (:client_id args))
(let [result (d-accounts/get-for-vendor (:vendor_id args) (:client_id args))]
(->graphql (d-accounts/clientize result (:client_id args)))))
(defn upsert-account [context args _]
(let [{{:keys [id client-overrides numeric-code location applicability account-set name type]} :account} (<-graphql args)]
(when-not id
(when (seq (d/query {:query {:find ['?e]
:in '[$ ?account-set ?numeric-code]
:where ['[?e :account/account-set ?account-set]
'[?e :account/numeric-code ?numeric-code]]}
:args [(d/db conn) account-set numeric-code]}))
(throw (ex-info (str "Account set " account-set " already has an account for code " numeric-code)
{} ))))
(let [original (when id
(d/entity (d/db conn) id))
result (audit-transact (cond->
[(remove-nils
{:db/id (or id "new-account")
:account/name name
:account/search-terms name
:account/type (keyword "account-type" (clojure.core/name type))
:account/applicability (enum->keyword applicability "account-applicability")
:account/account-set account-set
:account/location location
:account/numeric-code (when-not id
numeric-code)
:account/code (when-not id
(str numeric-code))})
[:reset (or id "new-account") :account/client-overrides
(mapv
(fn [client-override]
(remove-nils
{:db/id (:id client-override)
:account-client-override/client (:client-id client-override)
:account-client-override/name (:name client-override)
:account-client-override/search-terms (:name client-override)}))
client-overrides)]]
(and (not location) (:account/location original)) (conj [:db/retract (or id "new-account") :account/location (:account/location original)]))
(:id context))]
(->graphql
(d-accounts/get-by-id (or id (get-in result [:tempids "new-account"])))))))
(defn search [context {query :query client :client_id} _]
(when client
(assert-can-see-client (:id context) client))
(let [num (some-> (re-find #"([0-9]+)" query)
second
(not-empty )
Integer/parseInt)]
(if num
(->> (d/q '[:find ?n (pull ?i [:db/id :account/numeric-code :account/location])
:in $ ?numeric-code
:where [?i :account/numeric-code ?numeric-code]
[?i :account/name ?n]]
(d/db conn)
num)
(map (fn [[n a]]
{:name (str (:account/numeric-code a) " - " n)
:id (:db/id a)
:location (:account/location a)})))
(->> (d/q '[:find ?n (pull ?i [:db/id :account/numeric-code :account/location]) ?s
:in $ ?q
:where [(fulltext $ :account/search-terms ?q) [[?i ?n _ ?s]]]
[?i :account/numeric-code ?numeric-code]
(or [?i :account/applicability :account-applicability/global]
[?i :account/applicability :account-applicability/optional])]
(d/db conn)
query)
(concat (when client
(d/q '[:find ?n (pull ?a [:db/id :account/numeric-code :account/location]) ?s
:in $ ?c ?q
:where
[?i :account-client-override/client ?c]
[(fulltext $ :account-client-override/search-terms ?q) [[?i ?n _ ?s]]]
[?a :account/client-overrides ?i]
[?a :account/numeric-code ?numeric-code]]
(d/db conn)
client
query)))
(sort-by (comp - last))
(map (fn [[n a]]
{:name (str (:account/numeric-code a) " - " n)
:id (:db/id a)
:location (:account/location a)}))))))