Migrates back to datomic on-prem

This commit is contained in:
2023-04-29 07:12:51 -07:00
parent aca8f7c92c
commit b2ad7790cf
88 changed files with 2289 additions and 2286 deletions

View File

@@ -13,8 +13,8 @@
result->page]]
[auto-ap.search :as search]
[auto-ap.utils :refer [heartbeat]]
[datomic.client.api :as dc]
[iol-ion.tx :refer [random-tempid upsert-entity]]
[datomic.api :as dc]
[iol-ion.tx :refer [random-tempid]]
[mount.core :as mount]
[yang.scheduler :as scheduler]))
@@ -38,36 +38,36 @@
(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 (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]}))
(when (seq (dc/q {:find ['?e]
:in '[$ ?account-set ?numeric-code]
:where ['[?e :account/account-set ?account-set]
'[?e :account/numeric-code ?numeric-code]]}
(dc/db conn) account-set numeric-code))
(throw (ex-info (str "Account set " account-set " already has an account for code " numeric-code)
{} ))))
(let [result (audit-transact [`(upsert-entity
~(cond-> {: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 (or (:id client-override) (random-tempid))
: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 (dissoc :account/numeric-code :account/code)))]
(let [result (audit-transact [[:upsert-entity
(cond-> {: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 (or (:id client-override) (random-tempid))
: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 (dissoc :account/numeric-code :account/code))]]
(:id context))]
(->graphql
(d-accounts/get-by-id (or id (get-in result [:tempids "new-account"])))))))
@@ -149,12 +149,12 @@
(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 ]
[_ :account/client-overrides ?aco]]
(dc/db conn)))
(for [result (map first (dc/qseq {:query '[: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 ]
[_ :account/client-overrides ?aco]]
:args [(dc/db conn)]}))
:when (:account/numeric-code (:account/_client-overrides result))]
{:id (:db/id (:account/_client-overrides result))
:account-client-override-id (:db/id result)
@@ -165,14 +165,14 @@
"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)))
(for [result (map first (dc/qseq {:query '[:find (pull ?a [:account/numeric-code
:account/search-terms
{:account/applicability [:db/ident]}
:db/id
:account/location])
:in $
:where [?a :account/search-terms ]]
:args [(dc/db conn)]}))
:when (:account/search-terms result)
]
{:id (:db/id result)
@@ -183,11 +183,11 @@
"account"))
#_(dc/transact conn
{:tx-data [{:db/ident :account-client-override/k2
:db/valueType :db.type/tuple
:db/tupleAttrs [:account/_client-overrides :account-client-override/client ]
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}]})
[{:db/ident :account-client-override/k2
:db/valueType :db.type/tuple
:db/tupleAttrs [:account/_client-overrides :account-client-override/client ]
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}])
#_(dc/q '[:find ?o
:where [_ :account-client-override/k ?o]]

View File

@@ -3,7 +3,6 @@
[amazonica.aws.s3 :as s3]
[auto-ap.datomic :refer [conn remove-nils pull-many audit-transact]]
[auto-ap.datomic.accounts :as a]
[iol-ion.tx :refer [upsert-invoice pay plus]]
[auto-ap.datomic.bank-accounts :as d-bank-accounts]
[auto-ap.datomic.checks :as d-checks]
[auto-ap.datomic.clients :as d-clients]
@@ -35,7 +34,7 @@
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[config.core :refer [env]]
[clj-time.coerce :as coerce]
[datomic.client.api :as dc]
[datomic.api :as dc]
[digest])
(:import
(java.io ByteArrayOutputStream)
@@ -230,7 +229,7 @@
[{:invoice-payment/payment (-> invoice :invoice/vendor :db/id str)
:invoice-payment/amount invoice-amount
:invoice-payment/invoice (:db/id invoice)}
`(pay ~(:db/id invoice) ~invoice-amount)])
[:pay (:db/id invoice) invoice-amount]])
(reduce into [])))
(defn base-payment [invoices vendor client bank-account _ _ invoice-amounts]
@@ -408,7 +407,7 @@
(reduce into [])
doall))
checks (if (= type :payment-type/check)
(conj checks `(plus ~(:db/id bank-account) ~:bank-account/check-number ~(count invoices-grouped-by-vendor)))
(conj checks [:plus (:db/id bank-account) :bank-account/check-number (count invoices-grouped-by-vendor)])
checks)]
(when (= type :payment-type/check)
(mu/trace ::making-pdfs [:checks checks]
@@ -501,11 +500,11 @@
new-balance (+ (:invoice/outstanding-balance invoice)
(:invoice-payment/amount x))]
[[:db/retractEntity (:db/id x)]
`(upsert-invoice ~{:db/id (:db/id invoice)
:invoice/outstanding-balance new-balance
:invoice/status (if (dollars-0? new-balance)
(:invoice/status invoice)
:invoice-status/unpaid)})]))
[:upsert-invoice {:db/id (:db/id invoice)
:invoice/outstanding-balance new-balance
:invoice/status (if (dollars-0? new-balance)
(:invoice/status invoice)
:invoice-status/unpaid)}]]))
(:payment/invoices check))
updated-payment {:db/id id
:payment/amount 0.0
@@ -543,11 +542,11 @@
(let [new-balance (+ (:invoice/outstanding-balance invoice)
amount)]
[[:db.fn/retractEntity id]
`(upsert-invoice ~{:db/id (:db/id invoice)
:invoice/outstanding-balance new-balance
:invoice/status (if (dollars-0? new-balance)
(:invoice/status invoice)
:invoice-status/unpaid)})]))))))))
[:upsert-invoice {:db/id (:db/id invoice)
:invoice/outstanding-balance new-balance
:invoice/status (if (dollars-0? new-balance)
(:invoice/status invoice)
:invoice-status/unpaid)}]]))))))))
id))

View File

@@ -20,8 +20,8 @@
[clojure.string :as str]
[clojure.tools.logging :as log]
[com.brunobonacci.mulog :as mu]
[datomic.client.api :as dc]
[iol-ion.tx :refer [random-tempid upsert-entity]]
[datomic.api :as dc]
[iol-ion.tx :refer [random-tempid]]
[mount.core :as mount]
[unilog.context :as lc]
[yang.scheduler :as scheduler])
@@ -30,10 +30,10 @@
(org.apache.commons.codec.binary Base64)))
(defn assert-client-code-is-unique [code]
(when (seq (dc/q {:query {:find '[?id]
:in ['$ '?code]
:where ['[?id :client/code ?code]]}
:args [(dc/db conn) code]}))
(when (seq (dc/q {:find '[?id]
:in ['$ '?code]
:where ['[?id :client/code ?code]]}
(dc/db conn) code))
(throw (ex-info "Client is not unique" {:validation-error (str "Client code '" code "' is not unique.")}))))
(defn upload-signature-data [signature-data]
@@ -52,8 +52,8 @@
(str "https://integreat-signature-images.s3.amazonaws.com/" signature-id ".jpg")))))
(defn assert-no-shared-transaction-sources [client-code txes]
(let [new-db (:db-after (dc/with (dc/with-db conn)
{:tx-data txes}))]
(let [new-db (:db-after (dc/with (dc/db conn)
txes))]
(when (seq (->> (dc/q '[:find ?src (count ?ba)
:in $ ?c
:where [?c :client/bank-accounts ?ba]
@@ -150,10 +150,10 @@
}
_ (mu/log ::upserting :up updated-entity)
_ (assert-no-shared-transaction-sources client-code [`(upsert-entity ~updated-entity)])
_ (assert-no-shared-transaction-sources client-code [[:upsert-entity updated-entity]])
_ (log/info "upserting client" updated-entity)
result (audit-transact [`(upsert-entity ~updated-entity)] (:id context))]
result (audit-transact [[:upsert-entity updated-entity]] (:id context))]
(when (:square_auth_token edit_client)
(square/upsert-locations (-> result :tempids (get id) (or id) d-clients/get-by-id)))
(-> (-> result :tempids (get id) (or id) d-clients/get-by-id)
@@ -178,26 +178,25 @@
clients (dc/q '[:find (pull ?c [:db/id :client/code {:client/bank-accounts [:db/id :bank-account/code]}])
:where [?c :client/code]]
db )]
(dc/transact conn
{:tx-data
(for [[{client :db/id code :client/code bank-accounts :client/bank-accounts}] clients
{bank-account :db/id bac :bank-account/code} bank-accounts]
{:db/id bank-account
:bank-account/current-balance
(or
(->> (dc/index-pull db
{:index :avet
:selector [:db/id :journal-entry-line/location :journal-entry-line/account :journal-entry-line/running-balance :journal-entry-line/client+account+location+date {:journal-entry/_line-items [:journal-entry/date :journal-entry/client]}]
:start [:journal-entry-line/client+account+location+date [client bank-account "A" #inst "2030-01-01"]]
:limit 1
:reverse true
})
(filter (fn [{[c b] :journal-entry-line/client+account+location+date}]
(and (= c client)
(= b bank-account))))
(map :journal-entry-line/running-balance)
(first))
0.0)})}))))
@(dc/transact conn
(for [[{client :db/id code :client/code bank-accounts :client/bank-accounts}] clients
{bank-account :db/id bac :bank-account/code} bank-accounts]
{:db/id bank-account
:bank-account/current-balance
(or
(->> (dc/index-pull db
{:index :avet
:selector [:db/id :journal-entry-line/location :journal-entry-line/account :journal-entry-line/running-balance :journal-entry-line/client+account+location+date {:journal-entry/_line-items [:journal-entry/date :journal-entry/client]}]
:start [:journal-entry-line/client+account+location+date [client bank-account "A" #inst "2030-01-01"]]
:limit 1
:reverse true
})
(filter (fn [{[c b] :journal-entry-line/client+account+location+date}]
(and (= c client)
(= b bank-account))))
(map :journal-entry-line/running-balance)
(first))
0.0)})))))
(defn get-client [context _ _]
(->graphql

View File

@@ -5,7 +5,7 @@
:refer [assert-admin attach-tracing-resolvers cleanse-query]]
[auto-ap.search :as search]
[auto-ap.utils :refer [heartbeat]]
[datomic.client.api :as dc]
[datomic.api :as dc]
[mount.core :as mount]
[yang.scheduler :as scheduler]))
@@ -19,10 +19,10 @@
(defn rebuild-search-index []
(search/full-index-query
(for [result (map first (dc/qseq '[:find (pull ?a [:ezcater-caterer/search-terms :db/id :ezcater-caterer/name])
:in $
:where [?a :ezcater-caterer/search-terms ]]
(dc/db conn)))]
(for [result (map first (dc/qseq {:query '[:find (pull ?a [:ezcater-caterer/search-terms :db/id :ezcater-caterer/name])
:in $
:where [?a :ezcater-caterer/search-terms ]]
:args [(dc/db conn)]}))]
{:id (:db/id result)
:text (:ezcater-caterer/search-terms result)})
"ezcater-caterer"))

View File

@@ -1,12 +1,21 @@
(ns auto-ap.graphql.import-batch
(:require
[auto-ap.datomic :refer [add-sorter-fields apply-pagination apply-sort-3 conn merge-query pull-many-by-id]]
[auto-ap.datomic
:refer [add-sorter-fields
apply-pagination
apply-sort-3
conn
merge-query
pull-many-by-id
query2]]
[auto-ap.graphql.utils
:refer
[<-graphql assert-admin ident->enum-f result->page attach-tracing-resolvers]]
:refer [<-graphql
assert-admin
attach-tracing-resolvers
ident->enum-f
result->page]]
[clj-time.coerce :as coerce]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[datomic.client.api :as dc]))
[datomic.api :as dc]))
(def default-read '[:db/id
:import-batch/external-id
@@ -32,8 +41,7 @@
:where ['[?e :import-batch/date]]}}))]
(cond->> query
true (dc/q)
(cond->> (query2 query)
true (apply-sort-3 args)
true (apply-pagination args))))

View File

@@ -2,7 +2,7 @@
(:require
[auto-ap.datomic :refer [conn]]
[auto-ap.graphql.utils :refer [->graphql assert-admin]]
[datomic.client.api :as dc]))
[datomic.api :as dc]))
(defn get-intuit-bank-accounts [context _ _]
(assert-admin (:id context))

View File

@@ -2,7 +2,6 @@
(:require
[auto-ap.datomic
:refer [conn pull-attr pull-many pull-ref random-tempid audit-transact audit-transact-batch]]
[iol-ion.tx :refer [upsert-entity upsert-invoice]]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.invoices :as d-invoices]
[auto-ap.datomic.vendors :as d-vendors]
@@ -23,7 +22,7 @@
[clj-time.core :as time]
[clojure.tools.logging :as log]
[com.brunobonacci.mulog :as mu]
[datomic.client.api :as dc]))
[datomic.api :as dc]))
(defn ->graphql [invoice user ]
(if (= "admin" (:user/role user))
@@ -74,7 +73,7 @@
i)]]
(assert-can-see-client (:id context) (-> invoice :invoice/client :db/id))
(assert-not-locked (-> invoice :invoice/client :db/id) (-> invoice :invoice/date)))
(let [transactions (map (fn [i] `(upsert-invoice ~{:db/id i :invoice/import-status :import-status/imported})) invoices)]
(let [transactions (map (fn [i] [:upsert-invoice {:db/id i :invoice/import-status :import-status/imported}]) invoices)]
(audit-transact transactions (:id context))
invoices))
@@ -109,19 +108,19 @@
_ (when-not (:db/id account)
(throw (ex-info (str "Vendor '" (:vendor/name vendor) "' does not have a default expense acount.") {:vendor-id vendor_id})))]
`(upsert-invoice ~{:db/id "invoice"
:invoice/invoice-number invoice_number
:invoice/client client_id
:invoice/vendor vendor_id
:invoice/import-status :import-status/imported
:invoice/total total
:invoice/outstanding-balance total
:invoice/status :invoice-status/unpaid
:invoice/date (coerce/to-date date)
:invoice/expense-accounts (map expense-account->entity
expense_accounts)
:invoice/due (coerce/to-date due)
:invoice/scheduled-payment (coerce/to-date scheduled_payment)})))
[:upsert-invoice {:db/id "invoice"
:invoice/invoice-number invoice_number
:invoice/client client_id
:invoice/vendor vendor_id
:invoice/import-status :import-status/imported
:invoice/total total
:invoice/outstanding-balance total
:invoice/status :invoice-status/unpaid
:invoice/date (coerce/to-date date)
:invoice/expense-accounts (map expense-account->entity
expense_accounts)
:invoice/due (coerce/to-date due)
:invoice/scheduled-payment (coerce/to-date scheduled_payment)}]))
(defn assert-valid-expense-accounts [expense_accounts vendor_id]
(doseq [expense-account expense_accounts
@@ -230,7 +229,7 @@
expense_accounts)
:invoice/due (coerce/to-date due)
:invoice/scheduled-payment (coerce/to-date scheduled_payment)}]
(audit-transact [`(upsert-invoice ~updated-invoice)]
(audit-transact [[:upsert-invoice updated-invoice]]
(:id context))
(-> (d-invoices/get-by-id id)
(->graphql (:id context)))))
@@ -239,13 +238,13 @@
(let [invoice (d-invoices/get-by-id id)]
(assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))
(assert-not-locked (:db/id (:invoice/client invoice)) (:invoice/date invoice))
(audit-transact [`(upsert-invoice ~{:db/id id
(audit-transact [[:upsert-invoice {:db/id id
:invoice/total 0.0
:invoice/outstanding-balance 0.0
:invoice/status :invoice-status/voided
:invoice/expense-accounts (map (fn [ea] {:db/id (:db/id ea)
:invoice-expense-account/amount 0.0})
(:invoice/expense-accounts invoice))})]
(:invoice/expense-accounts invoice))}]]
(:id context))
(-> (d-invoices/get-by-id id) (->graphql (:id context)))))
@@ -308,15 +307,15 @@
(dc/db conn))
(map
(fn [[i]]
`(upsert-invoice ~{:db/id (:db/id i)
:invoice/total 0.0
:invoice/outstanding-balance 0.0
:invoice/status :invoice-status/voided
:invoice/expense-accounts (map
(fn [iea]
{:db/id (:db/id iea)
:invoice-expense-account/amount 0.0})
(:invoice/expense-accounts i))}))))
[:upsert-invoice {:db/id (:db/id i)
:invoice/total 0.0
:invoice/outstanding-balance 0.0
:invoice/status :invoice-status/voided
:invoice/expense-accounts (map
(fn [iea]
{:db/id (:db/id iea)
:invoice-expense-account/amount 0.0})
(:invoice/expense-accounts i))}])))
(:id context))
{:message (str "Succesfully voided " (count all-ids))}))
@@ -325,26 +324,26 @@
_ (assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))
_ (assert-not-locked (:db/id (:invoice/client invoice)) (:invoice/date invoice))
history (dc/history (dc/db conn))
txs (dc/q {:query {:find ['?tx '?e '?original-status '?original-outstanding '?total '?ea '?ea-amount]
:where ['[?e :invoice/status :invoice-status/voided ?tx true]
'[?e :invoice/status ?original-status ?tx false]
'[?e :invoice/outstanding-balance ?original-outstanding ?tx false]
'[?e :invoice/total ?total ?tx false]
'[?ea :invoice-expense-account/amount ?ea-amount ?tx false]]
:in ['$ '?e]}
:args [history id]})
txs (dc/q {:find ['?tx '?e '?original-status '?original-outstanding '?total '?ea '?ea-amount]
:where ['[?e :invoice/status :invoice-status/voided ?tx true]
'[?e :invoice/status ?original-status ?tx false]
'[?e :invoice/outstanding-balance ?original-outstanding ?tx false]
'[?e :invoice/total ?total ?tx false]
'[?ea :invoice-expense-account/amount ?ea-amount ?tx false]]
:in ['$ '?e]}
history id)
[last-transaction] (->> txs (sort-by first) (last))]
(audit-transact [`(upsert-invoice
~(->> txs
(filter (fn [[tx]] (= tx last-transaction)))
(reduce (fn [new-transaction [_ entity original-status original-outstanding total expense-account expense-account-amount]]
(-> new-transaction
(assoc :db/id entity
:invoice/total total
:invoice/status original-status
:invoice/outstanding-balance original-outstanding)
(update :invoice/expense-accounts conj {:db/id expense-account :invoice-expense-account/amount expense-account-amount})))
{})))]
(audit-transact [[:upsert-invoice
(->> txs
(filter (fn [[tx]] (= tx last-transaction)))
(reduce (fn [new-transaction [_ entity original-status original-outstanding total expense-account expense-account-amount]]
(-> new-transaction
(assoc :db/id entity
:invoice/total total
:invoice/status original-status
:invoice/outstanding-balance original-outstanding)
(update :invoice/expense-accounts conj {:db/id expense-account :invoice-expense-account/amount expense-account-amount})))
{}))]]
(:id context))
(-> (d-invoices/get-by-id id)
@@ -360,11 +359,11 @@
(assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))
(assert-not-locked (:db/id (:invoice/client invoice)) (:invoice/date invoice))
(assert (not (seq (:invoice-payment/_invoice invoice))))
(audit-transact [`(upsert-invoice
~{:db/id id
:invoice/status :invoice-status/unpaid
:invoice/outstanding-balance (:invoice/total invoice)
:invoice/scheduled-payment nil})]
(audit-transact [[:upsert-invoice
{:db/id id
:invoice/status :invoice-status/unpaid
:invoice/outstanding-balance (:invoice/total invoice)
:invoice/scheduled-payment nil}]]
(:id context))
(-> (d-invoices/get-by-id id)
@@ -377,10 +376,10 @@
_ (assert-not-locked (:db/id (:invoice/client invoice)) (:invoice/date invoice))
_ (assert-valid-expense-accounts (:expense_accounts args) (:db/id (:invoice/vendor invoice )))]
(audit-transact [`(upsert-invoice ~{:db/id invoice-id
:invoice/expense-accounts (map
expense-account->entity
(:expense_accounts args))})]
(audit-transact [[:upsert-invoice {:db/id invoice-id
:invoice/expense-accounts (map
expense-account->entity
(:expense_accounts args))}]]
(:id context))
(->graphql
(d-invoices/get-by-id (:invoice_id args))
@@ -469,8 +468,8 @@
(log/info "Bulk coding " (count all-ids) args)
(audit-transact-batch
(map (fn [i]
`(upsert-invoice ~{:db/id (:db/id i)
:invoice/expense-accounts (maybe-code-accounts i (:accounts args) locations)}))
[:upsert-invoice {:db/id (:db/id i)
:invoice/expense-accounts (maybe-code-accounts i (:accounts args) locations)}])
invoices)
(:id context))
{:message (str "Successfully coded " (count all-ids) " invoices.")}))

View File

@@ -27,8 +27,8 @@
[clojure.data.csv :as csv]
[clojure.tools.logging :as log]
[com.brunobonacci.mulog :as mu]
[datomic.client.api :as dc]
[iol-ion.tx :refer [random-tempid upsert-ledger]])
[datomic.api :as dc]
[iol-ion.tx :refer [random-tempid]])
(:import
(org.apache.commons.codec.binary Base64)))
@@ -132,26 +132,25 @@
(defn full-ledger-for-client [client-id]
(->> (dc/q
{:query {:find ['?d '?jel '?account '?location '?debit '?credit]
:in ['$ '?client-id]
:where '[[?e :journal-entry/client ?client-id]
[?e :journal-entry/date ?d]
[?e :journal-entry/line-items ?jel]
(or-join [?e]
(and [?e :journal-entry/original-entity ?i]
(or-join [?e ?i]
(and
[?i :transaction/bank-account ?b]
(or [?b :bank-account/include-in-reports true]
(not [?b :bank-account/include-in-reports])))
(not [?i :transaction/bank-account])))
(not [?e :journal-entry/original-entity ]))
[(get-else $ ?jel :journal-entry-line/account :account/unknown) ?account]
[(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit ]
[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
[(get-else $ ?jel :journal-entry-line/location "") ?location]]
}
:args [(dc/db conn) client-id]})
{:find ['?d '?jel '?account '?location '?debit '?credit]
:in ['$ '?client-id]
:where '[[?e :journal-entry/client ?client-id]
[?e :journal-entry/date ?d]
[?e :journal-entry/line-items ?jel]
(or-join [?e]
(and [?e :journal-entry/original-entity ?i]
(or-join [?e ?i]
(and
[?i :transaction/bank-account ?b]
(or [?b :bank-account/include-in-reports true]
(not [?b :bank-account/include-in-reports])))
(not [?i :transaction/bank-account])))
(not [?e :journal-entry/original-entity ]))
[(get-else $ ?jel :journal-entry-line/account :account/unknown) ?account]
[(get-else $ ?jel :journal-entry-line/debit 0.0) ?debit ]
[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
[(get-else $ ?jel :journal-entry-line/location "") ?location]]}
(dc/db conn) client-id)
(sort-by first)))
(defn get-balance-sheet [context args _]
@@ -438,71 +437,71 @@
(assoc entry
:status :success
:tx
`(upsert-ledger
~(remove-nils
{:journal-entry/source (:source entry)
:journal-entry/client [:client/code (:client_code entry)]
:journal-entry/date (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry)))
:journal-entry/external-id (:external_id entry)
:journal-entry/vendor (:db/id (all-vendors (:vendor_name entry)))
:journal-entry/amount (:amount entry)
:journal-entry/note (:note entry)
:journal-entry/cleared-against (:cleared_against entry)
[:upsert-ledger
(remove-nils
{:journal-entry/source (:source entry)
:journal-entry/client [:client/code (:client_code entry)]
:journal-entry/date (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry)))
:journal-entry/external-id (:external_id entry)
:journal-entry/vendor (:db/id (all-vendors (:vendor_name entry)))
:journal-entry/amount (:amount entry)
:journal-entry/note (:note entry)
:journal-entry/cleared-against (:cleared_against entry)
:journal-entry/line-items
(mapv (fn [ea]
(let [debit (or (:debit ea) 0.0)
credit (or (:credit ea) 0.0)]
(when (and (not (get
(get all-client-locations (:client_code entry))
(:location ea)))
(not= "A" (:location ea)))
(throw (ex-info (str "Location '" (:location ea) "' not found.")
:journal-entry/line-items
(mapv (fn [ea]
(let [debit (or (:debit ea) 0.0)
credit (or (:credit ea) 0.0)]
(when (and (not (get
(get all-client-locations (:client_code entry))
(:location ea)))
(not= "A" (:location ea)))
(throw (ex-info (str "Location '" (:location ea) "' not found.")
{:status :error})))
(when (and (<= debit 0.0)
(<= credit 0.0))
(throw (ex-info (str "Line item amount " (or debit credit) " must be greater than 0.")
{:status :error})))
(when (and (not (all-accounts (:account_identifier ea)))
(not (get
(get all-client-bank-accounts (:client_code entry))
(:account_identifier ea))))
(throw (ex-info (str "Account '" (:account_identifier ea) "' not found.")
{:status :error})))
(let [matching-account (when (re-matches #"^[0-9]+$" (:account_identifier ea))
(a/get-account-by-numeric-code-and-sets (Integer/parseInt (:account_identifier ea)) ["default"]))]
(when (and matching-account
(:account/location matching-account)
(not= (:account/location matching-account)
(:location ea)))
(throw (ex-info (str "Account '"
(:account/numeric-code matching-account)
"' requires location '"
(:account/location matching-account)
"' but got '"
(:location ea)
"'")
{:status :error})))
(when (and (<= debit 0.0)
(<= credit 0.0))
(throw (ex-info (str "Line item amount " (or debit credit) " must be greater than 0.")
(when (and matching-account
(not (:account/location matching-account))
(= "A" (:location ea)))
(throw (ex-info (str "Account '"
(:account/numeric-code matching-account)
"' cannot use location '"
(:location ea)
"'")
{:status :error})))
(when (and (not (all-accounts (:account_identifier ea)))
(not (get
(get all-client-bank-accounts (:client_code entry))
(:account_identifier ea))))
(throw (ex-info (str "Account '" (:account_identifier ea) "' not found.")
{:status :error})))
(let [matching-account (when (re-matches #"^[0-9]+$" (:account_identifier ea))
(a/get-account-by-numeric-code-and-sets (Integer/parseInt (:account_identifier ea)) ["default"]))]
(when (and matching-account
(:account/location matching-account)
(not= (:account/location matching-account)
(:location ea)))
(throw (ex-info (str "Account '"
(:account/numeric-code matching-account)
"' requires location '"
(:account/location matching-account)
"' but got '"
(:location ea)
"'")
{:status :error})))
(when (and matching-account
(not (:account/location matching-account))
(= "A" (:location ea)))
(throw (ex-info (str "Account '"
(:account/numeric-code matching-account)
"' cannot use location '"
(:location ea)
"'")
{:status :error})))
(remove-nils (cond-> {:db/id (random-tempid)
:journal-entry-line/location (:location ea)
:journal-entry-line/debit (when (> debit 0)
debit)
:journal-entry-line/credit (when (> credit 0)
credit)}
matching-account (assoc :journal-entry-line/account (:db/id matching-account))
(not matching-account) (assoc :journal-entry-line/account [:bank-account/code (:account_identifier ea)]))))))
(:line_items entry))
:journal-entry/cleared true}))))))
(remove-nils (cond-> {:db/id (random-tempid)
:journal-entry-line/location (:location ea)
:journal-entry-line/debit (when (> debit 0)
debit)
:journal-entry-line/credit (when (> credit 0)
credit)}
matching-account (assoc :journal-entry-line/account (:db/id matching-account))
(not matching-account) (assoc :journal-entry-line/account [:bank-account/code (:account_identifier ea)]))))))
(:line_items entry))
:journal-entry/cleared true})]))))
(:entries args))))
errors (filter #(= (:status %) :error) transaction)
ignored (filter #(= (:status %) :ignored) transaction)

View File

@@ -7,7 +7,8 @@
conn
merge-query
pull-attr
pull-many-by-id]]
pull-many-by-id
query2]]
[auto-ap.graphql.utils
:refer [->graphql
<-graphql
@@ -20,7 +21,7 @@
[clj-time.coerce :as coerce]
[clj-time.core :as time]
[clojure.tools.logging :as log]
[datomic.client.api :as dc]))
[datomic.api :as dc]))
(defn plaid-link-token [context value _]
(when-not (:client_id value)
@@ -46,16 +47,15 @@
:plaid-item/last-updated (coerce/to-date (time/now))
:db/id "plaid-item"}]
(dc/transact conn {:tx-data
(->> (:accounts account-result)
(map (fn [a]
(let [balance (some-> a :balances :current (* 0.01))]
(cond-> {:plaid-account/external-id (:account_id a)
:plaid-account/number (:mask a)
:plaid-account/name (str (:name a) " " (:mask a))
:plaid-item/_accounts "plaid-item"}
balance (assoc :plaid-account/balance balance)))))
(into [item]))})
@(dc/transact conn (->> (:accounts account-result)
(map (fn [a]
(let [balance (some-> a :balances :current (* 0.01))]
(cond-> {:plaid-account/external-id (:account_id a)
:plaid-account/number (:mask a)
:plaid-account/name (str (:name a) " " (:mask a))
:plaid-item/_accounts "plaid-item"}
balance (assoc :plaid-account/balance balance)))))
(into [item])))
(log/info "Access token was " access-token)
{:message (str "Plaid linked successfully.")}))
@@ -93,8 +93,7 @@
(merge-query {:query {:find ['?e]
:where ['[?e :plaid-item/external-id]]}}))]
(cond->> query
true (dc/q)
(cond->> (query2 query)
true (apply-sort-3 args)
true (apply-pagination args))))
@@ -126,7 +125,7 @@
(defn delete-plaid-item [context args _]
(assert-admin (:id context))
(assert-present args :id)
(dc/transact conn {:tx-data [[:db/retractEntity (:id args)]]})
@(dc/transact conn [[:db/retractEntity (:id args)]])
{:message "Item deleted."})
(defn attach [schema]

View File

@@ -6,7 +6,7 @@
[auto-ap.graphql.utils
:refer [<-graphql assert-admin attach-tracing-resolvers result->page]]
[config.core :refer [env]]
[datomic.client.api :as dc]))
[datomic.api :as dc]))
(defn get-report-page [context args _]
(let [args (assoc args :id (:id context))
@@ -25,7 +25,7 @@
(when id-to-delete
(s3/delete-object :bucket-name (:data-bucket env)
:key key)
(dc/transact conn {:tx-data [[:db/retractEntity id-to-delete]]}))
@(dc/transact conn [[:db/retractEntity id-to-delete]]))
{:message (format "deleted %s successfully" key)}))

View File

@@ -1,7 +1,6 @@
(ns auto-ap.graphql.transaction-rules
(:require
[auto-ap.datomic :refer [audit-transact conn merge-query]]
[iol-ion.tx :refer [upsert-entity random-tempid]]
[auto-ap.datomic :refer [audit-transact conn merge-query query2]]
[auto-ap.datomic.transaction-rules :as tr]
[auto-ap.datomic.transactions :as d-transactions]
[auto-ap.graphql.utils
@@ -16,8 +15,9 @@
[auto-ap.utils :refer [dollars=]]
[clj-time.coerce :as c]
[clojure.string :as str]
[datomic.client.api :as dc]
[com.brunobonacci.mulog :as mu]))
[com.brunobonacci.mulog :as mu]
[datomic.api :as dc]
[iol-ion.tx :refer [random-tempid]]))
(defn get-transaction-rule-page [context args _]
(let [args (assoc args :id (:id context))
@@ -82,7 +82,7 @@
rule-id (if id
id
"transaction-rule")
transaction [`(upsert-entity ~#:transaction-rule {:db/id (or rule-id (random-tempid))
transaction [[:upsert-entity #:transaction-rule {:db/id (or rule-id (random-tempid))
:description description
:note note
:client client_id
@@ -98,7 +98,7 @@
name
snake->kebab
(keyword "transaction-approval-status"))
:transaction-rule/accounts (map transaction-rule-account->entity accounts)})]
:transaction-rule/accounts (map transaction-rule-account->entity accounts)}]]
transaction-result (audit-transact transaction (:id context))]
@@ -108,87 +108,85 @@
(->graphql))))
(defn -test-transaction-rule [id {:keys [:transaction-rule/description :transaction-rule/client :transaction-rule/bank-account :transaction-rule/amount-lte :transaction-rule/amount-gte :transaction-rule/dom-lte :transaction-rule/dom-gte :transaction-rule/yodlee-merchant]} include-coded? count]
(->>
(dc/q
(cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name]
:transaction/bank-account [:bank-account/name]
:transaction/payment [:db/id]}
])]
:in ['$ ]
:where []}
:args [(dc/db conn)]
:timeout 55000}
description
(merge-query {:query {:in ['?descr]
:where ['[(iol-ion.query/->pattern ?descr) ?description-regex]]}
:args [description]})
(let [query (cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name]
:transaction/bank-account [:bank-account/name]
:transaction/payment [:db/id]}
])]
:in ['$ ]
:where []}
:args [(dc/db conn)]}
description
(merge-query {:query {:in ['?descr]
:where ['[(iol-ion.query/->pattern ?descr) ?description-regex]]}
:args [description]})
(limited-clients id)
(merge-query {:query {:in ['[?xx ...]]
:where ['[?e :transaction/client ?xx]]}
:args [(set (map :db/id (limited-clients id)))]})
(limited-clients id)
(merge-query {:query {:in ['[?xx ...]]
:where ['[?e :transaction/client ?xx]]}
:args [(set (map :db/id (limited-clients id)))]})
bank-account
(merge-query {:query {:in ['?bank-account-id]
:where ['[?e :transaction/bank-account ?bank-account-id]]}
:args [(:db/id bank-account)]})
bank-account
(merge-query {:query {:in ['?bank-account-id]
:where ['[?e :transaction/bank-account ?bank-account-id]]}
:args [(:db/id bank-account)]})
description
(merge-query {:query {:where ['[?e :transaction/description-original ?do]
'[(re-find ?description-regex ?do)]]}})
description
(merge-query {:query {:where ['[?e :transaction/description-original ?do]
'[(re-find ?description-regex ?do)]]}})
yodlee-merchant
(merge-query {:query {:in ['?yodlee-merchant-id]
:where ['[?e :transaction/yodlee-merchant ?yodlee-merchant-id]]}
:args [(:db/id yodlee-merchant)]})
yodlee-merchant
(merge-query {:query {:in ['?yodlee-merchant-id]
:where ['[?e :transaction/yodlee-merchant ?yodlee-merchant-id]]}
:args [(:db/id yodlee-merchant)]})
amount-gte
(merge-query {:query {:in ['?amount-gte]
:where ['[?e :transaction/amount ?ta]
'[(>= ?ta ?amount-gte)]]}
:args [amount-gte]})
amount-gte
(merge-query {:query {:in ['?amount-gte]
:where ['[?e :transaction/amount ?ta]
'[(>= ?ta ?amount-gte)]]}
:args [amount-gte]})
amount-lte
(merge-query {:query {:in ['?amount-lte]
:where ['[?e :transaction/amount ?ta]
'[(<= ?ta ?amount-lte)]]}
:args [amount-lte]})
amount-lte
(merge-query {:query {:in ['?amount-lte]
:where ['[?e :transaction/amount ?ta]
'[(<= ?ta ?amount-lte)]]}
:args [amount-lte]})
dom-lte
(merge-query {:query {:in ['?dom-lte]
:where ['[?e :transaction/date ?transaction-date]
'[(iol-ion.query/dom ?transaction-date) ?dom]
'[(<= ?dom ?dom-lte)]]}
:args [dom-lte]})
dom-lte
(merge-query {:query {:in ['?dom-lte]
:where ['[?e :transaction/date ?transaction-date]
'[(iol-ion.query/dom ?transaction-date) ?dom]
'[(<= ?dom ?dom-lte)]]}
:args [dom-lte]})
dom-gte
(merge-query {:query {:in ['?dom-gte]
:where ['[?e :transaction/date ?transaction-date]
'[(iol-ion.query/dom ?transaction-date) ?dom]
'[(>= ?dom ?dom-gte)]]}
:args [dom-gte]})
dom-gte
(merge-query {:query {:in ['?dom-gte]
:where ['[?e :transaction/date ?transaction-date]
'[(iol-ion.query/dom ?transaction-date) ?dom]
'[(>= ?dom ?dom-gte)]]}
:args [dom-gte]})
client
(merge-query {:query {:in ['?client-id]
:where ['[?e :transaction/client ?client-id]]}
:args [(:db/id client)]})
client
(merge-query {:query {:in ['?client-id]
:where ['[?e :transaction/client ?client-id]]}
:args [(:db/id client)]})
(not include-coded?)
(merge-query {:query {:where ['[or [?e :transaction/approval-status :transaction-approval-status/unapproved]
[(missing? $ ?e :transaction/approval-status)]]]}})
(not include-coded?)
(merge-query {:query {:where ['[or [?e :transaction/approval-status :transaction-approval-status/unapproved]
[(missing? $ ?e :transaction/approval-status)]]]}})
true
(merge-query {:query {:where ['[?e :transaction/id]]}})))
(transduce (comp
(take (or count 15))
(map first)
(map #(dissoc % :transaction/id))
(map (fn [x]
(update x :transaction/date c/from-date)))
(map ->graphql))
conj [])))
true
(merge-query {:query {:where ['[?e :transaction/id]]}}))]
(->>
(query2 query)
(transduce (comp
(take (or count 15))
(map first)
(map #(dissoc % :transaction/id))
(map (fn [x]
(update x :transaction/date c/from-date)))
(map ->graphql))
conj []))))
(defn test-transaction-rule [{:keys [id]} {{:keys [description client_id bank_account_id amount_lte amount_gte dom_lte dom_gte yodlee_merchant_id]} :transaction_rule} _]
(assert-admin id)

View File

@@ -25,8 +25,8 @@
[clojure.set :as set]
[clojure.string :as str]
[clojure.tools.logging :as log]
[datomic.client.api :as dc]
[iol-ion.tx :refer [random-tempid upsert-transaction]]
[datomic.api :as dc]
[iol-ion.tx :refer [random-tempid]]
[com.brunobonacci.mulog :as mu]))
(def approval-status->graphql (ident->enum-f :transaction/approval-status))
@@ -92,8 +92,8 @@
(audit-transact-batch
(->> all-ids
(mapv (fn [t]
`(upsert-transaction ~{:db/id t
:transaction/approval-status (enum->keyword (:status args) "transaction-approval-status")}))))
[:upsert-transaction {:db/id t
:transaction/approval-status (enum->keyword (:status args) "transaction-approval-status")}])))
(:id context))
{:message (str "Succesfully changed " (count all-ids) " transactions to be " (name (:status args) ) ".")}))
@@ -172,10 +172,10 @@
(log/info "Bulk coding " (count all-ids) args)
(audit-transact-batch
(map (fn [i]
`(upsert-transaction ~(cond-> i
(:approval_status args) (assoc :transaction/approval-status (enum->keyword (:approval_status args) "transaction-approval-status"))
(:vendor args) (assoc :transaction/vendor (:vendor args))
(seq (:accounts args)) (assoc :transaction/accounts (maybe-code-accounts i (:accounts args) locations)))))
[:upsert-transaction (cond-> i
(:approval_status args) (assoc :transaction/approval-status (enum->keyword (:approval_status args) "transaction-approval-status"))
(:vendor args) (assoc :transaction/vendor (:vendor args))
(seq (:accounts args)) (assoc :transaction/accounts (maybe-code-accounts i (:accounts args) locations)))])
transactions)
(:id context))
{:message (str "Successfully coded " (count all-ids) " transactions.")}))
@@ -252,13 +252,12 @@
(assert-not-locked (:db/id (:transaction/client transaction)) (-> transaction :transaction/payment :payment/date)))
_ (log/info "Unlinking" transaction)
payment (-> transaction :transaction/payment )
is-autopay-payment? (some->> (doto (dc/q {:query {:find ['?sp]
:in ['$ '?payment]
:where ['[?ip :invoice-payment/payment ?payment]
'[?ip :invoice-payment/invoice ?i]
'[(get-else $ ?i :invoice/scheduled-payment "N/A") ?sp]]}
:args [(dc/db conn) (:db/id payment)]})
log/info)
is-autopay-payment? (some->> (dc/q {:find ['?sp]
:in ['$ '?payment]
:where ['[?ip :invoice-payment/payment ?payment]
'[?ip :invoice-payment/invoice ?i]
'[(get-else $ ?i :invoice/scheduled-payment "N/A") ?sp]]}
(dc/db conn) (:db/id payment))
seq
(map first)
(every? #(instance? java.util.Date %)))
@@ -273,33 +272,34 @@
(audit-transact
(-> [{:db/id (:db/id payment)
:payment/status :payment-status/pending}
`(upsert-transaction
~{:db/id transaction-id
:transaction/approval-status :transaction-approval-status/unapproved
:transaction/payment nil
:transaction/vendor nil
:transaction/location nil
:transaction/accounts nil})
[:upsert-transaction
{:db/id transaction-id
:transaction/approval-status :transaction-approval-status/unapproved
:transaction/payment nil
:transaction/vendor nil
:transaction/location nil
:transaction/accounts nil}]
[:db/retractEntity (:db/id payment) ]]
(into (map (fn [[invoice-payment]]
[:db/retractEntity invoice-payment])
(dc/q {:query {:find ['?ip]
:in ['$ '?p]
:where ['[?ip :invoice-payment/payment ?p]]}
:args [(dc/db conn) (:db/id payment)]} ))))
(dc/q {:find ['?ip]
:in ['$ '?p]
:where ['[?ip :invoice-payment/payment ?p]]}
(dc/db conn)
(:db/id payment) ))))
(:id context))
(audit-transact
[{:db/id (:db/id payment)
:payment/status :payment-status/pending}
`(upsert-transaction
~{:db/id transaction-id
:transaction/approval-status :transaction-approval-status/unapproved
:transaction/payment nil
:transaction/vendor nil
:transaction/location nil
:transaction/accounts nil})]
[:upsert-transaction
{:db/id transaction-id
:transaction/approval-status :transaction-approval-status/unapproved
:transaction/payment nil
:transaction/vendor nil
:transaction/location nil
:transaction/accounts nil}]]
(:id context)))
(-> (d-transactions/get-by-id transaction-id)
approval-status->graphql
@@ -360,14 +360,14 @@
(when missing-locations
(throw (ex-info (str "Location '" (str/join ", " missing-locations) "' not found on client.") {})) )
(audit-transact [`(upsert-transaction ~{:db/id id
:transaction/vendor vendor_id
:transaction/approval-status (some->> approval_status
name
snake->kebab
(keyword "transaction-approval-status"))
:transaction/accounts (map transaction-account->entity accounts)
:transaction/forecast-match forecast_match})]
(audit-transact [[:upsert-transaction {:db/id id
:transaction/vendor vendor_id
:transaction/approval-status (some->> approval_status
name
snake->kebab
(keyword "transaction-approval-status"))
:transaction/accounts (map transaction-account->entity accounts)
:transaction/forecast-match forecast_match}]]
(:id context))
(-> (d-transactions/get-by-id id)
approval-status->graphql
@@ -392,16 +392,16 @@
:payment/date (coerce/to-date (first (sort [(:payment/date payment)
(:transaction/date transaction)])))}
`(upsert-transaction
~{:db/id (:db/id transaction)
:transaction/payment (:db/id payment)
:transaction/vendor (:db/id (:payment/vendor payment))
:transaction/location "A"
:transaction/approval-status :transaction-approval-status/approved
:transaction/accounts [{:db/id (random-tempid)
:transaction-account/account (:db/id (a/get-account-by-numeric-code-and-sets 21000 ["default"]))
:transaction-account/location "A"
:transaction-account/amount (Math/abs (:transaction/amount transaction))}]})])
[:upsert-transaction
{:db/id (:db/id transaction)
:transaction/payment (:db/id payment)
:transaction/vendor (:db/id (:payment/vendor payment))
:transaction/location "A"
:transaction/approval-status :transaction-approval-status/approved
:transaction/accounts [{:db/id (random-tempid)
:transaction-account/account (:db/id (a/get-account-by-numeric-code-and-sets 21000 ["default"]))
:transaction-account/location "A"
:transaction-account/amount (Math/abs (:transaction/amount transaction))}]}]])
(:id context)))
(-> (d-transactions/get-by-id transaction_id)
approval-status->graphql
@@ -509,13 +509,13 @@
(throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"}))))
(audit-transact (mapv (fn [t]
`(upsert-transaction
~(remove-nils (rm/apply-rule {:db/id (:db/id t)
:transaction/amount (:transaction/amount t)}
transaction-rule
[:upsert-transaction
(remove-nils (rm/apply-rule {:db/id (:db/id t)
:transaction/amount (:transaction/amount t)}
transaction-rule
(or (-> t :transaction/bank-account :bank-account/locations)
(-> t :transaction/client :client/locations))))))
(or (-> t :transaction/bank-account :bank-account/locations)
(-> t :transaction/client :client/locations))))])
transactions)
(:id context))
)

View File

@@ -4,7 +4,7 @@
[clj-time.coerce :as coerce]
[auto-ap.time :as atime]
[buddy.auth :refer [throw-unauthorized]]
[datomic.client.api :as dc]
[datomic.api :as dc]
[clojure.walk :as walk]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[clojure.tools.logging :as log]

View File

@@ -1,7 +1,7 @@
(ns auto-ap.graphql.vendors
(:require
[auto-ap.datomic :refer [audit-transact conn pull-attr remove-nils]]
[iol-ion.tx :refer [upsert-entity random-tempid]]
[iol-ion.tx :refer [random-tempid]]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.graphql.utils
:refer [->graphql
@@ -19,7 +19,7 @@
[manifold.executor :as ex]
[manifold.deferred :as de]
[clojure.tools.logging :as log]
[datomic.client.api :as dc]
[datomic.api :as dc]
[yang.scheduler :as scheduler]
[mount.core :as mount]
[clj-time.core :as time]
@@ -87,55 +87,55 @@
:dom (:dom ao)
:db/id (or (:id ao) (random-tempid))})
schedule_payment_dom)
transaction `(upsert-entity ~(cond-> #:vendor {:db/id (if id
id
"vendor")
:name name
:code code
:hidden hidden
:terms terms
:print-as print_as
:default-account default_account_id
:invoice-reminder-schedule (keyword invoice_reminder_schedule)
:address (when address
(remove-nils #:address {:db/id (if (:id address)
(:id address)
"address")
:street1 (:street1 address)
:street2 (:street2 address)
:city (:city address)
:state (:state address)
:zip (:zip address)}))
:primary-contact (when primary_contact
transaction [:upsert-entity (cond-> #:vendor {:db/id (if id
id
"vendor")
:name name
:code code
:hidden hidden
:terms terms
:print-as print_as
:default-account default_account_id
:invoice-reminder-schedule (keyword invoice_reminder_schedule)
:address (when address
(remove-nils #:address {:db/id (if (:id address)
(:id address)
"address")
:street1 (:street1 address)
:street2 (:street2 address)
:city (:city address)
:state (:state address)
:zip (:zip address)}))
:primary-contact (when primary_contact
(remove-nils #:contact {:db/id (if (:id primary_contact)
(:id primary_contact)
"primary")
:name (:name primary_contact)
:phone (:phone primary_contact)
:email (:email primary_contact)}))
:secondary-contact (when secondary_contact
(remove-nils #:contact {:db/id (if (:id primary_contact)
(:id primary_contact)
"primary")
:name (:name primary_contact)
:phone (:phone primary_contact)
:email (:email primary_contact)}))
:secondary-contact (when secondary_contact
(remove-nils #:contact {:db/id (if (:id secondary_contact)
(:id secondary_contact)
"secondary")
:name (:name secondary_contact)
:phone (:phone secondary_contact)
:email (:email secondary_contact)})
)
:search-terms [name]}
(is-admin? (:id context)) (assoc
:vendor/legal-entity-name (:legal_entity_name in)
:vendor/legal-entity-first-name (:legal_entity_first_name in)
:vendor/legal-entity-middle-name (:legal_entity_middle_name in)
:vendor/legal-entity-last-name (:legal_entity_last_name in)
:vendor/legal-entity-tin (:legal_entity_tin in)
:vendor/legal-entity-tin-type (enum->keyword (:legal_entity_tin_type in) "legal-entity-tin-type")
:vendor/legal-entity-1099-type (enum->keyword (:legal_entity_1099_type in) "legal-entity-1099-type")
:vendor/account-overrides account-overrides
:vendor/terms-overrides terms-overrides
:vendor/schedule-payment-dom schedule-payment-dom
:vendor/automatically-paid-when-due (:automatically_paid_when_due in))))
(remove-nils #:contact {:db/id (if (:id secondary_contact)
(:id secondary_contact)
"secondary")
:name (:name secondary_contact)
:phone (:phone secondary_contact)
:email (:email secondary_contact)})
)
:search-terms [name]}
(is-admin? (:id context)) (assoc
:vendor/legal-entity-name (:legal_entity_name in)
:vendor/legal-entity-first-name (:legal_entity_first_name in)
:vendor/legal-entity-middle-name (:legal_entity_middle_name in)
:vendor/legal-entity-last-name (:legal_entity_last_name in)
:vendor/legal-entity-tin (:legal_entity_tin in)
:vendor/legal-entity-tin-type (enum->keyword (:legal_entity_tin_type in) "legal-entity-tin-type")
:vendor/legal-entity-1099-type (enum->keyword (:legal_entity_1099_type in) "legal-entity-1099-type")
:vendor/account-overrides account-overrides
:vendor/terms-overrides terms-overrides
:vendor/schedule-payment-dom schedule-payment-dom
:vendor/automatically-paid-when-due (:automatically_paid_when_due in)))]
transaction-result (audit-transact [transaction] (:id context))]
@@ -145,11 +145,12 @@
(->graphql))))
(defn merge-vendors [context {:keys [from to]} _]
(let [transaction (->> (dc/q {:query {:find '[?x ?a2]
:in '[$ ?vendor-from ]
:where ['[?x ?a ?vendor-from]
'[?a :db/ident ?a2]]}
:args [(dc/db conn) from]})
(let [transaction (->> (dc/q {:find '[?x ?a2]
:in '[$ ?vendor-from ]
:where ['[?x ?a ?vendor-from]
'[?a :db/ident ?a2]]}
(dc/db conn)
from)
(mapcat (fn [[src attr]]
[[:db/retract src attr from]
@@ -195,10 +196,10 @@
(de/future-with
single-thread
(search/full-index-query
(for [[result] (dc/qseq '[:find (pull ?v [:vendor/search-terms :db/id :vendor/name :vendor/hidden])
:in $
:where [?v :vendor/search-terms ]]
(dc/db conn))]
(for [[result] (dc/qseq {:query '[:find (pull ?v [:vendor/search-terms :db/id :vendor/name :vendor/hidden])
:in $
:where [?v :vendor/search-terms ]]
:args [(dc/db conn)]})]
{:id (:db/id result)
:text (or (first (:vendor/search-terms result))
(:vendor/name result))
@@ -216,12 +217,11 @@
_ (mu/log ::indexing
:last-run last-run-basis-value
:starting-from (:basisT db))
results (for [[result] (dc/qseq '[:find (pull ?v [:vendor/search-terms :db/id :vendor/name :vendor/hidden])
:in $ $$
:where [$ ?v :vendor/name ]
[$$ ?v]]
db
recent)]
results (for [[result] (dc/qseq {:query '[:find (pull ?v [:vendor/search-terms :db/id :vendor/name :vendor/hidden])
:in $ $$
:where [$ ?v :vendor/name ]
[$$ ?v]]
:args [db recent]})]
{:id (:db/id result)
:text (or (first (:vendor/search-terms result))
(:vendor/name result))