Yodlee page in ssr.

This commit is contained in:
2023-05-13 22:51:03 -07:00
parent 8dca622947
commit b9ae20125b
19 changed files with 417 additions and 160 deletions

View File

@@ -0,0 +1,168 @@
(ns auto-ap.ssr.company.yodlee
(:require
[auto-ap.datomic :refer [conn]]
[auto-ap.datomic.yodlee2 :as yodlee2]
[auto-ap.graphql.utils :refer [is-admin?]]
[auto-ap.ssr-routes :as ssr-routes]
[auto-ap.ssr.components :as com]
[auto-ap.ssr.svg :as svg]
[auto-ap.ssr.ui :refer [base-page]]
[auto-ap.ssr.utils :refer [html-response]]
[auto-ap.time :as atime]
[auto-ap.yodlee.core2 :as yodlee]
[bidi.bidi :as bidi]
[config.core :refer [env]]
[datomic.api :as dc]
[hiccup2.core :as hiccup]))
(defn row* [{:keys [flash? yodlee-provider-account identity delete-after-settle?]}]
(com/data-grid-row
{:class (when flash?
"live-added")
"_" (hiccup/raw (when delete-after-settle?" on htmx:afterSettle wait 400ms then remove me"))}
(com/data-grid-cell
{}
(:yodlee-provider-account/id yodlee-provider-account))
(com/data-grid-cell
{}
(when-let [status (:yodlee-provider-account/status yodlee-provider-account)]
(com/pill {:color (if (not= status "SUCCESS")
:yellow
:primary) }
status)))
(com/data-grid-cell
{}
(when-let [status (:yodlee-provider-account/detailed-status yodlee-provider-account)]
status)
)
(com/data-grid-cell
{}
(atime/unparse-local (:yodlee-provider-account/last-updated yodlee-provider-account)
atime/normal-date))
(com/data-grid-cell
{}
[:ul
(for [a (:yodlee-provider-account/accounts yodlee-provider-account)]
[:li (:yodlee-account/name a) " - " (:yodlee-account/number a) #_[:div.tag (->$ (:available-balance a))]])])
(com/data-grid-right-stack-cell
{}
(when (is-admin? identity)
[:form
[:input {:type :hidden :name "id" :value (:db/id yodlee-provider-account)}]
(com/icon-button {:hx-put (bidi/path-for ssr-routes/only-routes
:company-yodlee-provider-account-refresh)
:hx-target "closest tr"}
svg/refresh)])
#_(when (is-admin? identity)
[:form
[:input {:type :hidden :name "id" :value (:db/id report)}]
(com/icon-button {:hx-delete (str (bidi/path-for ssr-routes/only-routes
:company-reports-delete
:request-method :delete))
:hx-target "closest tr"}
svg/trash)]))))
(defn table* [{:keys [identity start per-page client flash-id]}]
(let [start (or start 0)
per-page (or per-page 30)
[yodlee-provider-accounts total] (yodlee2/get-graphql {:id identity
:start start
:per-page per-page
:client-id (:db/id client)
:sort nil})]
[:div
(com/data-grid-card {:id "yodlee-table"
:title "Yodlee Accounts"
:entity-name "Yodlee accounts"
:route :company-yodlee-table
:start start
:per-page per-page
:total total
:action-buttons [(com/button {:color :primary
:on-click "openFastlink()"
:hx-get (bidi/path-for ssr-routes/only-routes
:company-yodlee-fastlink-dialog)
:hx-target "#modal-holder"}
(com/button-icon {} svg/refresh)
"Link new account")]
:rows (for [yodlee-provider-account yodlee-provider-accounts]
(row* {:yodlee-provider-account yodlee-provider-account
:flash? (= flash-id
(:db/id yodlee-provider-account))
:identity identity}))
:headers [(com/data-grid-header {} "Provider Account")
(com/data-grid-header {} "Status")
(com/data-grid-header {} "Detailed Status")
(com/data-grid-header {} "Last Updated")
(com/data-grid-header {:class "hidden md:table-cell"} "Accounts")
(com/data-grid-header {})]})]))
(def default-read '[:db/id
:yodlee-provider-account/last-updated
:yodlee-provider-account/status
:yodlee-provider-account/id
:yodlee-provider-account/detailed-status
{:yodlee-provider-account/accounts [:yodlee-account/name :yodlee-account/number]
:yodlee-provider-account/client [:client/code]}])
(defn refresh-provider-account [{:keys [form-params identity]}]
(let [provider-account (dc/pull (dc/db conn) default-read (some-> (get form-params "id") not-empty Long/parseLong))]
(yodlee/refresh-provider-account (:client/code (:yodlee-provider-account/client provider-account))
(:yodlee-provider-account/id provider-account))
(html-response
(row* {:yodlee-provider-account provider-account
:flash? true
:identity identity}))))
(defn fastlink-dialog [{:keys [session]}]
(html-response
(com/modal
{}
(com/modal-card
{}
[:div.flex [:div.p-2 "Yodlee Fastlink"] ]
[:div
[:div#fa-spot]
[:script {:lang "text/javascript"}
(hiccup/raw
(format "
fastlink.open({fastLinkURL: '%s',
accessToken: '%s',
params: {'configName': 'Aggregation'}},
'fa-spot');
" (:yodlee2-fastlink env) (yodlee/get-access-token (:client/code (:client session)))))]
]
[:div]))))
(defn table [{:keys [query-params hx-query-params identity session] :as request}]
(html-response (table* {:client (:client (:session request))
:start (some-> (or (get query-params "start") (get hx-query-params "start")) not-empty (Long/parseLong ))
:per-page (some-> (or (get query-params "per-page") (get hx-query-params "per-page")) not-empty (Long/parseLong ))
:identity identity
:session session})
:headers {"hx-push-url" (str "?start=" (get (:query-params request) "start"))}))
(defn page [{:keys [identity matched-route query-params :hx-query-params session] :as request}]
(base-page
request
(com/page {:nav (com/company-aside-nav)
:active-client (:client (:session request))
:identity (:identity request)}
(com/breadcrumbs {}
[:a {:href (bidi/path-for ssr-routes/only-routes
:company)}
"My Company"]
[:a {:href (bidi/path-for ssr-routes/only-routes
:company-yodlee)}
"Yodlee"]
)
(table* {:client (:client session)
:start (some-> (or (get query-params "start") (get hx-query-params "start")) not-empty (Long/parseLong ))
:per-page (some-> (or (get query-params "per-page") (get hx-query-params "per-page")) not-empty (Long/parseLong ))
:identity identity
:session session}))
nil))