From 6e573a0a574b36113f1cce24bfa8cfcc8259df19 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 6 Apr 2018 17:54:05 -0700 Subject: [PATCH] improved import --- src/clj/auto_ap/background/mail.clj | 11 +---------- src/clj/auto_ap/db/invoices.clj | 11 +++++++++++ src/clj/auto_ap/routes/invoices.clj | 13 +------------ 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/clj/auto_ap/background/mail.clj b/src/clj/auto_ap/background/mail.clj index e078acf0..1fa45a70 100644 --- a/src/clj/auto_ap/background/mail.clj +++ b/src/clj/auto_ap/background/mail.clj @@ -43,16 +43,7 @@ (filter #(re-find #"application/pdf" (:content-type %)) ))] (let [filename (str "/tmp/" (UUID/randomUUID) ".pdf")] (io/copy (:body pdf-stream) (io/file filename)) - (invoices/insert-multi! - (for [{:keys [total date invoice-number customer-identifier vendor] :as row} - (parse/parse-file filename filename)] - (do - (println row) - (assoc row - :company (:name (parse/best-match companies customer-identifier)) - :imported false - :potential-duplicate false)))) - + (invoices/import (parse/parse-file filename filename) companies) (io/delete-file filename)))))) (sqs/delete-message (assoc message :queue-url queue-url ))))) diff --git a/src/clj/auto_ap/db/invoices.clj b/src/clj/auto_ap/db/invoices.clj index 1e7bcf05..dfa2632e 100644 --- a/src/clj/auto_ap/db/invoices.clj +++ b/src/clj/auto_ap/db/invoices.clj @@ -1,5 +1,6 @@ (ns auto-ap.db.invoices (:require [clojure.java.jdbc :as j] + [auto-ap.parse :as parse] [auto-ap.db.utils :refer [clj->db db->clj get-conn]])) (defn insert-multi! [rows] @@ -26,3 +27,13 @@ (if company (map db->clj (j/query (get-conn) ["SELECT * FROM invoices WHERE (imported=false or imported is null) AND company_id = ?" (Integer/parseInt company)])) (map db->clj (j/query (get-conn) "SELECT * FROM invoices WHERE imported=false or imported is null")))) + +(defn import [parsed-invoices companies] + (insert-multi! + (for [{:keys [total date invoice-number customer-identifier vendor] :as row} parsed-invoices] + (do + (println row) + (assoc row + :company (:name (parse/best-match companies customer-identifier)) + :imported false + :potential-duplicate false))))) diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index fb2f0b8a..163fbfcd 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -40,19 +40,8 @@ (POST "/upload" {{ files "file"} :params :as params} (let [{:keys [filename tempfile]} files - existing-invoices (invoices/get-all) companies (companies/get-all)] - (invoices/insert-multi! - (for [{:keys [total date invoice-number customer-identifier vendor] :as row} - (parse/parse-file (.getPath tempfile) filename)] - (assoc row - :company-id (:id (parse/best-match companies customer-identifier)) - - :imported false - :potential-duplicate (boolean (seq (filter #(and (= vendor (:vendor %)) - (= invoice-number (:invoice-number %))) - existing-invoices))) - ))) + (invoices/import (parse/parse-file (.getPath tempfile) filename) companies) {:status 200 :body (pr-str (invoices/get-pending ((:query-params params ) "company"))) :headers {"Content-Type" "application/edn"}}))