(ns auto-ap.datomic.bank-accounts
  (:require
   [auto-ap.datomic :refer [conn]]
   [datomic.client.api :as dc]))

(defn add-arg [query name value where & rest]
  (let [query (-> query
                  (update :args conj value)
                  (update-in [:query :in] conj name)
                  (update-in [:query :where] conj where))]
    (reduce #(update-in %1 [:query :where] conj %2) query rest)))

(def default-read '(pull ?e [* {:client/_bank-accounts [:db/id]}]))

(defn <-datomic [x]
  (->> x 
       (map #(update % :bank-account/type :db/ident))
       ))



(defn get-by-id [id]
  (->> 
   (dc/q (-> {:query {:find [default-read]
                         :in ['$]
                         :where []}
                 :args [(dc/db conn)]}
                (add-arg '?e id ['?e])))
   (map first)
   (<-datomic)   
   (first)))




