adds glimpse for power users
This commit is contained in:
@@ -1,33 +1,32 @@
|
||||
|
||||
(ns auto-ap.ssr.invoice.glimpse
|
||||
(:require
|
||||
[amazonica.aws.s3 :as s3]
|
||||
[amazonica.aws.textract :as textract]
|
||||
[auto-ap.datomic :refer [conn pull-attr pull-id]]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.logging :as alog]
|
||||
[auto-ap.solr :as solr]
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[auto-ap.ssr.components :as com]
|
||||
[auto-ap.ssr.ui :refer [base-page]]
|
||||
[auto-ap.ssr.utils :refer [html-response path->name]]
|
||||
[auto-ap.time :as atime]
|
||||
[bidi.bidi :as bidi]
|
||||
[cemerick.url :as url]
|
||||
[clj-time.coerce :as coerce]
|
||||
[cheshire.core :as cheshire]
|
||||
[clojure.java.io :as io]
|
||||
[clojure.string :as str]
|
||||
[com.brunobonacci.mulog :as mu]
|
||||
[config.core :refer [env]]
|
||||
[datomic.api :as dc]
|
||||
[hiccup2.core :as hiccup]
|
||||
[iol-ion.tx :refer [random-tempid]]
|
||||
[auto-ap.client-routes :as client-routes]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[clj-time.core :as time])
|
||||
(:import
|
||||
(java.util UUID)))
|
||||
(:require [amazonica.aws.s3 :as s3]
|
||||
[amazonica.aws.textract :as textract]
|
||||
[auto-ap.client-routes :as client-routes]
|
||||
[auto-ap.datomic :refer [conn pull-attr]]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.datomic.vendors :as d-vendors]
|
||||
[auto-ap.graphql.utils :refer [extract-client-ids]]
|
||||
[auto-ap.logging :as alog]
|
||||
[auto-ap.solr :as solr]
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[auto-ap.ssr.components :as com]
|
||||
[auto-ap.ssr.ui :refer [base-page]]
|
||||
[auto-ap.ssr.utils :refer [html-response path->name]]
|
||||
[auto-ap.time :as atime]
|
||||
[bidi.bidi :as bidi]
|
||||
[cemerick.url :as url]
|
||||
[cheshire.core :as cheshire]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as time]
|
||||
[clojure.java.io :as io]
|
||||
[clojure.string :as str]
|
||||
[com.brunobonacci.mulog :as mu]
|
||||
[config.core :refer [env]]
|
||||
[datomic.api :as dc]
|
||||
[hiccup2.core :as hiccup]
|
||||
[iol-ion.tx :refer [random-tempid]])
|
||||
(:import (java.util UUID)))
|
||||
|
||||
(def bucket-name (:data-bucket env))
|
||||
|
||||
@@ -63,8 +62,12 @@
|
||||
[[] #{}]
|
||||
xs)))
|
||||
|
||||
(defn textract->textract-invoice [id tx]
|
||||
(defn textract->textract-invoice [request id tx]
|
||||
(let [lookup (lookup tx)
|
||||
valid-client-ids (extract-client-ids (:clients request)
|
||||
(:client-id request)
|
||||
(when (:client-code request)
|
||||
[:client/code (:client-code request)]))
|
||||
total-options (->> (stack-rank #{"AMOUNT_DUE"} lookup)
|
||||
(map (fn [t]
|
||||
[t (some->> t
|
||||
@@ -87,8 +90,16 @@
|
||||
[t (->> (solr/query solr/impl "clients" {"query" (format "name:(%s) ", (clean-customer t)) "fields" "score, *"})
|
||||
#_(filter (fn [d] (> (:score d) 4.0)))
|
||||
(map (comp #(Long/parseLong %) :id))
|
||||
first)]))))
|
||||
first)]))
|
||||
(filter (fn [[t id]]
|
||||
(valid-client-ids id)))))
|
||||
deduplicate)
|
||||
customer-identifier-options (if (seq customer-identifier-options)
|
||||
customer-identifier-options
|
||||
(->> valid-client-ids
|
||||
(take 10)
|
||||
(map (fn [c]
|
||||
[(pull-attr (dc/db conn) :client/name c) c]))))
|
||||
vendor-name-options (->> (stack-rank #{"VENDOR_NAME"} lookup)
|
||||
(mapcat (fn [t]
|
||||
(for [m (->> (solr/query solr/impl "vendors" {"query" (format "name:(%s) ", t) "fields" "score, *"})
|
||||
@@ -161,13 +172,13 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
(update :textract-invoice/vendor-name vendor-name-tuple->vendor-tuple)
|
||||
(update :textract-invoice/vendor-name-options #(map vendor-name-tuple->vendor-tuple %) )))
|
||||
|
||||
(defn refresh-job [id]
|
||||
(defn refresh-job [request id]
|
||||
(let [{:keys [:db/id :textract-invoice/job-id :textract-invoice/textract-status]} (get-job id)]
|
||||
(when (and job-id (= "IN_PROGRESS" textract-status))
|
||||
(let [result (textract/get-expense-analysis {:job-id job-id})
|
||||
new-status (:job-status result)]
|
||||
(cond (= "SUCCEEDED" new-status)
|
||||
@(dc/transact conn [[:upsert-entity (textract->textract-invoice id result)]])
|
||||
@(dc/transact conn [[:upsert-entity (textract->textract-invoice request id result)]])
|
||||
:else
|
||||
@(dc/transact conn [{:db/id id :textract-invoice/textract-status new-status}]))))
|
||||
(get-job id)))
|
||||
@@ -198,6 +209,7 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
(com/field {:label "Client"}
|
||||
(com/text-input {:name (path->name [:invoice/client])
|
||||
:value (-> textract-invoice :textract-invoice/customer-identifier second second)
|
||||
:class "w-96"
|
||||
:placeholder "Client"
|
||||
:disabled true
|
||||
:autofocus true}))]
|
||||
@@ -213,6 +225,7 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
[:div.col-span-6
|
||||
(com/field {:label "Vendor"}
|
||||
(com/text-input {:name (path->name [:invoice/vendor])
|
||||
:class "w-96"
|
||||
:value (-> textract-invoice :textract-invoice/vendor-name second second)
|
||||
:disabled true
|
||||
:placeholder "Vendor"}))]
|
||||
@@ -270,8 +283,8 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
(str invoice-number))})]
|
||||
(com/button {:color :primary} "Save")])
|
||||
|
||||
(defn job-progress* [id]
|
||||
(let [textract-invoice (refresh-job id)]
|
||||
(defn job-progress* [request id]
|
||||
(let [textract-invoice (refresh-job request id)]
|
||||
(cond
|
||||
(= "IN_PROGRESS" (:textract-invoice/textract-status textract-invoice))
|
||||
[:div.bg-blue-100.border-2.border-dashed.rounded-lg.border-blue-300.p-4.max-w-md.w-md.text-center.cursor-pointer
|
||||
@@ -290,12 +303,12 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
[:div {:style {:width "805"}}
|
||||
(com/card {}
|
||||
[:iframe.p-4 {:src (:textract-invoice/pdf-url textract-invoice) :width 791 :height 700}])]
|
||||
[:div {:class "basis-1/4"}
|
||||
[:div {:class "basis-1/2"}
|
||||
(com/card {}
|
||||
[:div.p-4
|
||||
(textract->invoice-form* textract-invoice)])]]])))
|
||||
|
||||
(defn page* [id]
|
||||
(defn page* [request id]
|
||||
[:div#invoice-glimpse-content.mt-4
|
||||
(com/card {}
|
||||
[:div.px-4.py-3.space-y-4.flex.flex-col
|
||||
@@ -307,7 +320,7 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
[:p.text-sm.italic "Import your invoices with the power of AI. Please only use PDFs with a single invoice in them."]
|
||||
|
||||
(when id
|
||||
(job-progress* id))
|
||||
(job-progress* request id))
|
||||
(when-not id
|
||||
(upload-form*))])])
|
||||
|
||||
@@ -402,7 +415,7 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
_ (when new-invoice-id @(dc/transact conn [{:db/id (:db/id current-job)
|
||||
:textract-invoice/invoice new-invoice-id}]))]
|
||||
(if new-invoice-id
|
||||
(html-response (page* nil)
|
||||
(html-response (page* request nil)
|
||||
:headers {"hx-push-url" (bidi/path-for ssr-routes/only-routes :invoice-glimpse)
|
||||
"hx-retarget" "#invoice-glimpse-content"
|
||||
"hx-trigger" (cheshire/generate-string {"notification" (str (hiccup/html [:div "Successfully created "
|
||||
@@ -420,10 +433,11 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
:method request-method)
|
||||
(base-page
|
||||
request
|
||||
(com/page {:nav com/admin-aside-nav
|
||||
(com/page {:nav com/main-aside-nav
|
||||
:client-selection (:client-selection request)
|
||||
:client (:client request)
|
||||
:clients (:clients request)
|
||||
:request request
|
||||
:identity (:identity request)
|
||||
:app-params {:hx-get (bidi/path-for ssr-routes/only-routes
|
||||
:invoice-glimpse)
|
||||
@@ -437,7 +451,7 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
[:a {:href (bidi/path-for ssr-routes/only-routes
|
||||
:invoice-glimpse)}
|
||||
"Glimpse"])
|
||||
(page* (some-> request
|
||||
(page* request (some-> request
|
||||
:route-params
|
||||
:textract-invoice-id
|
||||
Long/parseLong)))
|
||||
@@ -446,7 +460,7 @@ invoice_dropzone = new Dropzone(\"#invoice\", {
|
||||
|
||||
(defn textract-invoice [request]
|
||||
(if (get-in request [:headers "hx-request"])
|
||||
(html-response (job-progress* (some-> request
|
||||
(html-response (job-progress* request (some-> request
|
||||
:route-params
|
||||
:textract-invoice-id
|
||||
Long/parseLong)))
|
||||
|
||||
Reference in New Issue
Block a user