From 3597a5004ddb1fa8fef06cb83b31637f618c6593 Mon Sep 17 00:00:00 2001 From: Bryce Date: Sun, 20 Aug 2023 15:40:22 -0700 Subject: [PATCH] adds plaid reauth --- src/clj/auto_ap/plaid/core.clj | 16 +++++++++++ src/clj/auto_ap/ssr/company/plaid.clj | 39 ++++++++++++++++++++------- src/clj/auto_ap/ssr/core.clj | 1 + src/cljc/auto_ap/ssr_routes.cljc | 1 + 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/clj/auto_ap/plaid/core.clj b/src/clj/auto_ap/plaid/core.clj index 55ee7ce4..f9a9da29 100644 --- a/src/clj/auto_ap/plaid/core.clj +++ b/src/clj/auto_ap/plaid/core.clj @@ -26,6 +26,22 @@ :body :link_token)) +(defn get-relink-token [client-code access-token] + (-> (client/post (str base-url "/link/token/create") + {:as :json + :headers {"Content-Type" "application/json"} + :body (json/write-str {"client_id" client-id + "secret" secret-key + "client_name" "Integreat Consulting" + "access_token" access-token + "country_codes" ["US"] + "language" "en" + "user" {"client_user_id" client-code}})}) + :body + :link_token)) + + + (defn exchange-public-token [public-token _] (-> (client/post (str base-url "/item/public_token/exchange") {:as :json diff --git a/src/clj/auto_ap/ssr/company/plaid.clj b/src/clj/auto_ap/ssr/company/plaid.clj index c0d8ae3b..61d5b48f 100644 --- a/src/clj/auto_ap/ssr/company/plaid.clj +++ b/src/clj/auto_ap/ssr/company/plaid.clj @@ -16,6 +16,7 @@ [auto-ap.ssr.components :as com] [auto-ap.ssr.grid-page-helper :as helper] [auto-ap.ssr.svg :as svg] + [auto-ap.ssr.utils :refer [html-response]] [auto-ap.time :as atime] [bidi.bidi :as bidi] [clj-time.coerce :as coerce] @@ -25,6 +26,7 @@ (def default-read '[:db/id :plaid-item/external-id + :plaid-item/access-token :plaid-item/last-updated :plaid-item/status {:plaid-item/accounts [:db/id @@ -116,6 +118,25 @@ {:headers {"Hx-redirect" (bidi/path-for ssr-routes/only-routes :company-plaid)}})) +(defn relink [{{:strs [plaid-item-id]} :query-params :keys [identity]}] + + (let [pi (dc/pull (dc/db conn) + [:plaid-item/access-token {:plaid-item/client [:client/code]}] + (Long/parseLong plaid-item-id))] + (assert-can-see-client identity (pull-attr (dc/db conn) :db/id [:client/code (-> pi :plaid-item/client + :client/code)])) + (html-response + [:div + [:script (hiccup/raw (plaid-link-script (p/get-relink-token (-> pi :plaid-item/client + :client/code) + (-> pi :plaid-item/access-token))))] + [:script (hiccup/raw "window.plaid.open()")] + #_(com/button {:color :primary + :id "link-account" + :onClick "window.plaid.open()"} + (com/button-icon {} svg/refresh) + "Start relink")]))) + (def grid-page {:id "plaid-table" :nav (com/company-aside-nav) @@ -146,12 +167,12 @@ (com/button-icon {} svg/refresh) "Link new account")]])) :row-buttons (fn [user e] - [] - #_[(when (is-admin? user) - (com/icon-button {:hx-put (bidi/path-for ssr-routes/only-routes - :company-yodlee-provider-account-refresh) - :hx-target "closest tr"} - svg/refresh))]) + [[:div (com/button {:hx-put (str (bidi/path-for ssr-routes/only-routes + :company-plaid-relink) + "?plaid-item-id=" (:db/id e)) + :color :primary + :hx-target "closest div"} + "Reauthenticate")]]) :headers [{:key "plaid-item" :name "Plaid Item" :sort-key "id" @@ -159,11 +180,11 @@ {:key "status" :name "Status" :sort-key "status" - :render #(when-let [status (:plaid-item/status %)] - [:div [:div (com/pill {:color :primary } + :render #(when-let [status (:plaid-item/status %)] + [:div [:div (com/pill {:color :primary} status)] [:div (atime/unparse-local (coerce/to-date-time (:plaid-item/last-updated %)) atime/normal-date)]])} - + {:key "accounts" :name "Accounts" :show-starting "md" diff --git a/src/clj/auto_ap/ssr/core.clj b/src/clj/auto_ap/ssr/core.clj index 54371fac..8dc4bd5d 100644 --- a/src/clj/auto_ap/ssr/core.clj +++ b/src/clj/auto_ap/ssr/core.clj @@ -33,6 +33,7 @@ :company-plaid (wrap-client-redirect-unauthenticated (wrap-secure company-plaid/page)) :company-plaid-table (wrap-client-redirect-unauthenticated (wrap-secure company-plaid/table)) :company-plaid-link (wrap-client-redirect-unauthenticated (wrap-secure company-plaid/link)) + :company-plaid-relink (wrap-client-redirect-unauthenticated (wrap-secure company-plaid/relink)) :company-yodlee (wrap-client-redirect-unauthenticated (wrap-secure company-yodlee/page)) :company-yodlee-table (wrap-client-redirect-unauthenticated (wrap-secure company-yodlee/table)) :company-yodlee-fastlink-dialog (wrap-client-redirect-unauthenticated (wrap-secure company-yodlee/fastlink-dialog)) diff --git a/src/cljc/auto_ap/ssr_routes.cljc b/src/cljc/auto_ap/ssr_routes.cljc index 5a3e7565..cd0d1949 100644 --- a/src/cljc/auto_ap/ssr_routes.cljc +++ b/src/cljc/auto_ap/ssr_routes.cljc @@ -38,6 +38,7 @@ "/plaid" {"" {:get :company-plaid} "/table" {:get :company-plaid-table} "/link" {:post :company-plaid-link} + "/relink" {:put :company-plaid-relink} #_#_"/fastlink" {:get :company-yodlee-fastlink-dialog} #_#_"/refresh" {:put :company-yodlee-provider-account-refresh}}}})