progress on making it possible to import from excel.
This commit is contained in:
@@ -21,21 +21,25 @@
|
||||
(defn upsert-multi! [rows]
|
||||
(let [k (vec (map #(keyword (kebab->snake (name %))) [:company-id :vendor-id :invoice-number :total :date :imported]))
|
||||
column-names (str/join "," (map name k))]
|
||||
(doseq [rows (partition-all 2000 rows)]
|
||||
(j/db-do-prepared (get-conn)
|
||||
(sql/format {:with [[[:v (str "(" column-names ")")]
|
||||
(helpers/values (->> rows
|
||||
(map clj->db )
|
||||
(map (apply juxt k))))]]
|
||||
:insert-into [[:invoices k]
|
||||
{:select [:v.company-id :v.vendor-id :v.invoice-number :v.total (sql/raw "cast(v.date as timestamp)") :v.imported ]
|
||||
:from [:v]
|
||||
:left-join [[:invoices :exist]
|
||||
[:and
|
||||
[:= :exist.invoice-number :v.invoice-number]
|
||||
[:= :exist.company-id :v.company-id]
|
||||
[:= :exist.vendor-id :v.vendor-id]]]
|
||||
:where [:= :exist.id nil] }] })))))
|
||||
(reduce
|
||||
(fn [affected rows]
|
||||
(+ affected
|
||||
(first (j/db-do-prepared (get-conn)
|
||||
(sql/format {:with [[[:v (str "(" column-names ")")]
|
||||
(helpers/values (->> rows
|
||||
(map clj->db )
|
||||
(map (apply juxt k))))]]
|
||||
:insert-into [[:invoices k]
|
||||
{:select [:v.company-id :v.vendor-id :v.invoice-number :v.total (sql/raw "cast(v.date as timestamp)") :v.imported ]
|
||||
:from [:v]
|
||||
:left-join [[:invoices :exist]
|
||||
[:and
|
||||
[:= :exist.invoice-number :v.invoice-number]
|
||||
[:= :exist.company-id :v.company-id]
|
||||
[:= :exist.vendor-id :v.vendor-id]]]
|
||||
:where [:= :exist.id nil] }] })))))
|
||||
0
|
||||
(partition-all 2000 rows))))
|
||||
|
||||
(def base-query (sql/build :select :invoices.*
|
||||
:from :invoices))
|
||||
|
||||
@@ -135,12 +135,16 @@
|
||||
:imported true
|
||||
:status "unpaid"
|
||||
:invoice-number invoice-number
|
||||
:date date}))))]
|
||||
:date date}))))
|
||||
|
||||
(invoices/upsert-multi! insert-rows)
|
||||
inserted-row-count (invoices/upsert-multi! insert-rows)
|
||||
already-imported-count (- (count insert-rows) inserted-row-count)]
|
||||
|
||||
|
||||
|
||||
{:status 200
|
||||
:body (pr-str {:imported (count insert-rows)
|
||||
:body (pr-str {:imported inserted-row-count
|
||||
:already-imported already-imported-count
|
||||
:errors (map #(dissoc % :date) error-rows)})
|
||||
:headers {"Content-Type" "application/edn"}}))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user