Skip to content

Commit 23cedca

Browse files
committed
OK-784 throw an exception when payment status updater returns zero payment states
1 parent a3111a3 commit 23cedca

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

spec/ataru/kk_application_payment/kk_application_payment_status_updater_job_spec.clj

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
[clj-time.format :as time-format]
1010
[clojure.java.jdbc :as jdbc]
1111
[clojure.string :as str]
12-
[speclj.core :refer [it describe should-not-throw stub should-have-invoked should-not-have-invoked
13-
tags with-stubs should= around before]]
12+
[speclj.core :refer [it describe should-throw should-not-throw stub should-have-invoked
13+
should-not-have-invoked tags with-stubs should= around before]]
1414
[ataru.kk-application-payment.kk-application-payment :as payment]
1515
[ataru.fixtures.application :as application-fixtures]
1616
[ataru.fixtures.form :as form-fixtures]
@@ -65,6 +65,13 @@
6565
(get-many-from [_ _])
6666
(remove-from [_ _])
6767
(clear-all [_])))
68+
(def empty-get-haut-cache (reify cache-service/Cache
69+
(get-from [_ _]
70+
[])
71+
(get-many-from [_ _])
72+
(remove-from [_ _])
73+
(clear-all [_])))
74+
6875

6976
(defn start-runner-job [_ _ _ _])
7077

@@ -82,6 +89,13 @@
8289
:koodisto-cache fake-koodisto-cache
8390
:maksut-service mock-maksut-service}))
8491

92+
(def runner-with-empty-haku-cache
93+
(map->FakeJobRunner {:tarjonta-service fake-tarjonta-service
94+
:person-service fake-person-service
95+
:get-haut-cache empty-get-haut-cache
96+
:koodisto-cache fake-koodisto-cache
97+
:maksut-service mock-maksut-service}))
98+
8599
(declare conn)
86100
(declare spec)
87101

@@ -142,6 +156,19 @@
142156
:maksut-secret test-maksut-secret}
143157
(select-keys payment [:application-key :state :maksut-secret]))))
144158

159+
(it "should throw an exception when person and term found but updater doesn't find eligible applications"
160+
(let [application-id (unit-test-db/init-db-fixture
161+
form-fixtures/payment-exemption-test-form
162+
application-fixtures/application-without-hakemusmaksu-exemption
163+
nil)]
164+
; Here the handler first resolves the person from application correctly, but then the updater
165+
; function itself queries for all applications with hakus from get-haku-cache (= all haku oids from applications)
166+
; and finds none from an empty one. This may happen with the very first application(s) for haku until
167+
; cache is completely refreshed, so the job runner needs to retry.
168+
(should-throw
169+
(updater-job/update-kk-payment-status-for-person-handler
170+
{:application_id application-id} runner-with-empty-haku-cache))))
171+
145172
(it "should update payment status fetching person and term with application key"
146173
(let [application-id (unit-test-db/init-db-fixture
147174
form-fixtures/payment-exemption-test-form

src/clj/ataru/kk_application_payment/kk_application_payment_status_updater_job.clj

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,33 @@
145145
(payment/update-payments-for-person-term-and-year person-service tarjonta-service
146146
koodisto-cache get-haut-cache
147147
person-oid application-term application-year)]
148-
(log/info "Update kk application payment status handler for"
149-
person-oid application-term application-year
150-
"returned" (count existing-payments) "created or modified payments and"
151-
(count modified-payments) "existing payments before creating / modifying.")
152-
(doseq [payment modified-payments]
153-
(let [new-state (:state payment)]
154-
(cond
155-
(= (:awaiting payment/all-states) new-state)
156-
(create-payment-and-send-email job-runner maksut-service payment))))
157-
158-
(doseq [application-payment existing-payments]
159-
(let [{:keys [application payment]} application-payment]
160-
(cond
161-
(needs-reminder-sent? payment)
162-
(send-reminder-email-and-mark-sent job-runner payment application))))
163-
164-
(invalidate-maksut-payments-if-needed maksut-service modified-payments)
165-
(log/info "Update kk payment status handler for" person-oid application-term application-year "finished."))
148+
(if (or (some? modified-payments) (some? existing-payments))
149+
(do
150+
(log/info "Update kk application payment status handler for"
151+
person-oid application-term application-year
152+
"returned" (count existing-payments) "created or modified payments and"
153+
(count modified-payments) "existing payments before creating / modifying.")
154+
(doseq [payment modified-payments]
155+
(let [new-state (:state payment)]
156+
(cond
157+
(= (:awaiting payment/all-states) new-state)
158+
(create-payment-and-send-email job-runner maksut-service payment))))
159+
160+
(doseq [application-payment existing-payments]
161+
(let [{:keys [application payment]} application-payment]
162+
(cond
163+
(needs-reminder-sent? payment)
164+
(send-reminder-email-and-mark-sent job-runner payment application))))
165+
166+
(invalidate-maksut-payments-if-needed maksut-service modified-payments)
167+
(log/info "Update kk payment status handler for" person-oid application-term application-year "finished."))
168+
169+
; Here we've already established there should be a person with at least one application, but for the first
170+
; applications in haku, get-haku-cache may still be refreshing so it doesn't return any applications for
171+
; the specific haku. That's a temporary error, and quite short-lived, so let's try again later.
172+
(throw (ex-info "Could not find or create a payment status for person" {:person-oid person-oid
173+
:term application-term
174+
:year application-year}))))
166175
(log/warn "Update kk payment status handler not run for params"
167176
person_oid term year application_id application_key
168177
"because no valid payment info was found.")))))

0 commit comments

Comments
 (0)