Skip to content

Commit

Permalink
Merge pull request #1642 from Opetushallitus/OK-723-payment-module-sh…
Browse files Browse the repository at this point in the history
…ow-logic

Ok 723 payment module show logic
  • Loading branch information
SalamaGofore authored Nov 21, 2024
2 parents 91c5f3c + d394855 commit 9f5e1b6
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 49 deletions.
7 changes: 6 additions & 1 deletion oph-configuration/config.edn.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@
:application-key-masking {:secret-key "{{ataru_application_key_masking_secret_key}}"}
:application-delete-key {:secret-key "{{ataru_application_delete_secret_key}}"}
:yksiloimattomat {:allow {{ataru_allow_yksiloimattomat}}}
:cache {:type {{ataru_cache_type}}}
:cache {:type {{ataru_cache_type}}
:ttl-amounts {:haku {{ataru_cache_ttl_amounts_haku | default('3')}}
:hakukohde {{ataru_cache_ttl_amounts_hakukohde | default('3')}}
:kouta-hakus-by-form-key {{ataru_cache_ttl_amounts_kouta_hakus_by_form_key | default('3')}}
:hakukohde-search {{ataru_cache_ttl_amounts_hakukohde_search | default('3')}}}}
:redis {:uri "{{ataru_redis_uri}}"}
:jobs {:application-payment-module-cron {{ataru_jobs_application_payment_module_cron | default('0 4 * * *')}}}
:aws {:region "{{aws_region}}"
:liiteri-files {:bucket "{{liiteri_s3_bucket}}"}
:feedback-queue {:queue-url "{{palaute_queue_url}}"}
Expand Down
2 changes: 1 addition & 1 deletion spec/ataru/forms/form_access_control_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"upsert-kk-application-payment-module"
(tags :unit)

(it "Is not be able to upsert application payment module as a non-superuser"
(it "Is not able to upsert application payment module as a non-superuser"
(with-redefs [form-store/fetch-by-key (fn [_] field-id-test-form)]
(let [non-superuser-session (update session :identity assoc :superuser false)
result (try (fac/upsert-kk-application-payment-module "test-field-id-change-form" non-superuser-session nil)
Expand Down
21 changes: 17 additions & 4 deletions spec/ataru/hakija/validator_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

(def f form)
(def a answer)
(def flattened-form (util/flatten-form-fields (:content form)))
(def extra-answers (update a :answers conj {:key "foo" :value "barbara"}))
(def answers-by-key (util/answers-by-key (:answers a)))
(def base-oppija-session {:data {:fields {:first-name {:value "Timo"
Expand Down Expand Up @@ -161,10 +162,10 @@
content))))

(def koodisto-cache (reify cache-service/Cache
(get-from [this key])
(get-many-from [this keys])
(remove-from [this key])
(clear-all [this])))
(get-from [_ _])
(get-many-from [_ _])
(remove-from [_ _])
(clear-all [_])))

(describe "application validation"
(tags :unit :backend-validation)
Expand All @@ -190,6 +191,7 @@
answers-by-key
f
(:content f)
flattened-form
#{}
false)))

Expand All @@ -208,6 +210,7 @@
(assoc-in [:params :hidden] true))
:else form))
(:content f))
flattened-form
#{}
false)))

Expand All @@ -228,6 +231,7 @@
(assoc-in [:params :hidden] true))
:else form))
(:content f))
flattened-form
#{}
false)))

Expand All @@ -238,6 +242,7 @@
(assoc-in answers-by-key [:c8558a1f-86e9-4d76-83eb-a0d7e1fd44b0 :value] [])
f
(:content f)
flattened-form
#{}
false)))

Expand All @@ -254,6 +259,7 @@
(dissoc form :validators)
:else form))
(:content f))
flattened-form
#{}
false)))

Expand All @@ -271,6 +277,7 @@
(assoc form :validators ["required"])
:else form))
(:content f))
flattened-form
#{}
false)))

Expand All @@ -288,6 +295,7 @@
(assoc form :validators ["required"])
:else form))
(:content f))
flattened-form
#{}
false)))

Expand All @@ -298,6 +306,7 @@
(assoc-in answers-by-key [:047da62c-9afe-4e28-bfe8-5b50b21b4277 :value] [""])
f
(:content f)
flattened-form
#{}
false)))

Expand All @@ -315,6 +324,7 @@
(assoc form :validators ["required"])
:else form))
(:content f))
flattened-form
#{}
false)))

Expand All @@ -332,6 +342,7 @@
(assoc form :validators ["required"])
:else form))
(:content f))
flattened-form
#{}
false)))

Expand All @@ -350,6 +361,7 @@
(assoc form :validators ["required"])
:else form))
(:content f))
flattened-form
#{}
false)))

Expand All @@ -367,6 +379,7 @@
(assoc form-field :child-validator "birthdate-and-gender-component")
:else form-field))
(:content f))
flattened-form
#{}
false)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[ataru.kk-application-payment.kk-application-payment-module-job :as payment-module-job]
[ataru.component-data.kk-application-payment-module :refer [kk-application-payment-wrapper-key]]
[ataru.fixtures.form :as form-fixtures]
[ataru.component-data.person-info-module :refer [person-info-module-keys]]
[ataru.tarjonta-service.mock-tarjonta-service :as tarjonta-service]))

(def haku-key "payment-info-test-kk-haku-custom-form")
Expand Down Expand Up @@ -38,6 +39,12 @@
(payment-module-job/check-and-update ts [haku-key])
(let [form (form-store/fetch-by-key form-key)]
(should= 6 (count (:content form)))
(should= (:onr-kk-application-payment person-info-module-keys)
(->> form
:content
(filter #(= (:module %) "person-info"))
first
:id))
(should= true (->> form
:content
(some #(= (:id %) kk-application-payment-wrapper-key))
Expand All @@ -59,6 +66,12 @@
(payment-module-job/check-and-update ts [haku-key])
(let [form (form-store/fetch-by-key form-key)]
(should= 6 (count (:content form)))
(should= (:onr-kk-application-payment person-info-module-keys)
(->> form
:content
(filter #(= (:module %) "person-info"))
first
:id))
(should= true (->> form
:content
(some #(= (:id %) kk-application-payment-wrapper-key))
Expand Down
54 changes: 54 additions & 0 deletions spec/ataru/util_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
(:require [speclj.core :refer :all]
[ataru.fixtures.answer :refer [answer]]
[ataru.fixtures.person-info-form :refer [form]]
[ataru.component-data.person-info-module :as person-module]
[medley.core :refer [find-first]]
[ataru.component-data.kk-application-payment-module :as payment-module :refer [kk-application-payment-wrapper-key asiakasnumero-migri-key kk-application-payment-choice-key]]
[ataru.util :as util]))

(def form-with-visibility-conditions (assoc form :content [(person-module/person-info-module :onr-kk-application-payment)
(payment-module/kk-application-payment-module)]))

(def payment-wrapper (get-in form-with-visibility-conditions [:content 1]))
(def answer-hiding-wrapper {:nationality {:value [["246"]]}})
(def answer-showing-wrapper {:nationality {:value [["200"]["245"]]}})

(defn extract-wrapper-sections [form]
(map #(select-keys % [:id :label :children])
(filter #(= (:fieldClass %) "wrapperElement") (:content form))))
Expand All @@ -20,6 +30,50 @@
(map keys)
flatten))))

(describe "is-field-hidden-by-section-visibility-conditions"
(tags :unit :visibility)

(it "field is shown by default"
(should= nil (util/is-field-hidden-by-section-visibility-conditions
form-with-visibility-conditions
{}
payment-wrapper
false)))

(it "field is hidden by section"
(should= true (util/is-field-hidden-by-section-visibility-conditions
form-with-visibility-conditions
answer-hiding-wrapper
payment-wrapper
false)))

(it "field is shown when answer does not meet condition"
(should= nil (util/is-field-hidden-by-section-visibility-conditions
form-with-visibility-conditions
answer-showing-wrapper
payment-wrapper
false))))

(describe "find-wrapper-parent"
(tags :unit :form)

(it "returns nil for element with no parent"
(let [flattened-form (util/flatten-form-fields (:content form-with-visibility-conditions))
wrapper-field (find-first #(= (:id %) kk-application-payment-wrapper-key) flattened-form)]
(should= nil (util/find-wrapper-parent flattened-form wrapper-field))))

(it "finds parent that is wrapper"
(let [flattened-form (util/flatten-form-fields (:content form-with-visibility-conditions))
choice-field (find-first #(= (:id %) kk-application-payment-choice-key) flattened-form)]
(should= kk-application-payment-wrapper-key
(:id (util/find-wrapper-parent flattened-form choice-field)))))

(it "finds grandparent that is wrapper"
(let [flattened-form (util/flatten-form-fields (:content form-with-visibility-conditions))
migri-field (find-first #(= (:id %) asiakasnumero-migri-key) flattened-form)]
(should= kk-application-payment-wrapper-key
(:id (util/find-wrapper-parent flattened-form migri-field))))))

(def field-descriptor-id "64d4a625-370b-4814-ae4f-d5956e8881be")
(def field-descriptor {:id field-descriptor-id
:label {:fi "Pohjakoulutuksesi?" :sv ""}
Expand Down
50 changes: 24 additions & 26 deletions src/clj/ataru/hakija/validator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,22 @@
(defn extra-answers-not-in-original-form [form-keys answer-keys]
(apply disj (set answer-keys) form-keys))

(defn- passed? [has-applied form answer validators answers-by-key field-descriptor virkailija?]
(every? (fn [validator]
(first (async/<!! (validator/validate {:has-applied has-applied
:try-selection (constantly (async/go [true []]))
:validator validator
:value answer
:answers-by-key answers-by-key
:tarjonta-hakukohteet (get-in form [:tarjonta :hakukohteet])
:priorisoivat-hakukohderyhmat (:priorisoivat-hakukohderyhmat form)
:rajaavat-hakukohderyhmat (:rajaavat-hakukohderyhmat form)
:virkailija? virkailija?
:field-descriptor field-descriptor}))))
validators))
(defn- passed? [has-applied form flattened-form-fields answer validators answers-by-key field-descriptor virkailija?]
(let [wrapper-parent (util/find-wrapper-parent flattened-form-fields field-descriptor)
parent-hidden? (and (some? wrapper-parent) (util/is-field-hidden-by-section-visibility-conditions form answers-by-key wrapper-parent false))]
(or parent-hidden?
(every? (fn [validator]
(first (async/<!! (validator/validate {:has-applied has-applied
:try-selection (constantly (async/go [true []]))
:validator validator
:value answer
:answers-by-key answers-by-key
:tarjonta-hakukohteet (get-in form [:tarjonta :hakukohteet])
:priorisoivat-hakukohderyhmat (:priorisoivat-hakukohderyhmat form)
:rajaavat-hakukohderyhmat (:rajaavat-hakukohderyhmat form)
:virkailija? virkailija?
:field-descriptor field-descriptor}))))
validators))))

; Use the code set version embedded in the form by default, try looking up from koodisto if not found.
(defn allowed-values [koodisto-cache koodisto-source options]
Expand Down Expand Up @@ -113,7 +116,7 @@
(belongs-to-correct-hakukohderyhma? field hakukohderyhmat)))

(defn build-results
[koodisto-cache has-applied answers-by-key form fields hakukohderyhmat virkailija?]
[koodisto-cache has-applied answers-by-key form fields flattened-form-fields hakukohderyhmat virkailija?]
(let [hakukohteet (-> answers-by-key :hakukohteet :value set)]
(loop [fields (map (fn [f] [nil false f]) fields)
results {}]
Expand Down Expand Up @@ -150,7 +153,7 @@
(recur (rest fields)
(if ((validator-keyword->fn (:child-validator field))
answers-by-key
(build-results koodisto-cache has-applied answers-by-key form (:children field) hakukohderyhmat virkailija?)
(build-results koodisto-cache has-applied answers-by-key form (:children field) flattened-form-fields hakukohderyhmat virkailija?)
(:children field))
results
(->> (:children field)
Expand Down Expand Up @@ -204,7 +207,7 @@
[idx (not (= value (:value option))) followup])
(rest fields))
(if (or (and (contains? allowed-values value)
(passed? has-applied form value (:validators field) answers-by-key field virkailija?))
(passed? has-applied form flattened-form-fields value (:validators field) answers-by-key field virkailija?))
(boolean (:per-hakukohde field)))
results
(assoc results id (get answers-by-key id)))))
Expand All @@ -218,34 +221,29 @@
[idx (not (contains? (set value) (:value option))) followup])
(rest fields))
(if (or (and (every? #(contains? allowed-values %) value)
(passed? has-applied form value (:validators field) answers-by-key field virkailija?))
(passed? has-applied form flattened-form-fields value (:validators field) answers-by-key field virkailija?))
(boolean (:per-hakukohde field)))
results
(assoc results id (get answers-by-key id)))))

(or (= :hakukohteet id)
(= "attachment" (:fieldType field)))
(recur (rest fields)
(if (passed? has-applied form value (:validators field) answers-by-key field virkailija?)
(if (passed? has-applied form flattened-form-fields value (:validators field) answers-by-key field virkailija?)
results
(assoc results id (get answers-by-key id))))

:else
(recur (rest fields)
(if (if (vector? value)
(and (not-empty value)
(every? #(passed? has-applied form % (:validators field) answers-by-key field virkailija?)
(every? #(passed? has-applied form flattened-form-fields % (:validators field) answers-by-key field virkailija?)
value))
(passed? has-applied form value (:validators field) answers-by-key field virkailija?))
(passed? has-applied form flattened-form-fields value (:validators field) answers-by-key field virkailija?))
results
(assoc results id (get answers-by-key id))))))
results))))

(defn build-failed-results [answers-by-key failed-results]
(merge-with merge
(select-keys answers-by-key (keys failed-results))
failed-results))

(defn- validate-meta-fields [application]
(when (not (contains? #{"fi" "sv" "en"} (:lang application)))
{:lang (:lang application)}))
Expand Down Expand Up @@ -303,7 +301,7 @@
extra-answers (extra-answers-not-in-original-form
(map (comp keyword :id) flattened-form-fields)
(keys answers-no-duplicates))
failed-results (build-results koodisto-cache has-applied answers-by-key form (:content form) applied-hakukohderyhmat virkailija?)
failed-results (build-results koodisto-cache has-applied answers-by-key form (:content form) flattened-form-fields applied-hakukohderyhmat virkailija?)
failed-meta-fields (validate-meta-fields application)
failed-per-hakukohde-fields (validate-per-hakukohde-fields answers-by-key application flattened-form-fields)
failed-haku-oid (:haku application)
Expand Down
Loading

0 comments on commit 9f5e1b6

Please sign in to comment.