Files
integreat/src/clj/auto_ap/graphql/checks.clj
2018-07-25 19:35:26 -07:00

76 lines
3.1 KiB
Clojure

(ns auto-ap.graphql.checks
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql assert-can-see-company]]
[com.walmartlabs.lacinia :refer [execute]]
[com.walmartlabs.lacinia.executor :as executor]
[com.walmartlabs.lacinia.resolve :as resolve]
[auto-ap.db.invoices-checks :as invoices-checks]
[auto-ap.db.checks :as checks]
[auto-ap.db.vendors :as vendors]
[auto-ap.db.invoices :as invoices]
[auto-ap.utils :refer [by]]
[auto-ap.db.companies :as companies]
[auto-ap.time :refer [parse normal-date]]))
(defn get-vendor-for-check [context args value]
(->graphql
(if-let [vendor-cache (:vendor-cache context)]
(vendor-cache (:vendor_id value))
(vendors/get-by-id (:vendor_id value)))))
(defn get-company-for-check [context args value]
(->graphql
(if-let [company-cache (:company-cache context)]
(company-cache (:company_id value))
(companies/get-by-id (:company_id value)))))
(defn get-check-page [context args value]
(let [args (assoc args :id (:id context))
extra-context
(cond-> {}
(executor/selects-field? context :invoice/vendor) (assoc :vendor-cache (by :id (vendors/get-all)))
(executor/selects-field? context :invoice/company) (assoc :company-cache (by :id (companies/get-all))))
checks (map
->graphql
(checks/get-graphql (<-graphql args)))
checks-count (checks/count-graphql (<-graphql args))]
(resolve/with-context
[{:checks checks
:total checks-count
:count (count checks)
:start (:start args 0)
:end (+ (:start args 0) (count checks))}] extra-context)))
(defn add-handwritten-check [context args value]
(let [invoice (invoices/get-by-id (:invoice_id args))
_ (assert-can-see-company (:id context) (:company-id invoice))
check (checks/insert! {:s3-uuid nil
:s3-key nil
:s3-url nil
:check-number (:check_number args)
:amount (:amount args)
:bank-account-id (:bank_account_id args)
:vendor-id (:vendor-id invoice)
:company-id (:company-id invoice)
:invoices [(:invoice_id args)]})]
(invoices-checks/insert-multi! [{:invoice-id (:invoice_id args)
:check-id (:id check)
:amount (:amount args)}])
(invoices/apply-payment (:invoice_id args) (:amount args))
(->graphql
{:s3-url nil
:invoices [(invoices/get-by-id (:invoice_id args))]})))
(defn void-check [context {id :check_id} value]
(let [check (checks/get-by-id id)]
(assert-can-see-company (:id context) (:company-id check))
(assert (= "pending" (:status check)))
(checks/update! {:id id
:amount 0
:status "voided"})
(-> (checks/get-by-id id)
(->graphql))))