Makes a vendor account acceptable even if the invoice account isn't.
This commit is contained in:
@@ -5,10 +5,11 @@
|
||||
[auto-ap.graphql.utils
|
||||
:refer [->graphql
|
||||
<-graphql
|
||||
cleanse-query
|
||||
assert-admin
|
||||
assert-can-see-client
|
||||
cleanse-query
|
||||
enum->keyword
|
||||
is-admin?
|
||||
result->page]]
|
||||
[datomic.api :as d]))
|
||||
|
||||
@@ -74,7 +75,13 @@
|
||||
(->graphql
|
||||
(d-accounts/get-by-id (or id (get-in result [:tempids "new-account"])))))))
|
||||
|
||||
(defn search [context {query :query client :client_id allowance :allowance} _]
|
||||
(def search-pattern [:db/id
|
||||
:account/numeric-code
|
||||
:account/location
|
||||
{:account/vendor-allowance [:db/ident]
|
||||
:account/default-allowance [:db/ident]
|
||||
:account/invoice-allowance [:db/ident]}])
|
||||
(defn search [context {query :query client :client_id allowance :allowance vendor-id :vendor_id} _]
|
||||
(when client
|
||||
(assert-can-see-client (:id context) client))
|
||||
(let [query (cleanse-query query)
|
||||
@@ -82,45 +89,58 @@
|
||||
second
|
||||
(not-empty )
|
||||
Integer/parseInt)
|
||||
|
||||
pattern [:db/id :account/numeric-code :account/location {:account/vendor-allowance [:db/ident]} {:account/invoice-allowance [:db/ident]}]
|
||||
|
||||
valid-allowances (cond-> #{:allowance/allowed
|
||||
:allowance/warn}
|
||||
(is-admin? (:id context)) (conj :allowance/admin-only))
|
||||
allowance (cond (= allowance :vendor)
|
||||
:account/vendor-allowance
|
||||
(= allowance :invoice)
|
||||
:account/invoice-allowance
|
||||
:else
|
||||
:account/default-allowance)]
|
||||
:account/default-allowance)
|
||||
|
||||
vendor-account (when vendor-id
|
||||
(-> (d/q '[:find ?da
|
||||
:in $ ?v
|
||||
:where [?v :vendor/default-account ?da]]
|
||||
(d/db conn)
|
||||
vendor-id)
|
||||
ffirst))
|
||||
xform (comp
|
||||
(filter (fn [[_ a]]
|
||||
(or
|
||||
(valid-allowances (-> a allowance :db/ident))
|
||||
(= (:db/id a) vendor-account))))
|
||||
(map (fn [[n a]]
|
||||
{:name (str (:account/numeric-code a) " - " n)
|
||||
:id (:db/id a)
|
||||
:location (:account/location a)
|
||||
:warning (when (= :allowance/warn (-> a allowance :db/ident))
|
||||
"This account is not typically used for this purpose.")})))]
|
||||
(if query
|
||||
(if num
|
||||
(->> (d/q '[:find ?n (pull ?i pattern)
|
||||
:in $ ?numeric-code ?allowance pattern
|
||||
:where [?i :account/numeric-code ?numeric-code]
|
||||
[?i :account/name ?n]
|
||||
(or [?i ?allowance :allowance/allowed]
|
||||
[?i ?allowance :allowance/warn])]
|
||||
(or [?i :account/applicability :account-applicability/global]
|
||||
[?i :account/applicability :account-applicability/optional])]
|
||||
(d/db conn)
|
||||
num
|
||||
allowance
|
||||
pattern)
|
||||
(map (fn [[n a]]
|
||||
{:name (str (:account/numeric-code a) " - " n)
|
||||
:id (:db/id a)
|
||||
:location (:account/location a)
|
||||
:warning (when (= :allowance/warn (-> a allowance :db/ident))
|
||||
"This account is not typically used for this purpose.")})))
|
||||
|
||||
search-pattern)
|
||||
(sequence xform))
|
||||
(->> (d/q '[:find ?n (pull ?i pattern) ?s
|
||||
:in $ ?q ?allowance pattern
|
||||
:where [(fulltext $ :account/search-terms ?q) [[?i ?n _ ?s]]]
|
||||
[?i :account/numeric-code ?numeric-code]
|
||||
(or [?i ?allowance :allowance/allowed]
|
||||
[?i ?allowance :allowance/warn])
|
||||
(or [?i :account/applicability :account-applicability/global]
|
||||
[?i :account/applicability :account-applicability/optional])]
|
||||
(d/db conn)
|
||||
query
|
||||
allowance
|
||||
pattern)
|
||||
search-pattern)
|
||||
(concat (when client
|
||||
(d/q '[:find ?n (pull ?a pattern) ?s
|
||||
:in $ ?c ?q ?allowance pattern
|
||||
@@ -128,20 +148,13 @@
|
||||
[?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]
|
||||
(or [?a ?allowance :allowance/allowed]
|
||||
[?a ?allowance :allowance/warn])]
|
||||
[?a :account/numeric-code ?numeric-code]]
|
||||
(d/db conn)
|
||||
client
|
||||
query
|
||||
allowance
|
||||
pattern)))
|
||||
search-pattern)))
|
||||
(sort-by (comp - last))
|
||||
(map (fn [[n a]]
|
||||
{:name (str (:account/numeric-code a) " - " n)
|
||||
:id (:db/id a)
|
||||
:location (:account/location a)
|
||||
:warning (when (= :allowance/warn (-> a allowance :db/ident))
|
||||
"This account is not typically used for this purpose.")}))))
|
||||
(sequence xform)))
|
||||
[])))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user