Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EH-1757: Fix resend functionality #664

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SELECT * FROM hoksit h
WHERE h.created_at >= ?
AND h.created_at <= ?
WHERE h.ensikertainen_hyvaksyminen >= ?
AND h.ensikertainen_hyvaksyminen <= ?
AND h.deleted_at IS NULL
AND h.tuva_opiskeluoikeus_oid IS NULL
AND NOT EXISTS (SELECT 1 FROM hankittavat_koulutuksen_osat hko
Expand Down
12 changes: 6 additions & 6 deletions src/oph/ehoks/db/db_operations/hoks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -426,17 +426,17 @@
[queries/select-hoksit-finished-between from to]
{:row-fn hoks-from-sql}))

(defn select-non-tuva-hoksit-created-between
"Hakee tietokannasta ne HOKSit, jotka on luotu annettujen ajankohtien
välillä ja jotka eivät ole TUVA-HOKSeja tai TUVA-HOKSien kanssa
rinnakkaisia ammatillisia HOKSeja."
(defn select-non-tuva-hoksit-started-between
"Hakee tietokannasta ne HOKSit, jotka on merkitty alkaneiksi annettujen
ajankohtien välillä ja jotka eivät ole TUVA-HOKSeja tai TUVA-HOKSien
kanssa rinnakkaisia ammatillisia HOKSeja."
[from to]
(db-ops/query
[queries/select-non-tuva-hoksit-created-between from to]
[queries/select-non-tuva-hoksit-started-between from to]
{:row-fn hoks-from-sql}))

(defn select-non-tuva-hoksit-finished-between
"Hakee tietokannasta ne HOKSit, jotka on merkattu valmiiksi annettujen
"Hakee tietokannasta ne HOKSit, jotka on merkitty valmiiksi annettujen
ajankohtien välillä ja jotka eivät ole TUVA-HOKSeja tai TUVA-HOKSien kanssa
rinnakkaisia ammatillisia HOKSeja."
[from to]
Expand Down
4 changes: 2 additions & 2 deletions src/oph/ehoks/db/queries.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
(defq select-hoksit-created-between "hoksit/select_hoksit_created_between.sql")
(defq select-hoksit-finished-between
"hoksit/select_hoksit_finished_between.sql")
(defq select-non-tuva-hoksit-created-between
"hoksit/select_non_tuva_hoksit_created_between.sql")
(defq select-non-tuva-hoksit-started-between
"hoksit/select_non_tuva_hoksit_started_between.sql")
(defq select-non-tuva-hoksit-finished-between
"hoksit/select_non_tuva_hoksit_finished_between.sql")
(defq select-hoks-oppijat-without-index
Expand Down
4 changes: 2 additions & 2 deletions src/oph/ehoks/heratepalvelu.clj
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
(log/infof
"Sending %d (limit %d) hoksit between %s and %s"
(+ (count aloittaneet) (count paattyneet)) (* 2 limit) start end)
(op/initiate-every-needed! :aloituskysely aloittaneet {:resend? true})
(op/initiate-every-needed! :paattokysely paattyneet {:resend? true})
(op/initiate-every-needed! :aloituskysely aloittaneet)
(op/initiate-every-needed! :paattokysely paattyneet)
(concat aloittaneet paattyneet)))

(defn set-aloitusherate-kasitelty
Expand Down
29 changes: 18 additions & 11 deletions src/oph/ehoks/heratepalvelu/herate_handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
(restful/ok (count periods))))

(c-api/GET "/kasittelemattomat-heratteet" []
:summary "HOKSit, joilla on käsittelemättömiä herätteitä"
:summary
"lähettää uudestaan herätepalveluun herätteet HOKSeista,
joilla on käsittelemättömiä herätteitä. Herätepalvelu
kutsuu tätä säännöllisesti."
:query-params [start :- LocalDate
end :- LocalDate
limit :- (s/maybe s/Int)]
Expand All @@ -63,26 +66,30 @@
(response/no-content))

(c-api/POST "/hoksit/resend-aloitusherate" request
:summary "Lähettää uudet aloituskyselyherätteet herätepalveluun"
:summary
"Lähettää uudet aloituskyselyherätteet herätepalveluun.
Herätepalvelu kutsuu tätä viikon välein saadakseen
varmistettua kahden viime viikon herätteet (jotka lähetetään
uudestaan)."
:header-params [caller-id :- s/Str]
:query-params [from :- LocalDate
to :- LocalDate]
:return (restful/response {:count s/Int})
(let [hoksit (db-hoks/select-non-tuva-hoksit-created-between from to)
count (op/initiate-every-needed!
:aloituskysely hoksit {:resend? true})]
(restful/ok {:count count})))
(let [result (op/reinitiate-hoksit-between! :aloituskysely from to)]
(restful/ok {:count result})))

(c-api/POST "/hoksit/resend-paattoherate" request
:summary "Lähettää uudet päättökyselyherätteet herätepalveluun"
:summary
"Lähettää uudet päättökyselyherätteet herätepalveluun.
Herätepalvelu kutsuu tätä viikon välein saadakseen
varmistettua kahden viime viikon herätteet (jotka lähetetään
uudestaan)."
:header-params [caller-id :- s/Str]
:query-params [from :- LocalDate
to :- LocalDate]
:return (restful/response {:count s/Int})
(let [hoksit (db-hoks/select-non-tuva-hoksit-finished-between from to)
count (op/initiate-every-needed!
:paattokysely hoksit {:resend? true})]
(restful/ok {:count count})))
(let [result (op/reinitiate-hoksit-between! :paattokysely from to)]
(restful/ok {:count result})))

(c-api/POST "/opiskeluoikeus-update" request
:summary "Päivittää aktiivisten hoksien opiskeluoikeudet Koskesta"
Expand Down
106 changes: 46 additions & 60 deletions src/oph/ehoks/palaute/opiskelija.clj
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,7 @@
"Initiates opiskelijapalautekysely (`:aloituskysely` or `:paattokysely`).
Currently, stores kysely data to eHOKS DB `palautteet` table and also sends
the herate to AWS SQS for Herätepalvelu to process. Returns `true` if kysely
was successfully initiated, `nil` or `false` otherwise.
Supported options in `opts`:
`:resend?` If set to true, don't check if kysely is already
intiated, i.e., resend herate straight to Herätepalvelu. Also skip
the insertion to eHOKS `palautteet` table. Without this flag, the
functionality to resend heratteet to Herätepalvelu wouldn't work.
This should be removed once Herätepalvelu functionality has been
fully migrated to eHOKS."
was successfully initiated, `nil` or `false` otherwise."
[{:keys [tx hoks opiskeluoikeus state reason lisatiedot] :as ctx} kysely-type]
(let [heratepvm (get hoks (herate-date-basis kysely-type))
target-kasittelytila (not= state :odottaa-kasittelya)
Expand Down Expand Up @@ -132,62 +125,55 @@
:alkupvm (str heratepvm)}))))

(defn initiate-if-needed!
"Sends heräte data required for opiskelijapalautekysely (`:aloituskysely` or
`:paattokysely`) to appropriate DynamoDB table of Herätepalvelu if no check is
preventing the sending. Returns `true` if kysely was successfully sent.

Supported options in `opts`:
`:resend?` If set to true, don't check if kysely is already
intiated, i.e., resend herate straight to Herätepalvelu. Also skip
the insertion to eHOKS `palautteet` table. Without this flag, the
functionality to resend heratteet to Herätepalvelu wouldn't work.
This should be removed once Herätepalvelu functionality has been
fully migrated to eHOKS."
([ctx kysely-type] (initiate-if-needed! ctx kysely-type nil))
([{:keys [hoks opiskeluoikeus] :as ctx} kysely-type opts]
(jdbc/with-db-transaction
[tx db/spec]
(let [ctx (assoc
ctx
:tapahtumatyyppi :hoks-tallennus
:tx tx
:koulutustoimija (palaute/koulutustoimija-oid!
opiskeluoikeus)
:existing-palaute (when-not (:resend? opts)
(existing-palaute! tx ctx kysely-type)))
[state field reason] (initial-palaute-state-and-reason
ctx kysely-type)
lisatiedot (map-vals str (select-keys hoks [field]))]
(log/info "Initial state for" kysely-type "for HOKS" (:id hoks)
"will be" (or state :ei-luoda-ollenkaan)
"because of" reason "in" field)
(when state
(initiate!
(assoc ctx :state state :reason reason :lisatiedot lisatiedot)
kysely-type))
state))))
"Saves heräte data required for opiskelijapalautekysely
(`:aloituskysely` or `:paattokysely`) to database and sends it to
appropriate DynamoDB table of Herätepalvelu if no check is preventing
the sending. Returns the initial state of kysely if it was created,
`nil` otherwise."
[{:keys [hoks opiskeluoikeus] :as ctx} kysely-type]
(jdbc/with-db-transaction
[tx db/spec]
(let [ctx (assoc
ctx
:tapahtumatyyppi :hoks-tallennus
:tx tx
:koulutustoimija (palaute/koulutustoimija-oid! opiskeluoikeus)
:existing-palaute (existing-palaute! tx ctx kysely-type))
[state field reason]
(initial-palaute-state-and-reason ctx kysely-type)
lisatiedot (map-vals str (select-keys hoks [field]))]
(log/info "Initial state for" kysely-type "for HOKS" (:id hoks)
"will be" (or state :ei-luoda-ollenkaan)
"because of" reason "in" field)
(when state
(initiate!
(assoc ctx :state state :reason reason :lisatiedot lisatiedot)
kysely-type))
state)))

(defn initiate-every-needed!
"Effectively the same as running `initiate-if-needed!` for multiple HOKSes,
but also returns a count of the number of kyselys initiated.
but also returns a count of the number of kyselys initiated."
[kysely-type hoksit]
(count (filter #(= :odottaa-kasittelya
(initiate-if-needed!
{:hoks %
:opiskeluoikeus (koski/get-opiskeluoikeus!
(:opiskeluoikeus-oid %))}
kysely-type))
hoksit)))

Supported options in `opts`:
`:resend?` If set to true, don't check if kysely is already
intiated, i.e., resend herate straight to Herätepalvelu. Also skip
the insertion to eHOKS `palautteet` table. Without this flag, the
functionality to resend heratteet to Herätepalvelu wouldn't work.
This should be removed once Herätepalvelu functionality has been
fully migrated to eHOKS."
([kysely-type hoksit] (initiate-every-needed! kysely-type hoksit nil))
([kysely-type hoksit opts]
(count (filter #(= :odottaa-kasittelya
(initiate-if-needed!
{:hoks %
:opiskeluoikeus (koski/get-opiskeluoikeus!
(:opiskeluoikeus-oid %))}
kysely-type
opts))
hoksit))))
(defn reinitiate-hoksit-between!
"Hakee ei-TUVA-HOKSit tietyllä aikavälillä ja päivittää niiden
palautteet ja lähettää SQS-viestit samaan tapaan kuin HOKSit olisi
juuri tallennettu."
[kyselytyyppi from to]
(log/info "Reinitiating" kyselytyyppi "for HOKSit between" from "and" to)
(let [fetcher
(case kyselytyyppi
:aloituskysely db-hoks/select-non-tuva-hoksit-started-between
:paattokysely db-hoks/select-non-tuva-hoksit-finished-between)]
(initiate-every-needed! kyselytyyppi (fetcher from to))))

(defn create-arvo-kyselylinkki!
"For the given palaute, make Arvo call for creating its kyselylinkki
Expand Down
6 changes: 2 additions & 4 deletions src/oph/ehoks/palaute/tyoelama.clj
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@
(palaute/get-by-hoks-id-and-yksiloiva-tunniste!
tx {:hoks-id (:id hoks)
:yksiloiva-tunniste (:yksiloiva-tunniste jakso)}))
[state field reason]
(initial-palaute-state-and-reason ctx)
[state field reason] (initial-palaute-state-and-reason ctx)
lisatiedot (map-vals str (select-keys (merge jakso hoks) [field]))]
(log/info "Initial state for jakso" (:yksiloiva-tunniste jakso)
"of HOKS" (:id hoks) "will be"
(or state :ei-luoda-ollenkaan)
"of HOKS" (:id hoks) "will be" (or state :ei-luoda-ollenkaan)
"because of" reason "in" field)
(when state
(->> (build! ctx state)
Expand Down
24 changes: 10 additions & 14 deletions src/oph/ehoks/virkailija/system_handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@
{:hoks hoks
:opiskeluoikeus (koski/get-existing-opiskeluoikeus!
(:opiskeluoikeus-oid hoks))}
:aloituskysely
{:resend? true}))
:aloituskysely))
(response/no-content)
(response/bad-request
{:error (str "Either `osaamisen-hankkimisen-tarve` is `false` or "
Expand All @@ -277,8 +276,7 @@
{:hoks hoks
:opiskeluoikeus (koski/get-existing-opiskeluoikeus!
(:opiskeluoikeus-oid hoks))}
:paattokysely
{:resend? true}))
:paattokysely))
(response/no-content)
(response/bad-request
{:error (str "Either `osaamisen-hankkimisen-tarve` is `false`, "
Expand All @@ -290,29 +288,27 @@
::audit/target {:hoks-id hoks-id}))

(c-api/POST "/hoks/resend-aloitusherate" request
:summary "Lähettää uudet aloituskyselyherätteet herätepalveluun"
:summary "Lähettää uudet aloituskyselyherätteet herätepalveluun.
Tätä kutsutaan käsin käyttöliittymän ylläpitonäkymästä."
:header-params [caller-id :- s/Str]
:query-params [from :- LocalDate
to :- LocalDate]
:return {:count s/Int}
(let [hoksit (db-hoks/select-non-tuva-hoksit-created-between from to)
count (op/initiate-every-needed!
:aloituskysely hoksit {:resend? true})]
(assoc (restful/ok {:count count})
(let [result (op/reinitiate-hoksit-between! :aloituskysely from to)]
(assoc (restful/ok {:count result})
::audit/operation :system/resend-aloitusheratteet
::audit/target {:hoksit-from from
:hoksit-to to})))

(c-api/POST "/hoks/resend-paattoherate" request
:summary "Lähettää uudet päättökyselyherätteet herätepalveluun"
:summary "Lähettää uudet päättökyselyherätteet herätepalveluun.
Tätä kutsutaan käsin käyttöliittymän ylläpitonäkymästä."
:header-params [caller-id :- s/Str]
:query-params [from :- LocalDate
to :- LocalDate]
:return {:count s/Int}
(let [hoksit (db-hoks/select-non-tuva-hoksit-finished-between from to)
count (op/initiate-every-needed!
:paattokysely hoksit {:resend? true})]
(assoc (restful/ok {:count count})
(let [result (op/reinitiate-hoksit-between! :paattokysely from to)]
(assoc (restful/ok {:count result})
::audit/operation :system/resend-paattoheratteet
::audit/target {:hoksit-from from
:hoksit-to to})))))
24 changes: 21 additions & 3 deletions test/oph/ehoks/hoks/hoks_save_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
[oph.ehoks.hoks.schema :as hoks-schema]
[oph.ehoks.opiskeluoikeus-test :as oo-test]
[oph.ehoks.palaute :as palaute]
[oph.ehoks.palaute.opiskelija :as opalaute]
[oph.ehoks.palaute.tapahtuma :as tapahtumat]
[oph.ehoks.test-utils :as test-utils :refer [eq]]
[oph.ehoks.utils.date :as date]
Expand Down Expand Up @@ -704,11 +705,10 @@
"osaamisen-hankkimisen-tarve")
(let [sqs-call-counter (atom 0)]
(with-redefs [sqs/send-amis-palaute-message (mock-call sqs-call-counter)
k/get-opiskeluoikeus! (fn [oid] oo-test/opiskeluoikeus-1)
organisaatio/get-organisaatio!
organisaatio-test/mock-get-organisaatio!
date/now (constantly (LocalDate/of 2018 7 1))]
; (let [saved-hoks (hoks-handler/save-hoks-and-initiate-palautteet!
; hoks-data oo-test/opiskeluoikeus-6)
(let [opiskeluoikeus oo-test/opiskeluoikeus-1
saved-hoks (hoks-handler/save-hoks-and-initiate-all-palautteet!
{:hoks hoks-data
Expand All @@ -732,7 +732,25 @@
#{["odottaa_kasittelya" "aloittaneet" (LocalDate/of 2021 1 1)]
["odottaa_kasittelya" "valmistuneet" (LocalDate/of 2022 12 15)]
["lahetetty" "aloittaneet" (LocalDate/of 2019 3 18)]})
(is (= @sqs-call-counter 3)))))))
(is (= @sqs-call-counter 3))
(is (= 1 (opalaute/reinitiate-hoksit-between!
:aloituskysely
(LocalDate/of 2021 1 1)
(LocalDate/of 2021 6 1))))
(is (= @sqs-call-counter 4))
(is (= 1 (opalaute/reinitiate-hoksit-between!
:paattokysely
(LocalDate/of 2022 12 1)
(LocalDate/of 2022 12 30))))
(is (= @sqs-call-counter 5))
(eq (set (map (juxt :tila :kyselytyyppi :heratepvm)
(palaute/get-by-hoks-id-and-kyselytyypit!
db/spec
{:hoks-id (:id saved-hoks)
:kyselytyypit ["aloittaneet" "valmistuneet"]})))
#{["odottaa_kasittelya" "aloittaneet" (LocalDate/of 2021 1 1)]
["odottaa_kasittelya" "valmistuneet" (LocalDate/of 2022 12 15)]
["lahetetty" "aloittaneet" (LocalDate/of 2019 3 18)]}))))))

(deftest do-not-form-opiskelijapalaute-in-hoks-update
(testing (str "update: does not form opiskelijapalaute when "
Expand Down
6 changes: 0 additions & 6 deletions test/oph/ehoks/palaute/opiskelija_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,6 @@
(db-ops/query ["UPDATE palautteet SET tila='lahetetty'
WHERE hoks_id=12345 RETURNING *"])
(are [kysely-type] (nil? (op/initiate-if-needed! ctx kysely-type))
:aloituskysely :paattokysely))

(testing "sends kysely info to AWS SQS when `:resend?` option is given."
(are [kysely-type] (= :odottaa-kasittelya
(op/initiate-if-needed!
ctx kysely-type {:resend? true}))
:aloituskysely :paattokysely))))))

(deftest test-create-arvo-kyselylinkki!
Expand Down
Loading