migrates accounts
This commit is contained in:
292
src/clj/auto_ap/ssr/admin/accounts.clj
Normal file
292
src/clj/auto_ap/ssr/admin/accounts.clj
Normal file
@@ -0,0 +1,292 @@
|
||||
(ns auto-ap.ssr.admin.accounts
|
||||
(:require
|
||||
[auto-ap.datomic
|
||||
:refer [add-sorter-fields
|
||||
apply-pagination
|
||||
apply-sort-3
|
||||
conn
|
||||
merge-query
|
||||
pull-many
|
||||
query2]]
|
||||
[auto-ap.query-params :as query-params]
|
||||
[auto-ap.routes.utils
|
||||
:refer [wrap-admin wrap-client-redirect-unauthenticated]]
|
||||
[auto-ap.ssr-routes :as ssr-routes]
|
||||
[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 [entity-id
|
||||
forced-vector
|
||||
html-response
|
||||
map->db-id-decoder
|
||||
ref->enum-schema
|
||||
ref->select-options
|
||||
temp-id
|
||||
wrap-schema-decode]]
|
||||
[bidi.bidi :as bidi]
|
||||
[clojure.string :as str]
|
||||
[datomic.api :as dc]
|
||||
[hiccup2.core :as hiccup]
|
||||
[malli.core :as mc]
|
||||
[ring.middleware.nested-params :refer [wrap-nested-params]]))
|
||||
|
||||
(defn filters [request]
|
||||
[:form {"hx-trigger" "change delay:500ms, keyup changed from:.hot-filter delay:1000ms"
|
||||
"hx-get" (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-table)
|
||||
"hx-target" "#account-table"
|
||||
"hx-indicator" "#account-table"}
|
||||
|
||||
[:fieldset.space-y-6
|
||||
(com/field {:label "Name"}
|
||||
(com/text-input {:name "name"
|
||||
:id "name"
|
||||
:class "hot-filter"
|
||||
:value (:name (:parsed-query-params request))
|
||||
:placeholder "Cash"
|
||||
:size :small}))
|
||||
|
||||
(com/field {:label "Code"}
|
||||
(com/text-input {:name "code"
|
||||
:id "code"
|
||||
:class "hot-filter"
|
||||
:value (:code (:parsed-query-params request))
|
||||
:placeholder "11101"
|
||||
:size :small}))]])
|
||||
|
||||
(def default-read '[:db/id
|
||||
:account/code
|
||||
:account/name
|
||||
:account/numeric-code
|
||||
:account/location
|
||||
{[:account/type :xform iol-ion.query/ident] [:db/ident]
|
||||
[:account/invoice-allowance :xform iol-ion.query/ident] [:db/ident]
|
||||
[:account/vendor-allowance :xform iol-ion.query/ident] [:db/ident]
|
||||
[:account/applicability :xform iol-ion.query/ident] [:db/ident]
|
||||
:account/client-overrides [{:account-client-override/client [:client/name :db/id]}
|
||||
:account-client-override/name
|
||||
:db/id]}])
|
||||
|
||||
(defn fetch-ids [db request]
|
||||
(let [query-params (:parsed-query-params request)
|
||||
query (cond-> {:query {:find []
|
||||
:in '[$ ]
|
||||
:where '[]}
|
||||
:args [db ]}
|
||||
(:sort query-params) (add-sorter-fields {"name" ['[?e :account/name ?n]
|
||||
'[(clojure.string/upper-case ?n) ?sort-name]]
|
||||
"code" ['[(get-else $ ?e :account/numeric-code 0) ?sort-code]]
|
||||
|
||||
"type" ['[?e :account/type ?t]
|
||||
'[?t :db/ident ?ti]
|
||||
'[(name ?ti) ?sort-type]]}
|
||||
query-params)
|
||||
(some->> query-params :name not-empty)
|
||||
(merge-query {:query {:find []
|
||||
:in ['?ns]
|
||||
:where ['[?e :account/name ?an]
|
||||
'[(clojure.string/upper-case ?an) ?upper-an]
|
||||
'[(clojure.string/includes? ?upper-an ?ns)]]}
|
||||
:args [(str/upper-case (:name query-params))]})
|
||||
|
||||
(some->> query-params :code)
|
||||
(merge-query {:query {:find []
|
||||
:in ['?nc]
|
||||
:where ['[?e :account/numeric-code ?nc]
|
||||
]}
|
||||
:args [(:code query-params)]})
|
||||
|
||||
true
|
||||
(merge-query {:query {:find ['?sort-default '?e]
|
||||
:where ['[?e :account/code ?un]
|
||||
'[(clojure.string/upper-case ?un) ?sort-default]]}}))]
|
||||
|
||||
(cond->> (query2 query)
|
||||
true (apply-sort-3 query-params)
|
||||
true (apply-pagination query-params))))
|
||||
|
||||
(defn hydrate-results [ids db _]
|
||||
(let [results (->> (pull-many db default-read ids)
|
||||
(group-by :db/id))
|
||||
refunds (->> ids
|
||||
(map results)
|
||||
(map first))]
|
||||
refunds))
|
||||
|
||||
(defn fetch-page [request]
|
||||
(let [db (dc/db conn)
|
||||
{ids-to-retrieve :ids matching-count :count} (fetch-ids db request)]
|
||||
|
||||
[(->> (hydrate-results ids-to-retrieve db request))
|
||||
matching-count]))
|
||||
|
||||
(def grid-page
|
||||
(helper/build {:id "account-table"
|
||||
:nav (com/admin-aside-nav)
|
||||
:page-specific-nav filters
|
||||
:fetch-page fetch-page
|
||||
:parse-query-params (comp
|
||||
(query-params/parse-key :code query-params/parse-long)
|
||||
(helper/default-parse-query-params grid-page))
|
||||
:row-buttons (fn [request entity]
|
||||
[(com/icon-button {:hx-get (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-edit-dialog
|
||||
:db/id (doto (:db/id entity) println)))
|
||||
:hx-target "#modal-holder"
|
||||
:hx-swap "outerHTML"}
|
||||
svg/pencil)])
|
||||
:breadcrumbs [[:a {:href (bidi/path-for ssr-routes/only-routes
|
||||
:admin)}
|
||||
"Admin"]
|
||||
|
||||
[:a {:href (bidi/path-for ssr-routes/only-routes
|
||||
:admin-accounts)}
|
||||
"Accounts"]]
|
||||
:title "Accounts"
|
||||
:entity-name "Account"
|
||||
:route :admin-account-table
|
||||
:headers [{:key "code"
|
||||
:name "Code"
|
||||
:sort-key "code"
|
||||
:render :account/numeric-code}
|
||||
|
||||
{:key "name"
|
||||
:name "Name"
|
||||
:sort-key "name"
|
||||
:render :account/name}
|
||||
{:key "type"
|
||||
:name "Type"
|
||||
:sort-key "type"
|
||||
:render #(some->> % :account/type name (com/pill {:color :primary}))}
|
||||
{:key "location"
|
||||
:name "Location"
|
||||
:sort-key "location"
|
||||
:render :account/location}]}))
|
||||
|
||||
(def row* (partial helper/row* grid-page))
|
||||
(def table* (partial helper/table* grid-page))
|
||||
|
||||
(defn account-edit-save [{:keys [params route-params] :as request}]
|
||||
(let [_ @(dc/transact conn [[:upsert-entity (-> params (assoc :db/id (:db/id route-params)) (dissoc :id))]])
|
||||
new-account (some-> route-params :db/id (#(dc/pull (dc/db conn) default-read %)))]
|
||||
|
||||
(html-response
|
||||
(row* identity new-account {:flash? true})
|
||||
:headers {"hx-trigger" "closeModal"
|
||||
"hx-retarget" (format "#account-table tr[data-id=\"%d\"]" (:db/id new-account))})))
|
||||
|
||||
(defn client-override* [override]
|
||||
[:div.flex.gap-2.mb-2.client-override
|
||||
[:div.w-96
|
||||
(com/typeahead {:name (format "account/client-overrides[%s][account-client-override/client]" (:db/id override))
|
||||
:placeholder "Search..."
|
||||
:url (bidi/path-for ssr-routes/only-routes
|
||||
:company-search)
|
||||
:id (str "account-client-override-" (:db/id override))
|
||||
:value [(:db/id (:account-client-override/client override))
|
||||
(:client/name (:account-client-override/client override))]})]
|
||||
[:div.w-96
|
||||
(com/text-input {:name (format "account/client-overrides[%s][account-client-override/name]" (:db/id override))
|
||||
:class "w-full"
|
||||
:value (:account-client-override/name override)})]
|
||||
[:div (com/a-icon-button {"_" (hiccup/raw "on click halt the event then transition the closest <.client-override />'s opacity to 0 then remove closest <.client-override />") } svg/x)]])
|
||||
|
||||
(defn account-edit-dialog [request]
|
||||
(prn (:route-params request))
|
||||
(let [account (some-> request
|
||||
:route-params
|
||||
:db/id
|
||||
(#(dc/pull (dc/db conn) default-read %)))]
|
||||
(html-response
|
||||
(com/modal
|
||||
{:modal-class "max-w-4xl"}
|
||||
[:form#edit-form {:hx-ext "response-targets"
|
||||
:hx-post (str (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-edit-save
|
||||
:request-method :post
|
||||
:db/id (:db/id account )))
|
||||
:hx-swap "outerHTML swap:300ms"
|
||||
:hx-target-400 "#form-errors .error-content"}
|
||||
[:fieldset {:class "hx-disable"}
|
||||
(com/modal-card
|
||||
{}
|
||||
[:div.flex [:div.p-2 "Account"] [:p.ml-2.rounded.bg-gray-200.p-2.dark:bg-gray-600 (:account/numeric-code account) " - " (:account/name account)]]
|
||||
[:div.space-y-6
|
||||
(com/field {:label "Name"}
|
||||
(com/text-input {:name "account/name"
|
||||
:autofocus true
|
||||
:class "w-32"
|
||||
:value (:account/name account)}))
|
||||
(com/field {:label "Account Type"}
|
||||
(com/select {:name "account/type"
|
||||
:class "w-36"
|
||||
:id "type"
|
||||
:value (some-> account :account/type name)
|
||||
:options (ref->select-options "account-type")}))
|
||||
(com/field {:label "Location"}
|
||||
(com/text-input {:name "account/location"
|
||||
:class "w-16"
|
||||
:value (:account/location account)}))
|
||||
|
||||
(com/field {:label "Invoice Allowance"}
|
||||
(com/select {:name "account/invoice-allowance"
|
||||
:value (name (:account/invoice-allowance account))
|
||||
:class "w-36"
|
||||
:options (ref->select-options "allowance")}))
|
||||
(com/field {:label "Vendor Allowance"}
|
||||
(com/select {:name "account/vendor-allowance"
|
||||
:class "w-36"
|
||||
:value (name (:account/vendor-allowance account))
|
||||
:options (ref->select-options "allowance")}))
|
||||
(com/field {:label "Applicability"}
|
||||
(com/select {:name "account/applicability"
|
||||
:class "w-36"
|
||||
:value (name (:account/applicability account))
|
||||
:options (ref->select-options "account-applicability")}))
|
||||
|
||||
(com/field {:label "Client Overrides" :id "client-overrides"}
|
||||
(for [override (:account/client-overrides account)]
|
||||
(client-override* override)))
|
||||
(com/a-button {:hx-get (bidi/path-for ssr-routes/only-routes
|
||||
:admin-account-client-override-new)
|
||||
:hx-target "#client-overrides"
|
||||
:hx-swap "beforeend"}
|
||||
"New override")
|
||||
[:div#form-errors [:span.error-content]]
|
||||
(com/button {:color :primary :form "edit-form" :type "submit"}
|
||||
"Save")]
|
||||
[:div])]]))))
|
||||
|
||||
(defn new-client-override [request]
|
||||
(html-response
|
||||
(client-override* {:db/id (str (java.util.UUID/randomUUID))})))
|
||||
|
||||
(def key->handler
|
||||
{:admin-accounts (wrap-admin (helper/page-route grid-page))
|
||||
:admin-account-table (wrap-admin (helper/table-route grid-page))
|
||||
:admin-account-client-override-new (-> new-client-override wrap-admin wrap-client-redirect-unauthenticated)
|
||||
:admin-account-edit-save (-> account-edit-save
|
||||
wrap-admin
|
||||
wrap-client-redirect-unauthenticated
|
||||
(wrap-schema-decode
|
||||
:route-schema (mc/schema [:map [:db/id entity-id]])
|
||||
:params-schema (mc/schema
|
||||
[:map
|
||||
[:account/name :string]
|
||||
[:account/location [:maybe :string]]
|
||||
[:account/type (ref->enum-schema "account-type")]
|
||||
[:account/applicability (ref->enum-schema "account-applicability")]
|
||||
[:account/invoice-allowance (ref->enum-schema "allowance")]
|
||||
[:account/vendor-allowance (ref->enum-schema "allowance")]
|
||||
[:account/client-overrides {:decode/json map->db-id-decoder}
|
||||
(forced-vector [:map
|
||||
[:db/id [:or entity-id temp-id]]
|
||||
[:account-client-override/client [:or entity-id :string]]
|
||||
[:account-client-override/name :string]])]]))
|
||||
(wrap-nested-params))
|
||||
:admin-account-edit-dialog (-> account-edit-dialog
|
||||
wrap-admin
|
||||
wrap-client-redirect-unauthenticated
|
||||
(wrap-schema-decode
|
||||
:route-schema (mc/schema [:map [:db/id entity-id]])))})
|
||||
Reference in New Issue
Block a user