makes plaid really usable, allowing choosing on bank screen, and searchable on vendors page.

This commit is contained in:
Bryce
2023-07-22 21:33:06 -07:00
parent ff2d64fee5
commit dfd1af77c4
15 changed files with 101 additions and 21 deletions

View File

@@ -1626,6 +1626,9 @@
:db/doc "An unhashed version of the id", :db/doc "An unhashed version of the id",
:db/ident :transaction/raw-id, :db/ident :transaction/raw-id,
} }
{:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/ident :transaction/plaid-merchant}
{:db/valueType #:db{:ident :db.type/ref}, {:db/valueType #:db{:ident :db.type/ref},
:db/cardinality #:db{:ident :db.cardinality/one}, :db/cardinality #:db{:ident :db.cardinality/one},
@@ -1922,6 +1925,11 @@
:db/doc "If not a person, the legal entity naame", :db/doc "If not a person, the legal entity naame",
:db/ident :vendor/legal-entity-name, :db/ident :vendor/legal-entity-name,
} }
{:db/valueType db.type/ref,
:db/cardinality :db.cardinality/one,
:db/doc "A link to the plaid merchant for this vendor",
:db/ident :vendor/plaid-merchant,
}
{:db/valueType #:db{:ident :db.type/ref}, {:db/valueType #:db{:ident :db.type/ref},
:db/cardinality #:db{:ident :db.cardinality/one}, :db/cardinality #:db{:ident :db.cardinality/one},
@@ -2097,4 +2105,11 @@
:db/doc "The amount of money in the cash drawer at the start of the shift." :db/doc "The amount of money in the cash drawer at the start of the shift."
:db/valueType :db.type/double :db/valueType :db.type/double
:db/cardinality :db.cardinality/one} :db/cardinality :db.cardinality/one}
{:db/ident :plaid-merchant/name
:db/doc "A plaid merchant"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
] ]

View File

@@ -200,7 +200,8 @@
:account/location :account/location
{:account/client-overrides [:account-client-override/name {:account/client-overrides [:account-client-override/name
{:account-client-override/client [:db/id]}]}]}] {:account-client-override/client [:db/id]}]}]}]
:transaction/yodlee-merchant [:db/id :yodlee-merchant/yodlee-id :yodlee-merchant/name]}] :transaction/yodlee-merchant [:db/id :yodlee-merchant/yodlee-id :yodlee-merchant/name]
:transaction/plaid-merchant [:db/id :plaid-merchant/name]}]
ids) ids)
(map #(update % :transaction/date coerce/from-date)) (map #(update % :transaction/date coerce/from-date))
(map #(update % :transaction/post-date coerce/from-date)) (map #(update % :transaction/post-date coerce/from-date))

View File

@@ -47,6 +47,7 @@
:vendor/legal-entity-tin-type [:db/ident :db/id] :vendor/legal-entity-tin-type [:db/ident :db/id]
:vendor/legal-entity-1099-type [:db/ident :db/id] :vendor/legal-entity-1099-type [:db/ident :db/id]
:vendor/default-account [:db/id :account/numeric-code :account/name] :vendor/default-account [:db/id :account/numeric-code :account/name]
:vendor/plaid-merchant [:db/id :plaid-merchant/name]
:vendor-usage/_vendor [:vendor-usage/client :vendor-usage/count]}]) :vendor-usage/_vendor [:vendor-usage/client :vendor-usage/count]}])
@@ -124,6 +125,7 @@
{:vendor/default-account [:account/name :db/id :account/location] {:vendor/default-account [:account/name :db/id :account/location]
:vendor/legal-entity-tin-type [:db/ident :db/id] :vendor/legal-entity-tin-type [:db/ident :db/id]
:vendor/legal-entity-1099-type [:db/ident :db/id] :vendor/legal-entity-1099-type [:db/ident :db/id]
:vendor/plaid-merchant [:db/id :plaid-merchant/name]
:vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id] :vendor/account-overrides [* {:vendor-account-override/client [:client/name :db/id]
:vendor-account-override/account [:account/name :account/numeric-code :db/id]}] :vendor-account-override/account [:account/name :account/numeric-code :db/id]}]
:vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}] :vendor/terms-overrides [* {:vendor-terms-override/client [:client/name :db/id]}]

View File

@@ -176,6 +176,7 @@
:print_as {:type 'String} :print_as {:type 'String}
:primary_contact {:type :contact} :primary_contact {:type :contact}
:plaid_merchant {:type :plaid_merchant}
:secondary_contact {:type :contact} :secondary_contact {:type :contact}
:address {:type :address} :address {:type :address}
@@ -210,6 +211,9 @@
:yodlee_id {:type 'String} :yodlee_id {:type 'String}
:name {:type 'String}}} :name {:type 'String}}}
:plaid_merchant {:fields {:id {:type :id}
:name {:type 'String}}}
:intuit_bank_account {:fields {:id {:type :id} :intuit_bank_account {:fields {:id {:type :id}
:external_id {:type 'String} :external_id {:type 'String}
:name {:type 'String}}} :name {:type 'String}}}
@@ -470,6 +474,7 @@
:secondary_contact {:type :add_contact} :secondary_contact {:type :add_contact}
:address {:type :add_address} :address {:type :add_address}
:default_account_id {:type :id} :default_account_id {:type :id}
:plaid_merchant {:type :id}
:account_overrides {:type '(list :add_account_override)} :account_overrides {:type '(list :add_account_override)}
:schedule_payment_dom {:type '(list :add_schedule_payment_dom)} :schedule_payment_dom {:type '(list :add_schedule_payment_dom)}
:invoice_reminder_schedule {:type 'String} :invoice_reminder_schedule {:type 'String}
@@ -882,3 +887,4 @@
:exception e) :exception e)
(throw e)))))))))) (throw e))))))))))

View File

@@ -16,12 +16,16 @@
assert-can-see-client assert-can-see-client
assert-present assert-present
attach-tracing-resolvers attach-tracing-resolvers
cleanse-query
limited-clients]] limited-clients]]
[auto-ap.plaid.core :as p] [auto-ap.plaid.core :as p]
[clj-time.coerce :as coerce] [clj-time.coerce :as coerce]
[clj-time.core :as time] [clj-time.core :as time]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[datomic.api :as dc])) [datomic.api :as dc]
[auto-ap.solr :as solr]
[manifold.deferred :as de]
[manifold.executor :as ex]))
(defn plaid-link-token [context value _] (defn plaid-link-token [context value _]
(when-not (:client_id value) (when-not (:client_id value)
@@ -128,6 +132,30 @@
@(dc/transact conn [[:db/retractEntity (:id args)]]) @(dc/transact conn [[:db/retractEntity (:id args)]])
{:message "Item deleted."}) {:message "Item deleted."})
(defn search-merchants [context args _]
(if-let [query (not-empty (cleanse-query (:query args)))]
(let [search-query (str "name:(" query ")")]
(for [{:keys [id name]} (solr/query solr/impl "plaid_merchants" {"query" search-query
"fields" "id, name"})]
{:id (Long/parseLong id)
:name (first name)}))
[]))
(def single-thread (ex/fixed-thread-executor 1))
(defn rebuild-search-index []
(de/future-with
single-thread
(auto-ap.solr/index-documents-raw
auto-ap.solr/impl
"plaid_merchants"
(for [[result] (dc/qseq {:query '[:find (pull ?v [:plaid-merchant/name :db/id])
:in $
:where [?v :plaid-merchant/name]]
:args [(dc/db conn)]})]
{"id" (:db/id result)
"name" (:plaid-merchant/name result)}))))
(defn attach [schema] (defn attach [schema]
(-> (->
(merge-with merge schema (merge-with merge schema
@@ -162,7 +190,10 @@
:sort {:type '(list :sort_item)} :sort {:type '(list :sort_item)}
:start {:type 'Int} :start {:type 'Int}
:per_page {:type 'Int}} :per_page {:type 'Int}}
:resolve :get-plaid-item-page}} :resolve :get-plaid-item-page}
:search_plaid_merchants {:type '(list :plaid_merchant)
:args {:query {:type 'String}}
:resolve :search-plaid-merchants}}
:mutations {:link_plaid {:type :message :mutations {:link_plaid {:type :message
:args {:client_code {:type 'String} :args {:client_code {:type 'String}
:public_token {:type 'String}} :public_token {:type 'String}}
@@ -171,6 +202,7 @@
:args {:id {:type :id}} :args {:id {:type :id}}
:resolve :mutation/delete-plaid-item}}}) :resolve :mutation/delete-plaid-item}}})
(attach-tracing-resolvers {:plaid-link-token plaid-link-token (attach-tracing-resolvers {:plaid-link-token plaid-link-token
:search-plaid-merchants search-merchants
:get-plaid-item-page get-plaid-item-page :get-plaid-item-page get-plaid-item-page
:mutation/link-plaid link-plaid :mutation/link-plaid link-plaid
:mutation/delete-plaid-item delete-plaid-item}))) :mutation/delete-plaid-item delete-plaid-item})))

View File

@@ -550,6 +550,7 @@
:accounts {:type '(list :invoices_expense_accounts)} :accounts {:type '(list :invoices_expense_accounts)}
:payment {:type :payment} :payment {:type :payment}
:expected_deposit {:type :expected_deposit} :expected_deposit {:type :expected_deposit}
:plaid_merchant {:type :plaid_merchant}
:vendor {:type :vendor} :vendor {:type :vendor}
:bank_account {:type :bank_account} :bank_account {:type :bank_account}
:date {:type 'String} :date {:type 'String}

View File

@@ -34,7 +34,7 @@
(map first))) (map first)))
(set (map :db/id (:user/clients id))))))) (set (map :db/id (:user/clients id)))))))
(defn upsert-vendor [context {{:keys [id name hidden terms code print_as primary_contact secondary_contact address default_account_id invoice_reminder_schedule schedule_payment_dom terms_overrides account_overrides] :as in} :vendor} _] (defn upsert-vendor [context {{:keys [id name hidden terms code print_as primary_contact plaid_merchant secondary_contact address default_account_id invoice_reminder_schedule schedule_payment_dom terms_overrides account_overrides] :as in} :vendor} _]
(when (and id (not (can-user-edit-vendor? id (:id context)))) (when (and id (not (can-user-edit-vendor? id (:id context))))
(assert-failure "This vendor is managed by Integreat. Please reach out to ben@integreatconsult.com for your changes.")) (assert-failure "This vendor is managed by Integreat. Please reach out to ben@integreatconsult.com for your changes."))
@@ -125,6 +125,7 @@
:vendor/legal-entity-tin (:legal_entity_tin 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-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/legal-entity-1099-type (enum->keyword (:legal_entity_1099_type in) "legal-entity-1099-type")
:vendor/plaid-merchant plaid_merchant
:vendor/account-overrides account-overrides :vendor/account-overrides account-overrides
:vendor/terms-overrides terms-overrides :vendor/terms-overrides terms-overrides
:vendor/schedule-payment-dom schedule-payment-dom :vendor/schedule-payment-dom schedule-payment-dom

View File

@@ -25,16 +25,18 @@
(defn plaid->transaction [t] (defn plaid->transaction [t]
#:transaction {:description-original (:name t) (cond-> #:transaction {:description-original (:name t)
:raw-id (:transaction_id t) :raw-id (:transaction_id t)
:db/id (random-tempid) :db/id (random-tempid)
:id #_{:clj-kondo/ignore [:unresolved-var]} :id #_{:clj-kondo/ignore [:unresolved-var]}
(di/sha-256 (:transaction_id t)) (di/sha-256 (:transaction_id t))
:amount (if (= "credit" (:type (:account t))) :amount (if (= "credit" (:type (:account t)))
(- (double (:amount t))) (- (double (:amount t)))
(double (:amount t))) (double (:amount t)))
:date (coerce/to-date (atime/parse (:authorized_date t) atime/iso-date)) :date (coerce/to-date (atime/parse (:authorized_date t) atime/iso-date))
:status "POSTED"}) :status "POSTED"}
(:merchant_name t) (assoc :transaction/plaid-merchant {:plaid-merchant/name (:merchant_name t)
:db/id (random-tempid)})))
(defn import-plaid-int [] (defn import-plaid-int []
@@ -53,6 +55,7 @@
:account :account
(accounts-by-id (:account_id transaction)))) (accounts-by-id (:account_id transaction))))
:transaction/bank-account bank-account-id :transaction/bank-account bank-account-id
:transaction/client client-id)))) :transaction/client client-id))))
(t/finish! import-batch) (t/finish! import-batch)
(catch Exception e (catch Exception e

View File

@@ -78,7 +78,7 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::save ::save
[with-user with-is-admin? (forms/triggers-loading ::vendor-form) (forms/in-form ::vendor-form)] [with-user with-is-admin? (forms/triggers-loading ::vendor-form) (forms/in-form ::vendor-form)]
(fn [{:keys [user is-admin?] {{:keys [name hidden print-as terms invoice-reminder-schedule primary-contact automatically-paid-when-due schedule-payment-dom secondary-contact address default-account terms-overrides account-overrides id legal-entity-name legal-entity-tin legal-entity-tin-type legal-entity-first-name legal-entity-last-name legal-entity-middle-name legal-entity-1099-type] :as data} :data} :db} _] (fn [{:keys [user is-admin?] {{:keys [name hidden print-as terms invoice-reminder-schedule plaid-merchant primary-contact automatically-paid-when-due schedule-payment-dom secondary-contact address default-account terms-overrides account-overrides id legal-entity-name legal-entity-tin legal-entity-tin-type legal-entity-first-name legal-entity-last-name legal-entity-middle-name legal-entity-1099-type] :as data} :data} :db} _]
(if (m/validate schema data) (if (m/validate schema data)
(let [query [:upsert-vendor (let [query [:upsert-vendor
{:vendor (cond-> {:id id {:vendor (cond-> {:id id
@@ -114,6 +114,7 @@
:automatically-paid-when-due (mapv :automatically-paid-when-due (mapv
(comp :id :client) (comp :id :client)
automatically-paid-when-due) automatically-paid-when-due)
:plaid-merchant (:id plaid-merchant)
:legal-entity-name legal-entity-name :legal-entity-name legal-entity-name
:legal-entity-first-name legal-entity-first-name :legal-entity-first-name legal-entity-first-name
:legal-entity-middle-name legal-entity-middle-name :legal-entity-middle-name legal-entity-middle-name
@@ -179,8 +180,18 @@
"Print Checks As" "Print Checks As"
[:input.input]] [:input.input]]
(when is-admin? (when is-admin?
[form-builder/raw-field-v2 {:field :hidden} [:<>
[com/checkbox {:label "Hidden"}]]) [form-builder/raw-field-v2 {:field :hidden}
[com/checkbox {:label "Hidden"}]]
[form-builder/field-v2 {:field :plaid-merchant
:required? false}
"Plaid merchant"
[search-backed-typeahead {:search-query (fn [i]
[:search_plaid_merchants
{:query i}
[:name :id]])
:style {:width "19em"}}]]])
[form-builder/section {:title "Terms"} [form-builder/section {:title "Terms"}
[form-builder/field-v2 {:field :terms} [form-builder/field-v2 {:field :terms}

View File

@@ -460,9 +460,9 @@
"Intuit Bank Account" "Intuit Bank Account"
[typeahead-v3 {:entities @(re-frame/subscribe [::subs/intuit-bank-accounts]) [typeahead-v3 {:entities @(re-frame/subscribe [::subs/intuit-bank-accounts])
:entity->text (fn [m] (str (:name m)))}]] :entity->text (fn [m] (str (:name m)))}]]
[form-builder/field-v2 {:field :plaid-accounti} [form-builder/field-v2 {:field :plaid-account}
"Plaid Account" "Plaid Account"
[typeahead-v3 {:entities @(re-frame/subscribe [::plaid-accounts (:id new-client)]) [typeahead-v3 {:entities (mapcat :accounts (:plaid-items new-client ))
:entity->text (fn [m] (str (:name m)))}]]]) :entity->text (fn [m] (str (:name m)))}]]])
(when (#{:credit ":credit"} type ) (when (#{:credit ":credit"} type )
@@ -498,7 +498,7 @@
[form-builder/field-v2 {:field :plaid-account} [form-builder/field-v2 {:field :plaid-account}
"Plaid Account" "Plaid Account"
[typeahead-v3 {:entities @(re-frame/subscribe [::plaid-accounts (:id new-client)]) [typeahead-v3 {:entities (mapcat :accounts (:plaid-items new-client ))
:entity->text (fn [m] (str (:name m)))}]]]) :entity->text (fn [m] (str (:name m)))}]]])
[:div.field [:div.field
[:label.label "Locations"] [:label.label "Locations"]

View File

@@ -22,6 +22,7 @@
[:usage [:client-id :count]] [:usage [:client-id :count]]
[:primary-contact [:name :phone :email :id]] [:primary-contact [:name :phone :email :id]]
[:secondary-contact [:id :name :phone :email]] [:secondary-contact [:id :name :phone :email]]
[:plaid-merchant [:id :name]]
:print-as :invoice-reminder-schedule :code :print-as :invoice-reminder-schedule :code
:legal-entity-name :legal-entity-name
:legal-entity-first-name :legal-entity-middle-name :legal-entity-last-name :legal-entity-first-name :legal-entity-middle-name :legal-entity-last-name

View File

@@ -7,6 +7,7 @@
[:schedule-payment-dom [[:client [:id :name]] :id :dom]] [:schedule-payment-dom [[:client [:id :name]] :id :dom]]
[:usage [:client-id :count]] [:usage [:client-id :count]]
[:primary-contact [:name :phone :email :id]] [:primary-contact [:name :phone :email :id]]
[:plaid-merchant [:name :id]]
[:secondary-contact [:id :name :phone :email]] [:secondary-contact [:id :name :phone :email]]
:print-as :invoice-reminder-schedule :code :print-as :invoice-reminder-schedule :code
:legal-entity-name :legal-entity-name

View File

@@ -13,6 +13,7 @@
[:accounts [:id :amount :location [:account [:name :id :location :numeric-code]]]] [:accounts [:id :amount :location [:account [:name :id :location :numeric-code]]]]
:date :date
[:yodlee_merchant [:name :yodlee-id :id]] [:yodlee_merchant [:name :yodlee-id :id]]
[:plaid_merchant [:name :id]]
:post_date :post_date
[:expected-deposit [:id :date]] [:expected-deposit [:id :date]]
[:forecast-match [:id :identifier]] [:forecast-match [:id :identifier]]

View File

@@ -60,6 +60,7 @@
(-> which (-> which
(select-keys [:vendor :amount :payment :client :description-original (select-keys [:vendor :amount :payment :client :description-original
:yodlee-merchant :id :potential-payment-matches :yodlee-merchant :id :potential-payment-matches
:plaid-merchant
:forecast-match :date :forecast-match :date
:location :accounts :approval-status :location :accounts :approval-status
:matched-rule]) :matched-rule])
@@ -359,6 +360,10 @@
"Description" "Description"
[:input.input {:type "text" [:input.input {:type "text"
:disabled "disabled"}]] :disabled "disabled"}]]
[form-builder/field-v2 {:field [:plaid-merchant :name]}
"Merchant"
[:input.input {:type "text"
:disabled "disabled"}]]
[form-builder/field-v2 {:field [:date]} [form-builder/field-v2 {:field [:date]}
"Date" "Date"

View File

@@ -1,3 +1,3 @@
#/bin/bash #!/bin/bash
sudo docker run --rm -ti -v ~/dev/integreat/data/solr:/var/solr --network=bridge -p 8983:8983 solr sudo docker run --rm -ti -v ~/dev/integreat/data/solr:/var/solr --network=bridge -p 8983:8983 solr