Files
integreat/src/clj/auto_ap/datomic/clients.clj
2020-08-05 00:29:21 -07:00

59 lines
1.8 KiB
Clojure

(ns auto-ap.datomic.clients
(:require [datomic.api :as d]
[auto-ap.datomic :refer [uri]]
[clojure.tools.logging :as log]))
(defn cleanse [e]
(-> e
(update :client/location-matches
(fn [lms]
(map #(assoc % :location-match/match (first (:location-match/matches %))) lms)))
(update :client/bank-accounts
(fn [bas]
(map (fn [i ba]
(-> ba
(update :bank-account/type :db/ident )
(update :bank-account/sort-order (fn [so] (or so i)))))
(range) bas)))))
(defn get-all []
(->> (d/q '[:find (pull ?e [*
{:client/address [*]}
{:client/bank-accounts [* {:bank-account/type [*]}]}])
:where [?e :client/name]]
(d/db (d/connect uri)))
(map first)
(map cleanse)
))
(defn get-by-id [id]
(->>
(d/query (-> {:query {:find ['(pull ?e [* {:client/bank-accounts [* {:bank-account/type [*]}]}])]
:in ['$ '?e]
:where [['?e]]}
:args [(d/db (d/connect uri)) id]}
))
(first)
(first)
(cleanse)
#_(map first)
#_(first)))
(defn code->id [code]
(->>
(d/query (-> {:query {:find ['?e]
:in ['$ '?code]
:where [['?e :client/code '?code ]]}
:args [(d/db (d/connect uri)) code]}))
(first)
(first)))
(defn assoc-image [code image]
@(d/transact (d/connect uri)
[{ :client/code code
:client/signature-file (str "https://s3.amazonaws.com/integreat-signature-images/" image)}]))
#_(d/pull (d/db (d/connect uri)) '[*] [:client/code "FTLO"])
#_(assoc-image "FTLO" "Fratello.jpg" )