Adds reports page.

This commit is contained in:
2022-03-25 14:59:31 -07:00
parent a6ae06b760
commit 9e5a160d77
12 changed files with 368 additions and 12 deletions

View File

@@ -8,6 +8,7 @@
[auto-ap.datomic.migrate.add-general-ledger :as add-general-ledger]
[auto-ap.datomic.migrate.audit :as audit]
[auto-ap.datomic.migrate.clients :as clients]
[auto-ap.datomic.migrate.reports :as reports]
[auto-ap.datomic.migrate.invoice-converter
:refer [add-import-status-existing-invoices]]
[auto-ap.datomic.migrate.ledger :as ledger]
@@ -497,6 +498,7 @@
clients/norms-map
ledger/norms-map
yodlee2/norms-map
reports/norms-map
plaid/norms-map
audit/norms-map
vendors/norms-map)]

View File

@@ -0,0 +1,27 @@
(ns auto-ap.datomic.migrate.reports)
(def norms-map {::add-reports
{:txes [[{:db/ident :report/client
:db/doc "Which client(s) this is for"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
{:db/ident :report/name
:db/doc "A description for the report"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :report/key
:db/doc "The s3 key for the report"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :report/created
:db/doc "When this report was created"
:db/valueType :db.type/instant
:db/cardinality :db.cardinality/one}
{:db/ident :report/url
:db/doc "Where to download the report"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}]]}})

View File

@@ -0,0 +1,64 @@
(ns auto-ap.datomic.reports
(:require
[auto-ap.datomic
:refer [add-sorter-fields apply-pagination apply-sort-3 conn merge-query]]
[auto-ap.graphql.utils :refer [can-see-client? limited-clients]]
[clj-time.coerce :as c]
[datomic.api :as d]))
(defn raw-graphql-ids [db args]
(let [query (cond-> {:query {:find []
:in ['$ ]
:where []}
:args [db]}
(:client-id args)
(merge-query {:query {:in ['?client-id]
:where ['[?e :report/client ?client-id]]}
:args [(:client-id args)]})
(limited-clients (:id args))
(merge-query {:query {:in ['[?xx ...]]
:where ['[?e :report/client ?xx]]}
:args [(set (map :db/id (limited-clients (:id args))))]})
(:sort args) (add-sorter-fields {"client" ['[?e :report/client ?c]
'[?c :client/name ?sort-client]]
"created" ['[?e :report/created ?sort-created]]
"name" ['[?e :report/name ?sort-name]
]}
args)
true
(merge-query {:query {:find ['?sort-default '?e] :where ['[?e :report/created ?sort-default]]}}))]
(->> query
(d/query)
(apply-sort-3 (update args :sort conj {:sort-key "default-2" :asc true}))
(apply-pagination args))))
(defn graphql-results [ids db args]
(let [results (->> (d/pull-many db '[:db/id :report/client :report/created :report/url :report/name]
ids)
(map #(update % :report/created c/from-date))
(group-by :db/id))]
(->> ids
(map results)
(filter identity)
(map first)
(filter (fn [r]
(every?
#(can-see-client? (:id args) %)
(map :db/id (:report/client r))))))))
(defn get-graphql [args]
(let [db (d/db conn)
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
[(->> (graphql-results ids-to-retrieve db args))
matching-count]))

View File

@@ -10,6 +10,7 @@
[auto-ap.graphql.expected-deposit :as gq-expected-deposit]
[auto-ap.graphql.import-batch :as gq-import-batches]
[auto-ap.graphql.intuit-bank-accounts :as gq-intuit-bank-accounts]
[auto-ap.graphql.reports :as gq-reports]
[auto-ap.graphql.invoices :as gq-invoices]
[auto-ap.graphql.ledger :as gq-ledger]
[auto-ap.graphql.requests :as gq-requests]
@@ -789,6 +790,7 @@
:get-vendor gq-vendors/get-graphql})
gq-checks/attach
gq-ledger/attach
gq-reports/attach
gq-plaid/attach
gq-import-batches/attach
gq-transactions/attach

View File

@@ -0,0 +1,81 @@
(ns auto-ap.graphql.reports
(:require
[amazonica.aws.s3 :as s3]
[auto-ap.datomic :refer [conn]]
[auto-ap.datomic.reports :as r]
[auto-ap.graphql.utils :refer [<-graphql assert-admin result->page]]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[config.core :refer [env]]
[datomic.api :as d]))
(defn get-report-page [context args _]
(let [args (assoc args :id (:id context))
[reports reports-count] (r/get-graphql (assoc (<-graphql (:filters args))
:id (:id context)))]
(result->page reports reports-count :reports (:filters args)))
)
(defn delete-report [context args _]
(assert-admin (:id context))
(let [[id-to-delete key] (first (d/q '[:find ?i ?k
:in $ ?i
:where [?i :report/key ?k]]
(d/db conn)
(:id args)))]
(when id-to-delete
(s3/delete-object :bucket-name (:data-bucket env)
:key key)
@(d/transact conn [[:db/retractEntity id-to-delete]]))
{:message (format "deleted %s successfully" key)}))
(def objects
{:report
{:fields {:url {:type 'String}
:name {:type 'String}
:created {:type :iso_date}
:id {:type :id}}}
:report_page
{:fields {:reports {:type '(list :report)}
:count {:type 'Int}
:total {:type 'Int}
:start {:type 'Int}
:end {:type 'Int}}}})
(def queries
{:report_page {:type :report_page
:args {:filters {:type :report_filters}}
:resolve :get-report-page}})
(def mutations
{:delete_report {:type :message
:args {:id {:type :id}}
:resolve :mutation/delete-report}})
(def input-objects
{:report_filters
{:fields {:client_id {:type :id}
:start {:type 'Int}
:per_page {:type 'Int}
:sort {:type '(list :sort_item)}}}})
(def enums
{})
(def resolvers
{:get-report-page get-report-page
:mutation/delete-report delete-report})
(defn attach [schema]
(->
(merge-with merge schema
{:objects objects
:queries queries
:mutations mutations
:input-objects input-objects
:enums enums})
(attach-resolvers resolvers)))

View File

@@ -9,7 +9,8 @@
[clojure.java.io :as io]
[clojure.string :as str]
[config.core :refer [env]]
[datomic.api :as d])
[datomic.api :as d]
[clojure.tools.logging :as log])
(:import
(java.io ByteArrayOutputStream)
(java.text DecimalFormat)
@@ -498,12 +499,32 @@
output-stream)
(.toByteArray output-stream)))
(defn args->name [args]
(let [min-date (atime/unparse-local
(->> args :periods (map :start) first)
atime/iso-date)
max-date (atime/unparse-local
(->> args :periods (map :end) last)
atime/iso-date)
names (str/replace (->> args :client_ids (d/pull-many (d/db conn) [:client/name]) (map :client/name) (str/join "-")) #" " "_" )]
(format "Profit-and-loss-%s-to-%s-for-%s" min-date max-date names)))
(defn print-pnl [args data]
(let [uuid (str (UUID/randomUUID))
pdf-data (make-pnl args data)]
pdf-data (make-pnl args data)
name (args->name args)
key (str "reports/pnl/" uuid "/ " name ".pdf")
url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/" key)]
(s3/put-object :bucket-name (:data-bucket env)
:key (str "reports/pnl/" uuid ".pdf")
:key key
:input-stream (io/make-input-stream pdf-data {})
:metadata {:content-length (count pdf-data)
:content-type "application/pdf"})
(str "https://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/reports/pnl/" uuid ".pdf")))
@(d/transact conn
[{:report/name name
:report/client (:client_ids args)
:report/key key
:report/url url
:report/created (java.util.Date.)}])
url
))