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

(defn fix-check-numbers [conn]
  (let [max-check-numbers (d/query {:query {:find '[?e ?check-number (max ?d)] :in ['$] :where ['[?e :bank-account/check-number ?check-number] '[?c :payment/bank-account ?e] '[?c :payment/check-number ?d]]} :args [(d/db conn)]})]
    [(for [[bank-account check-number max-check client] max-check-numbers 
           :when (>= max-check check-number)]
       {:db/id bank-account :payment/check-number (inc max-check)})]))
