(ns auto-ap.datomic.migrate.account-sorting
  (:require [datomic.api :as d]))


(defn add-account-visibility-fields [conn]
  [[
    {:db/ident :bank-account/visible
     :db/valueType :db.type/boolean
     :db/cardinality :db.cardinality/one
     :db/doc "Whether this bank account is visible"}
    {:db/ident :bank-account/sort-order
     :db/valueType :db.type/long
     :db/cardinality :db.cardinality/one
     :db/doc "The sort order of the bank account"}]])

(defn make-every-account-visible [conn]
  (let [all-account-ids (d/query
                         {:query {:find ['?e]
                                  :in ['$]
                                  :where ['[?e :bank-account/code]]}
                          :args [(d/db conn)]}
                         )]
    [(map
      (fn [[account-id]]
        {:db/id account-id :bank-account/visible true}
        )
      all-account-ids)]))
