simplify sorting

This commit is contained in:
Bryce
2024-04-10 14:48:47 -07:00
parent 1493b03ba3
commit 65be50cf9f
7 changed files with 104 additions and 81 deletions

View File

@@ -601,7 +601,8 @@
(:sort args))) (:sort args)))
(defn apply-sort-3 [args results] (defn apply-sort-3 [args results]
(let [sort-bys (conj (:sort args)
(let [sort-bys (conj (into [] (:sort args))
{:sort-key "default" :asc (if (contains? args :default-asc?) {:sort-key "default" :asc (if (contains? args :default-asc?)
(:default-asc? args) (:default-asc? args)
true)}) true)})
@@ -609,6 +610,7 @@
comparator (fn [xs ys] comparator (fn [xs ys]
(reduce (reduce
(fn [_ i] (fn [_ i]
(let [comparison (if (:asc (nth sort-bys i)) (let [comparison (if (:asc (nth sort-bys i))
(compare (nth xs i) (nth ys i)) (compare (nth xs i) (nth ys i))
(compare (nth ys i) (nth xs i)))] (compare (nth ys i) (nth xs i)))]

View File

@@ -4,6 +4,9 @@
[clj-time.core :as time] [clj-time.core :as time]
[clojure.string :as str])) [clojure.string :as str]))
(defn wrap-copy-qp-pqp [handler]
(fn [request]
(handler (assoc request :parsed-query-params (:query-params request)))))
(defn wrap-parse-query-params [handler parser] (defn wrap-parse-query-params [handler parser]
(fn parsed-handler [request] (fn parsed-handler [request]
@@ -74,7 +77,7 @@
"all" "all"
(assoc query-params (assoc query-params
start-date-key (time/plus (time/now) (time/years -3)) start-date-key (time/plus (time/now) (time/years -6))
end-date-key (time/now)) end-date-key (time/now))
query-params) query-params)

View File

@@ -5,6 +5,7 @@
pull-many query2]] pull-many query2]]
[auto-ap.graphql.utils :refer [extract-client-ids]] [auto-ap.graphql.utils :refer [extract-client-ids]]
[auto-ap.logging :as alog] [auto-ap.logging :as alog]
[auto-ap.query-params :refer [wrap-copy-qp-pqp]]
[auto-ap.routes.admin.clients :as route] [auto-ap.routes.admin.clients :as route]
[auto-ap.routes.indicators :as indicators] [auto-ap.routes.indicators :as indicators]
[auto-ap.routes.queries :as q] [auto-ap.routes.queries :as q]
@@ -27,7 +28,7 @@
:refer [apply-middleware-to-all-handlers entity-id :refer [apply-middleware-to-all-handlers entity-id
form-validation-error html-response main-transformer form-validation-error html-response main-transformer
many-entity modal-response ref->enum-schema strip temp-id many-entity modal-response ref->enum-schema strip temp-id
wrap-entity wrap-schema-enforce]] wrap-entity wrap-schema-enforce wrap-merge-prior-hx]]
[auto-ap.time :as atime] [auto-ap.time :as atime]
[bidi.bidi :as bidi] [bidi.bidi :as bidi]
[cheshire.core :as cheshire] [cheshire.core :as cheshire]
@@ -77,7 +78,7 @@
(com/text-input {:name "name" (com/text-input {:name "name"
:id "name" :id "name"
:class "hot-filter" :class "hot-filter"
:value (:name (:parsed-query-params request)) :value (:name (:query-params request))
:placeholder "Best Restaurant LLC" :placeholder "Best Restaurant LLC"
:size :small})) :size :small}))
@@ -85,14 +86,14 @@
(com/text-input {:name "code" (com/text-input {:name "code"
:id "code" :id "code"
:class "hot-filter" :class "hot-filter"
:value (:code (:parsed-query-params request)) :value (:code (:query-params request))
:placeholder "BRLC" :placeholder "BRLC"
:size :small})) :size :small}))
(com/field {:label "Group"} (com/field {:label "Group"}
(com/text-input {:name "group" (com/text-input {:name "group"
:id "group" :id "group"
:class "hot-filter" :class "hot-filter"
:value (:group (:parsed-query-params request)) :value (:group (:query-params request))
:placeholder "NTG" :placeholder "NTG"
:size :small})) :size :small}))
(com/field {:label "Select"} (com/field {:label "Select"}
@@ -150,7 +151,7 @@
:client/location-matches [:location-match/matches :location-match/location :db/id]}]) :client/location-matches [:location-match/matches :location-match/location :db/id]}])
(defn fetch-ids [db request] (defn fetch-ids [db request]
(let [query-params (:parsed-query-params request) (let [query-params (:query-params request)
valid-clients (extract-client-ids #_(:clients request) valid-clients (extract-client-ids #_(:clients request)
(map first (dc/q '[:find ?c :where [?c :client/code]] (dc/db conn))) (map first (dc/q '[:find ?c :where [?c :client/code]] (dc/db conn)))
(:client-id query-params) (:client-id query-params)
@@ -1842,8 +1843,8 @@
(def key->handler (def key->handler
(apply-middleware-to-all-handlers (apply-middleware-to-all-handlers
{::route/page (helper/page-route grid-page) {::route/page (helper/page-route grid-page :parse-query-params? false)
::route/table (helper/table-route grid-page) ::route/table (helper/table-route grid-page :parse-query-params? false)
::route/new-location (add-new-primitive-handler [:step-params :client/locations] ::route/new-location (add-new-primitive-handler [:step-params :client/locations]
"" ""
location-row) location-row)
@@ -1904,7 +1905,10 @@
(mm/wrap-wizard client-wizard))} (mm/wrap-wizard client-wizard))}
(fn [h] (fn [h]
(-> h (-> h
(wrap-copy-qp-pqp)
(wrap-apply-sort grid-page) (wrap-apply-sort grid-page)
(wrap-merge-prior-hx)
(wrap-schema-enforce :query-schema query-schema) (wrap-schema-enforce :query-schema query-schema)
(wrap-schema-enforce :hx-schema query-schema)
(wrap-admin) (wrap-admin)
(wrap-client-redirect-unauthenticated))))) (wrap-client-redirect-unauthenticated)))))

View File

@@ -21,6 +21,8 @@
[malli.transform :as mt] [malli.transform :as mt]
[taoensso.encore :refer [filter-vals]])) [taoensso.encore :refer [filter-vals]]))
(defn row* [gridspec user entity {:keys [flash? delete-after-settle? request class] :as options}] (defn row* [gridspec user entity {:keys [flash? delete-after-settle? request class] :as options}]
(let [cells (if (:check-boxes? gridspec) (let [cells (if (:check-boxes? gridspec)
[(com/data-grid-cell {} (com/checkbox {:name "id" :value ((:id-fn gridspec) entity) [(com/data-grid-cell {} (com/checkbox {:name "id" :value ((:id-fn gridspec) entity)
@@ -75,6 +77,10 @@
"default sort")) "default sort"))
(defn table* [grid-spec user {{:keys [start per-page flash-id sort]} :parsed-query-params :as request}] (defn table* [grid-spec user {{:keys [start per-page flash-id sort]} :parsed-query-params :as request}]
(alog/info ::TABLE-QP
:qp (:query-params request)
:pqp (:parsed-query-params request)
:sort sort)
(let [start (or start 0) (let [start (or start 0)
per-page (or per-page 25) per-page (or per-page 25)
[entities total] ((:fetch-page grid-spec) [entities total] ((:fetch-page grid-spec)
@@ -206,9 +212,9 @@
set)] set)]
(handler (assoc request :trimmed-clients valid-clients))))) (handler (assoc request :trimmed-clients valid-clients)))))
(defn table-route [grid-spec] (defn table-route [grid-spec & {:keys [parse-query-params?] :or {parse-query-params? true}}]
(-> (fn table [{:keys [identity] :as request}] (cond-> (fn table [{:keys [identity] :as request}]
(alog/peek ::TABLE-QP (:parsed-query-params request))
(let [unparse-query-params (or (:unparse-query grid-spec) (let [unparse-query-params (or (:unparse-query grid-spec)
default-unparse-query-params)] default-unparse-query-params)]
(html-response (table* (html-response (table*
@@ -233,14 +239,18 @@
"selected" "all-selected")))} ;; TODO seems hacky to special case selected and all-selected here "selected" "all-selected")))} ;; TODO seems hacky to special case selected and all-selected here
:oob (when-let [oob-render (:oob-render grid-spec)] :oob (when-let [oob-render (:oob-render grid-spec)]
(oob-render request))))) (oob-render request)))))
(wrap-trim-client-ids) true (wrap-trim-client-ids)
(query-params/wrap-parse-query-params (or (:parse-query-params grid-spec) parse-query-params? (query-params/wrap-parse-query-params (or (:parse-query-params grid-spec)
(default-parse-query-params grid-spec))) (default-parse-query-params grid-spec)))
(wrap-secure) true (wrap-secure)
(wrap-client-redirect-unauthenticated))) true (wrap-client-redirect-unauthenticated)))
(defn page-route [grid-spec] (defn page-route [grid-spec & {:keys [parse-query-params?] :or {parse-query-params? true}}]
(-> (fn page [{:keys [identity] :as request}]
(cond-> (fn page [{:keys [identity] :as request}]
(alog/info ::page-route
:pqp (:parsed-query-params request)
:qp (:query-params request))
(base-page (base-page
request request
(com/page {:nav (:nav grid-spec) (com/page {:nav (:nav grid-spec)
@@ -263,11 +273,11 @@
(if (string? (:title grid-spec)) (if (string? (:title grid-spec))
(:title grid-spec) (:title grid-spec)
((:title grid-spec) request)))) ((:title grid-spec) request))))
(wrap-trim-client-ids) true (wrap-trim-client-ids)
(query-params/wrap-parse-query-params (or (:parse-query-params grid-spec) parse-query-params? (query-params/wrap-parse-query-params (or (:parse-query-params grid-spec)
(default-parse-query-params grid-spec))) (default-parse-query-params grid-spec)))
(wrap-secure) true (wrap-secure)
(wrap-client-redirect-unauthenticated))) true (wrap-client-redirect-unauthenticated)))
(def request-spec (m/schema [:map])) (def request-spec (m/schema [:map]))
(def entity-spec (m/schema [:map])) (def entity-spec (m/schema [:map]))

View File

@@ -7,6 +7,7 @@
[auto-ap.datomic.accounts :as d-accounts] [auto-ap.datomic.accounts :as d-accounts]
[auto-ap.datomic.bank-accounts :as d-bank-accounts] [auto-ap.datomic.bank-accounts :as d-bank-accounts]
[auto-ap.datomic.invoices :as d-invoices] [auto-ap.datomic.invoices :as d-invoices]
[auto-ap.query-params :refer [wrap-copy-qp-pqp]]
[auto-ap.graphql.checks :as gq-checks :refer [base-payment [auto-ap.graphql.checks :as gq-checks :refer [base-payment
invoice-payments invoice-payments
print-checks-internal print-checks-internal
@@ -236,6 +237,7 @@
query-params) query-params)
true true
(merge-query {:query {:find ['?sort-default '?e]}})))] (merge-query {:query {:find ['?sort-default '?e]}})))]
(->> (observable-query query) (->> (observable-query query)
(apply-sort-3 (assoc query-params :default-asc? false)) (apply-sort-3 (assoc query-params :default-asc? false))
(apply-pagination query-params)))) (apply-pagination query-params))))
@@ -715,7 +717,6 @@
(map :invoice-id invoices)) (map :invoice-id invoices))
(into {}))] (into {}))]
(every? (fn [%] (every? (fn [%]
(println "TEST" (:amount %) (outstanding-balances (:invoice-id %)))
(not (does-amount-exceed-outstanding? (:amount %) (outstanding-balances (:invoice-id %))))) (not (does-amount-exceed-outstanding? (:amount %) (outstanding-balances (:invoice-id %)))))
invoices)))]]] invoices)))]]]
[:has-warning? :boolean] [:has-warning? :boolean]
@@ -1176,13 +1177,13 @@
(def key->handler (def key->handler
(apply-middleware-to-all-handlers (apply-middleware-to-all-handlers
(-> (->
{::route/all-page (-> (helper/page-route grid-page) {::route/all-page (-> (helper/page-route grid-page :parse-query-params? false)
(wrap-implied-route-param :status nil)) (wrap-implied-route-param :status nil))
::route/paid-page (-> (helper/page-route grid-page) ::route/paid-page (-> (helper/page-route grid-page :parse-query-params? false)
(wrap-implied-route-param :status :invoice-status/paid)) (wrap-implied-route-param :status :invoice-status/paid))
::route/unpaid-page (-> (helper/page-route grid-page) ::route/unpaid-page (-> (helper/page-route grid-page :parse-query-params? false)
(wrap-implied-route-param :status :invoice-status/unpaid)) (wrap-implied-route-param :status :invoice-status/unpaid))
::route/voided-page (-> (helper/page-route grid-page) ::route/voided-page (-> (helper/page-route grid-page :parse-query-params? false)
(wrap-implied-route-param :status :invoice-status/voided)) (wrap-implied-route-param :status :invoice-status/voided))
::route/unvoid (-> unvoid-invoice ::route/unvoid (-> unvoid-invoice
(wrap-entity [:route-params :db/id] default-read) (wrap-entity [:route-params :db/id] default-read)
@@ -1211,10 +1212,11 @@
(mm/wrap-wizard pay-wizard) (mm/wrap-wizard pay-wizard)
(mm/wrap-decode-multi-form-state)) (mm/wrap-decode-multi-form-state))
::route/table (helper/table-route grid-page)} ::route/table (helper/table-route grid-page :parse-query-params? false)}
(merge new-invoice-wizard/key->handler)) (merge new-invoice-wizard/key->handler))
(fn [h] (fn [h]
(-> h (-> h
(wrap-copy-qp-pqp)
(wrap-status-from-source) (wrap-status-from-source)
(wrap-apply-sort grid-page) (wrap-apply-sort grid-page)
(wrap-merge-prior-hx) (wrap-merge-prior-hx)

View File

@@ -7,6 +7,7 @@
[auto-ap.graphql.utils :refer [assert-can-see-client [auto-ap.graphql.utils :refer [assert-can-see-client
exception->notification exception->notification
extract-client-ids notify-if-locked]] extract-client-ids notify-if-locked]]
[auto-ap.query-params :refer [wrap-copy-qp-pqp]]
[auto-ap.logging :as alog] [auto-ap.logging :as alog]
[auto-ap.permissions :refer [can?]] [auto-ap.permissions :refer [can?]]
[auto-ap.routes.invoice :as invoice-route] [auto-ap.routes.invoice :as invoice-route]
@@ -38,8 +39,8 @@
[malli.transform :as mt])) [malli.transform :as mt]))
(defn exact-match-id* [request] (defn exact-match-id* [request]
(if (nat-int? (:exact-match-id (:parsed-query-params request))) (if (nat-int? (:exact-match-id (:query-params request)))
[:div {:x-data (hx/json {:exact_match (:exact-match-id (:parsed-query-params request))}) :id "exact-match-id-tag"} [:div {:x-data (hx/json {:exact_match (:exact-match-id (:query-params request))}) :id "exact-match-id-tag"}
(com/hidden {:name "exact-match-id" (com/hidden {:name "exact-match-id"
"x-model" "exact_match"}) "x-model" "exact_match"})
(com/pill {:color :primary} (com/pill {:color :primary}
@@ -68,7 +69,7 @@
:value (:vendor (:query-params request)) :value (:vendor (:query-params request))
:value-fn :db/id :value-fn :db/id
:content-fn :vendor/name})) :content-fn :vendor/name}))
(date-range-field* request) (date-range-field* (assoc request :parsed-query-params (:query-params request)))
(com/field {:label "Check #"} (com/field {:label "Check #"}
(com/text-input {:name "check-number" (com/text-input {:name "check-number"
:id "check-number" :id "check-number"
@@ -530,13 +531,13 @@
(def key->handler (def key->handler
(apply-middleware-to-all-handlers (apply-middleware-to-all-handlers
{::route/cleared-page (-> (helper/page-route grid-page) {::route/cleared-page (-> (helper/page-route grid-page :parse-query-params? false)
(wrap-implied-route-param :status :payment-status/cleared)) (wrap-implied-route-param :status :payment-status/cleared))
::route/pending-page (-> (helper/page-route grid-page) ::route/pending-page (-> (helper/page-route grid-page :parse-query-params? false)
(wrap-implied-route-param :status :payment-status/pending)) (wrap-implied-route-param :status :payment-status/pending))
::route/voided-page (-> (helper/page-route grid-page) ::route/voided-page (-> (helper/page-route grid-page :parse-query-params? false)
(wrap-implied-route-param :status :payment-status/voided)) (wrap-implied-route-param :status :payment-status/voided))
::route/all-page (-> (helper/page-route grid-page) ::route/all-page (-> (helper/page-route grid-page :parse-query-params? false)
(wrap-implied-route-param :status nil)) (wrap-implied-route-param :status nil))
::route/delete (-> delete ::route/delete (-> delete
@@ -549,9 +550,10 @@
(wrap-admin)) (wrap-admin))
::route/table (helper/table-route grid-page)} ::route/table (helper/table-route grid-page :parse-query-params? false)}
(fn [h] (fn [h]
(-> h (-> h
(wrap-copy-qp-pqp)
(wrap-apply-sort grid-page) (wrap-apply-sort grid-page)
(wrap-merge-prior-hx) (wrap-merge-prior-hx)
(wrap-status-from-source) (wrap-status-from-source)

View File

@@ -255,7 +255,7 @@
end-date-key (time/now)) end-date-key (time/now))
"all" "all"
(assoc m start-date-key (time/plus (time/now) (time/years -3)) (assoc m start-date-key (time/plus (time/now) (time/years -6))
end-date-key (time/now)) end-date-key (time/now))
m) m)