Makes integreat run on datomic cloud
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
(ns auto-ap.graphql.accounts
|
||||
(:require
|
||||
[auto-ap.datomic :refer [audit-transact conn remove-nils]]
|
||||
[auto-ap.datomic :refer [audit-transact conn upsert-entity]]
|
||||
[auto-ap.datomic.accounts :as d-accounts]
|
||||
[auto-ap.graphql.utils
|
||||
:refer [->graphql
|
||||
@@ -11,7 +11,12 @@
|
||||
enum->keyword
|
||||
is-admin?
|
||||
result->page]]
|
||||
[datomic.api :as d]))
|
||||
[auto-ap.search :as search]
|
||||
[datomic.client.api :as dc]
|
||||
[clojure.string :as str])
|
||||
(:import
|
||||
(org.apache.lucene.search TermInSetQuery)
|
||||
(org.apache.lucene.util BytesRef)))
|
||||
|
||||
(defn get-graphql [context args _]
|
||||
(assert-admin (:id context))
|
||||
@@ -33,48 +38,38 @@
|
||||
(defn upsert-account [context args _]
|
||||
(let [{{:keys [id client-overrides numeric-code location applicability account-set name invoice-allowance vendor-allowance 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]}))
|
||||
(when (seq (dc/q {:query {:find ['?e]
|
||||
:in '[$ ?account-set ?numeric-code]
|
||||
:where ['[?e :account/account-set ?account-set]
|
||||
'[?e :account/numeric-code ?numeric-code]]}
|
||||
:args [(dc/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 (or (enum->keyword applicability "account-applicability")
|
||||
:account-applicability/global)
|
||||
|
||||
:account/invoice-allowance (some-> invoice-allowance (enum->keyword "allowance"))
|
||||
:account/vendor-allowance (some-> vendor-allowance (enum->keyword "allowance"))
|
||||
:account/default-allowance :allowance/allowed
|
||||
: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)]))
|
||||
(let [result (audit-transact [`(upsert-entity
|
||||
~{:db/id (or id "new-account")
|
||||
:account/name name
|
||||
:account/search-terms name
|
||||
:account/type (keyword "account-type" (clojure.core/name type))
|
||||
:account/applicability (or (enum->keyword applicability "account-applicability")
|
||||
:account-applicability/global)
|
||||
:account/invoice-allowance (some-> invoice-allowance (enum->keyword "allowance"))
|
||||
:account/vendor-allowance (some-> vendor-allowance (enum->keyword "allowance"))
|
||||
:account/default-allowance :allowance/allowed
|
||||
:account/account-set account-set
|
||||
:account/location location
|
||||
:account/numeric-code numeric-code
|
||||
:account/code (str numeric-code)
|
||||
:account/client-overrides (mapv
|
||||
(fn [client-override]
|
||||
{: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)})]
|
||||
(:id context))]
|
||||
(->graphql
|
||||
(d-accounts/get-by-id (or id (get-in result [:tempids "new-account"])))))))
|
||||
(d-accounts/get-by-id (or id (get-in result [:tempids "new-account"])))))))
|
||||
|
||||
(def search-pattern [:db/id
|
||||
:account/numeric-code
|
||||
@@ -86,6 +81,7 @@
|
||||
(when client
|
||||
(assert-can-see-client (:id context) client))
|
||||
(let [query (cleanse-query query)
|
||||
_ (println query)
|
||||
num (some-> (re-find #"([0-9]+)" query)
|
||||
second
|
||||
(not-empty )
|
||||
@@ -160,3 +156,35 @@
|
||||
(sequence xform)))
|
||||
[])))
|
||||
|
||||
(defn rebuild-search-index []
|
||||
(search/full-index-query
|
||||
(for [result (map first (dc/qseq '[:find (pull ?aco [:account-client-override/search-terms :account-client-override/client :db/id {:account/_client-overrides [:account/numeric-code :account/location :db/id]}])
|
||||
:in $
|
||||
:where [?aco :account-client-override/client ]
|
||||
[?aco :account-client-override/search-terms ]]
|
||||
(dc/db conn)))
|
||||
:when (:account/numeric-code (:account/_client-overrides result))]
|
||||
{:id (:db/id (:account/_client-overrides result))
|
||||
:text (:account-client-override/search-terms result)
|
||||
:client (str (:db/id (:account-client-override/client result)))
|
||||
:numeric-code (:account/numeric-code (:account/_client-overrides result))
|
||||
:location (:account/location (:account/_client-overrides result))})
|
||||
"account-client-override")
|
||||
|
||||
(search/full-index-query
|
||||
(for [result (map first (dc/qseq '[:find (pull ?a [:account/numeric-code
|
||||
:account/search-terms
|
||||
{:account/applicability [:db/ident]}
|
||||
:db/id
|
||||
:account/location])
|
||||
:in $
|
||||
:where [?a :account/search-terms ]]
|
||||
(dc/db conn)))
|
||||
:when (:account/search-terms result)
|
||||
]
|
||||
{:id (:db/id result)
|
||||
:text (:account/search-terms result)
|
||||
:numeric-code (:account/numeric-code result)
|
||||
:location (:account/location result)
|
||||
:applicability (name (:db/ident (:account/applicability result)))})
|
||||
"account"))
|
||||
|
||||
Reference in New Issue
Block a user