Skip to content

Commit 7260186

Browse files
authored
Fix invoices limit (#469)
* forbid invoices bigger than max available funds * consider unpaid invoices to get available funds * add balance left to job description
1 parent a5c2471 commit 7260186

6 files changed

Lines changed: 47 additions & 7 deletions

File tree

server/src/ethlance/server/graphql/resolvers.cljs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,27 @@
10591059
(:sum result))))
10601060

10611061

1062+
(defn job->balance-left-resolver
1063+
[root _args _]
1064+
(db/with-async-resolver-conn conn
1065+
(let [parsed-root (graphql-utils/gql->clj root)
1066+
job-id (:job/id parsed-root)
1067+
query-funding {:select [[(sql/call :sum :job-funding/amount) :amount]]
1068+
:from [:JobFunding]
1069+
:where [:= :JobFunding.job/id job-id]}
1070+
query-invoices {:select [[(sql/call :* -1 :JobStoryInvoiceMessage.invoice/amount-requested) :amount]]
1071+
:from [:JobStoryInvoiceMessage]
1072+
:join [:jobStory [:= :JobStory.job-story/id :JobStoryInvoiceMessage.job-story/id]]
1073+
:where [:and
1074+
[:= :JobStory.job/id job-id]
1075+
[:in :JobStoryInvoiceMessage.invoice/status ["created" "dispute-raised"]]]}
1076+
query {:select [[(sql/call :coalesce (sql/call :sum :t.amount) 0) :result]]
1077+
:from [[{:union-all [query-funding query-invoices]} :t]]}
1078+
result (:result (<? (db/get conn query)))]
1079+
(log/debug (str "job->balance-left-resolver " job-id " | " result))
1080+
result)))
1081+
1082+
10621083
(defn sign-in-mutation
10631084
[_ {:keys [:data :data-signature] :as input} {:keys [config]}]
10641085
(try-catch-throw
@@ -1329,6 +1350,7 @@
13291350
:invoices job->invoices-resolver
13301351
:invoice invoice-resolver
13311352
:balance job->balance-resolver
1353+
:balanceLeft job->balance-left-resolver
13321354
:job_requiredSkills job->required-skills-resolver}
13331355
:JobStory {:jobStory_employerFeedback job-story->employer-feedback-resolver
13341356
:jobStory_candidateFeedback job-story->candidate-feedback-resolver

shared/src/ethlance/shared/graphql/schema.cljs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@
453453
job_dateUpdated: Float # TODO: change back to Date after switching to district-ui-graphql
454454
455455
balance: Float
456+
balanceLeft: Float
456457
job_tokenType: String
457458
job_tokenAmount: Float
458459
job_tokenAddress: String

ui/resources/public/less/page/new-invoice.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494
padding-left: 0.4em;
9595
}
9696
}
97+
98+
> .max-available > div {
99+
display: inline;
100+
margin-left: 0.3rem;
101+
}
97102
}
98103

99104
> .right-form {

ui/src/ethlance/ui/page/job_detail.cljs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@
571571
has-accepted-arbiter? (not (nil? (get results :job/arbiter)))
572572
token-details (get results :token-details)
573573
job-balance (get results :balance)
574+
job-balance-left (get results :balance-left)
574575

575576
invoices (get-in results [:invoices :items])
576577
unpaid-invoices (filter #(= "created" (:invoice/status %)) invoices)
@@ -594,7 +595,9 @@
594595
[:div.ticket-listing
595596
[:div.ticket
596597
[:div.label "Available Funds"]
597-
[c-token-info job-balance token-details]]]
598+
[c-token-info job-balance token-details]
599+
[:div.label "Without unpaid invoices"]
600+
[c-token-info job-balance-left token-details]]]
598601

599602
(when job-ongoing? [c-add-funds contract-address (:job/token-id results) token-details])
600603
[:div.profiles
@@ -654,6 +657,7 @@
654657
:job/token-address
655658
:job/token-id
656659
:balance
660+
:balance-left
657661

658662
[:token-details
659663
[:token-detail/id

ui/src/ethlance/ui/page/new_invoice.cljs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[ethlance.ui.component.select-input :refer [c-select-input]]
88
[ethlance.ui.component.textarea-input :refer [c-textarea-input]]
99
[ethlance.ui.component.token-amount-input :refer [c-token-amount-input]]
10+
[ethlance.ui.component.token-info :refer [c-token-info]]
1011
[re-frame.core :as re]))
1112

1213

@@ -40,6 +41,7 @@
4041
:job/token-amount
4142
:job/token-type
4243
:job/token-id
44+
:balance-left
4345
[:token-details
4446
[:token-detail/id
4547
:token-detail/type
@@ -65,7 +67,10 @@
6567
(sort-by :job-story/date-created ,,,)
6668
reverse)
6769
token-display-name (name (or (@job-token :symbol) (@job-token :type) ""))
68-
job-token-decimals (get-in @*invoiced-job [:job :token-details :token-detail/decimals])
70+
job-token-details (get-in @*invoiced-job [:job :token-details])
71+
job-token-decimals (:token-detail/decimals job-token-details)
72+
balance-left (get-in @*invoiced-job [:job :balance-left])
73+
show-balance-left? (not (nil? balance-left))
6974
no-job-selected? (nil? @*invoiced-job)
7075
focus-on-element (fn [id _event] (.focus (.getElementById js/document id)))
7176
validations (re/subscribe [:page.new-invoice/validations])
@@ -113,8 +118,9 @@
113118
:disabled no-job-selected?
114119
:on-change #(re/dispatch [:page.new-invoice/set-invoice-amount %])}]
115120
[:div.post-label token-display-name]]
116-
[:div.usd-estimate @estimated-usd]]
117-
121+
[:div.usd-estimate @estimated-usd]
122+
(when show-balance-left?
123+
[:div.max-available "Max available:" [c-token-info balance-left job-token-details]])]
118124
[:div.right-form
119125
[:div.label "Message"]
120126
[c-textarea-input

ui/src/ethlance/ui/page/new_invoice/subscriptions.cljs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
:page.new-invoice/validations
6565
:<- [::form-fields]
6666
(fn [{:keys [invoiced-job invoice-amount]}]
67-
{:invoiced-job (not (nil? invoiced-job))
68-
:invoice-amount (and (not (nil? (get invoice-amount :token-amount)))
69-
(> (:token-amount invoice-amount) 0))}))
67+
(let [balance-left (get-in invoiced-job [:job :balance-left])]
68+
{:invoiced-job (not (nil? invoiced-job))
69+
:invoice-amount (and (not (nil? (get invoice-amount :token-amount)))
70+
(> (:token-amount invoice-amount) 0)
71+
(<= (:token-amount invoice-amount) balance-left))})))

0 commit comments

Comments
 (0)