Skip to content

Commit

Permalink
OK-719 review style fixes and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vaeinoe committed Dec 19, 2024
1 parent 170b6c0 commit 490784c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@
(declare conn)
(declare spec)

(defn- get-payment-obligation-review [application-key hakukohde]
(defn- get-tuition-payment-obligation-review [application-key hakukohde]
(jdbc/with-db-transaction [conn {:datasource (db/get-datasource :db)}]
(->> (jdbc/query
conn
["select * FROM application_hakukohde_reviews WHERE application_key = ? AND requirement = ? AND hakukohde = ?"
application-key "payment-obligation" hakukohde])
first)))

(defn- store-not-obligated-review [application-key hakukohde]
(defn- store-tuition-fee-not-required-review [application-key hakukohde]
(jdbc/with-db-transaction [conn {:datasource (db/get-datasource :db)}]
(jdbc/insert! conn "application_hakukohde_reviews"
{:application_key application-key
Expand Down Expand Up @@ -403,6 +403,9 @@
check-mail-fn]}))))

(it "should set tuition fee obligation for non fi/sv hakukohde when payment state changes to awaiting"
; Initial state: hakukohde in the application has only english as teaching language,
; and the application / person has no exemption, meaning the application should require both
; application fee AND tuition fee for the hakukohde.
(let [application-id (unit-test-db/init-db-fixture
form-fixtures/payment-exemption-test-form
application-fixtures/application-without-hakemusmaksu-exemption
Expand All @@ -411,13 +414,17 @@
{:person_oid test-person-oid :term test-term :year test-year} runner)
application-key (:key (application-store/get-application application-id))
payment (first (payment/get-raw-payments [application-key]))
obligation (get-payment-obligation-review application-key "payment-info-test-kk-hakukohde")]
obligation (get-tuition-payment-obligation-review application-key "payment-info-test-kk-hakukohde")]
(should= (:awaiting payment/all-states) (:state payment))
(should= {:application_key application-key, :requirement "payment-obligation",
:state "obligated", :hakukohde "payment-info-test-kk-hakukohde"}
(select-keys obligation [:application_key :requirement :state :hakukohde]))))

(it "should set tuition fee obligation for non fi/sv hakukohde when payment state changes to ok-by-proxy"
; Initial state: hakukohde in both applications has only english as teaching language,
; and the application / person has no exemption, and we mark one of the applications paid manually.
; This means the other application should not require application payment BUT should still require
; tuition fee.
(let [application-ids (unit-test-db/init-db-fixture
form-fixtures/payment-exemption-test-form
[application-fixtures/application-without-hakemusmaksu-exemption
Expand All @@ -438,13 +445,16 @@
_ (updater-job/update-kk-payment-status-for-person-handler
{:person_oid test-person-oid :term test-term :year test-year} runner)
payment (first (payment/get-raw-payments [second-key]))
obligation (get-payment-obligation-review second-key "payment-info-test-kk-hakukohde")]
obligation (get-tuition-payment-obligation-review second-key "payment-info-test-kk-hakukohde")]
(should= (:ok-by-proxy payment/all-states) (:state payment))
(should= {:application_key second-key, :requirement "payment-obligation",
:state "obligated", :hakukohde "payment-info-test-kk-hakukohde"}
(select-keys obligation [:application_key :requirement :state :hakukohde]))))

(it "should not set tuition fee obligation for non fi/sv hakukohde when payment state changes to not-required"
; Initial state: hakukohde in the application has only english as teaching language,
; but the application / person has an exemption, meaning the application should require neither
; application fee nor tuition fee for the hakukohde.
(let [application-id (unit-test-db/init-db-fixture
form-fixtures/payment-exemption-test-form
application-fixtures/application-with-hakemusmaksu-exemption
Expand All @@ -453,11 +463,14 @@
{:person_oid test-person-oid :term test-term :year test-year} runner)
application-key (:key (application-store/get-application application-id))
payment (first (payment/get-raw-payments [application-key]))
obligation (get-payment-obligation-review application-key "payment-info-test-kk-hakukohde")]
obligation (get-tuition-payment-obligation-review application-key "payment-info-test-kk-hakukohde")]
(should= (:not-required payment/all-states) (:state payment))
(should-be-nil obligation)))

(it "should not set tuition fee obligation for fi/sv hakukohde"
; Initial state: hakukohde in the application has swedish and/or finnish in its teaching languages,
; so even the application / person has no exemption, the application should require only an
; application fee, but no tuition fee for the hakukohde.
(let [application-id (unit-test-db/init-db-fixture
form-fixtures/payment-exemption-test-form
(merge
Expand All @@ -468,21 +481,24 @@
{:person_oid test-person-oid :term test-term :year test-year} runner)
application-key (:key (application-store/get-application application-id))
payment (first (payment/get-raw-payments [application-key]))
obligation (get-payment-obligation-review application-key "payment-info-test-kk-fisv-hakukohde")]
obligation (get-tuition-payment-obligation-review application-key "payment-info-test-kk-fisv-hakukohde")]
(should= (:awaiting payment/all-states) (:state payment))
(should-be-nil obligation)))

(it "should not override a non-automatic obligation"
; Initial state: hakukohde in the application has only english as teaching language,
; and application / person has no exemption, but there's a human review already for the tuition.
; Application fee should be required, but tuition fee state should not change automatically anymore.
(let [application-id (unit-test-db/init-db-fixture
form-fixtures/payment-exemption-test-form
application-fixtures/application-without-hakemusmaksu-exemption
nil)
application-key (:key (application-store/get-application application-id))
_ (store-not-obligated-review application-key "payment-info-test-kk-hakukohde")
_ (store-tuition-fee-not-required-review application-key "payment-info-test-kk-hakukohde")
_ (updater-job/update-kk-payment-status-for-person-handler
{:person_oid test-person-oid :term test-term :year test-year} runner)
payment (first (payment/get-raw-payments [application-key]))
obligation (get-payment-obligation-review application-key "payment-info-test-kk-hakukohde")]
obligation (get-tuition-payment-obligation-review application-key "payment-info-test-kk-hakukohde")]
(should= (:awaiting payment/all-states) (:state payment))
(should= {:application_key application-key, :requirement "payment-obligation",
:state "not-obligated", :hakukohde "payment-info-test-kk-hakukohde"}
Expand Down
8 changes: 8 additions & 0 deletions src/clj/ataru/applications/application_store.clj
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@
(queries/yesql-add-application-event<! event connection))))))

(defn save-payment-obligation-automatically-changed
"Only used by automatic tuition fee obligation logic. Sets new obligation state if previous one was not set by
virkailija and the state transition is allowed. If successful, adds an event marking the obligation was set automatically."
[application-key hakukohde-oid hakukohde-review-requirement hakukohde-review-state]
(jdbc/with-db-transaction [conn {:datasource (db/get-datasource :db)}]
(let [connection {:connection conn}
Expand All @@ -856,6 +858,11 @@
:event_type
(= "payment-obligation-automatically-changed"))
existing-requirement-review (first (queries/yesql-get-existing-requirement-review review-to-store connection))]
; Main idea:
; - When the state is still unreviewed, it can be always automatically changed.
; - When virkailija has made the latest modification to tuition fee obligation (not automatically-changed?)
; we should never override that state automatically anymore.
; - Once the state is automatically set as obligated, don't modify further without virkailija input.
(when (or (and (= "not-obligated" (:state review-to-store))
(= "unreviewed" (:state existing-requirement-review "unreviewed")))
(and (= "unreviewed" (:state review-to-store))
Expand All @@ -866,6 +873,7 @@
(and (= "obligated" (:state review-to-store))
(= "not-obligated" (:state existing-requirement-review))
automatically-changed?))
; Whenever we set the obligation automatically, also add an event for auditing and possible further state changes.
(queries/yesql-upsert-application-hakukohde-review! review-to-store connection)
(let [event {:application_key application-key
:event_type "payment-obligation-automatically-changed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,15 @@
(payment/get-valid-payment-info-for-application-id tarjonta-service application-id)
(payment/get-valid-payment-info-for-application-key tarjonta-service application-key))))

(defn- oid-if-needs-tuition-fee
(defn- needs-tuition-fee?
[hakukohde]
(let [codes (->> (:opetuskieli-koodi-urit hakukohde)
(map #(first (str/split % #"#")))
set)]
(when (and (seq codes)
(not (contains? codes "oppilaitoksenopetuskieli_1")) ; fi
(not (contains? codes "oppilaitoksenopetuskieli_2")) ; sv
(not (contains? codes "oppilaitoksenopetuskieli_3"))) ; fi/sv
(:oid hakukohde))))
(and (seq codes)
(not (contains? codes "oppilaitoksenopetuskieli_1")) ; fi
(not (contains? codes "oppilaitoksenopetuskieli_2")) ; sv
(not (contains? codes "oppilaitoksenopetuskieli_3"))))) ; fi/sv

(defn- mark-tuition-fee-obligated
"Marks tuition fee (lukuvuosimaksu) obligation for application key for every hakukohde that does not organize
Expand All @@ -140,7 +139,8 @@
hakukohteet (tarjonta/get-hakukohteet
tarjonta-service
(remove nil? hakukohde-oids))
tuition-hakukohde-oids (remove nil? (map oid-if-needs-tuition-fee hakukohteet))]
tuition-hakukohde-oids (remove nil?
(map #(when (needs-tuition-fee? %) (:oid %)) hakukohteet))]
(doseq [hakukohde-oid tuition-hakukohde-oids]
(log/info "Marking tuition payment obligation due to kk application fee eligibility for application key"
application-key "and hakukohde oid" hakukohde-oid)
Expand Down

0 comments on commit 490784c

Please sign in to comment.