Skip to content

Commit

Permalink
AE-2316: Add functionality to make the energiatodistus vanhentunut
Browse files Browse the repository at this point in the history
There is still a potential problem that now all of energiatodistukset
that have tila_id are repetedly set to tila_id 7 until their valvonta is
old enough
  • Loading branch information
solita-juhohaa committed Oct 24, 2024
1 parent 70de989 commit e8deb2b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
data linked to it."
(:require [clojure.java.jdbc :as jdbc]
[clojure.tools.logging :as log]
[clojure.set :as set]
[solita.etp.db :as db]
[solita.etp.exception :as exception]
[solita.etp.service.complete-energiatodistus :as complete-energiatodistus-service]
Expand Down Expand Up @@ -130,15 +131,26 @@
(destroy-energiatodistus-audit-data! db energiatodistus-id))
(log/info (str "Destroyed energiatodistus (id: " energiatodistus-id ")")))

(defn get-currently-expired-todistus-ids [db]
(->> (energiatodistus-destruction-db/select-expired-energiatodistus-ids db)
(defn get-currently-expired-todistus-ids [db {:keys [check-valvonta?]}]
(->> (energiatodistus-destruction-db/select-expired-energiatodistus-ids db {:check-valvonta check-valvonta?})
(map :energiatodistus-id)))

(defn- make-energiatodistus-vahnentunut
"There is an edge case where todistus might be expired but can not be
destroyed as there is still active valvonta on it. This function can be
used to make it 'vanhentunut' so that it can be handeled differently from"
[db energiatodistus-id]
(energiatodistus-destruction-db/make-energiatodistus-vanhentunut! db {:energiatodistus-id energiatodistus-id})
(log/info (str "Set energiatodistus (id: " energiatodistus-id ") tila to vanhentunut.")))

(defn destroy-expired-energiatodistukset! [db aws-s3-client whoami]
(when-not (and (rooli-service/system? whoami)
(= (:id whoami) (kayttaja-service/system-kayttaja :expiration)))
(exception/throw-forbidden! (str "Can not run destruction of expired todistukset as whoami (id: " (:id whoami) ") (rooli: " (:rooli whoami) ")")))
(log/info (str "Starting destruction of expired energiatodistukset."))
(let [expired-todistukset-ids (get-currently-expired-todistus-ids db)]
(run! #(destroy-expired-energiatodistus! db aws-s3-client %) expired-todistukset-ids))
(let [expired-todistukset-without-valvonta-ids (set (get-currently-expired-todistus-ids db {:check-vavonta? true}))
all-expired-todistukset-ids (set (get-currently-expired-todistus-ids db {:check-valvonta? false}))
expired-todistukset-with-valvonta-ids (set/difference all-expired-todistukset-ids expired-todistukset-without-valvonta-ids)]
(run! #(make-energiatodistus-vahnentunut db %) expired-todistukset-with-valvonta-ids)
(run! #(destroy-expired-energiatodistus! db aws-s3-client %) expired-todistukset-without-valvonta-ids))
(log/info (str "Destruction of expired energiatodistukset finished.")))
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
-- name: select-expired-energiatodistus-ids
select id as energiatodistus_id
from energiatodistus
left join (select max(create_time) as latest_toimenpide_create_time, energiatodistus_id
left join (select energiatodistus_id,
max(create_time) as latest_toimenpide_create_time,
max(publish_time) as latest_toimenpide_publish_time
from vo_toimenpide
group by energiatodistus_id) latest_toimenpide
on energiatodistus.id = latest_toimenpide.energiatodistus_id
where voimassaolo_paattymisaika < (now() at time zone 'Europe/Helsinki')::date - interval '1 days'
and (latest_toimenpide_create_time < (now() at time zone 'Europe/Helsinki')::date - interval '2 years'
or latest_toimenpide_create_time is null);
where voimassaolo_paattymisaika::date < (now() at time zone 'Europe/Helsinki')::date
and ((latest_toimenpide_create_time < (now() at time zone 'Europe/Helsinki')::date - interval '2 years'
or latest_toimenpide_create_time is null)
and (latest_toimenpide_publish_time < (now() at time zone 'Europe/Helsinki')::date - interval '2 years'
or latest_toimenpide_publish_time is null)
or :check-valvonta is false);

-- name: select-vo-toimenpiteet-by-energiatodistus-id
select id as vo_toimenpide_id
Expand Down Expand Up @@ -119,6 +124,11 @@ delete
from audit.energiatodistus
where id = :energiatodistus-id;

-- name: make-energiatodistus-vanhentunut!
update energiatodistus
set tila_id = 7 -- vanhentunut
where id = :energiatodistus-id;

-- name: anonymize-energiatodistus!
update energiatodistus
set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[solita.etp.service.viesti-test :as viesti-test]
[solita.etp.service.valvonta-oikeellisuus.asha :as vo-asha-service]
[solita.etp.service.kayttaja :as kayttaja-service]
[solita.etp.service.energiatodistus-tila :as tila-service]
[solita.etp.service.file :as file-service]
[solita.etp.service.liite :as liite-service]
[solita.etp.service.viesti :as viesti-service]
Expand All @@ -31,11 +32,18 @@
energiatodistus-id
energiatodistus))

(defn expire-energiatodistus!
(defn expire-energiatodistus [energiatodistus-id]
(jdbc/execute! ts/*db* ["update energiatodistus set voimassaolo_paattymisaika = CURRENT_DATE - INTERVAL '2 days' where id = ?" energiatodistus-id]))

(defn expire-valvonta [energiatodistus-id]
(jdbc/execute! ts/*db* ["update vo_toimenpide set create_time = CURRENT_DATE - INTERVAL '3 years' where energiatodistus_id = ?" energiatodistus-id])
(jdbc/execute! ts/*db* ["update vo_toimenpide set publish_time = CURRENT_DATE - INTERVAL '3 years' where energiatodistus_id = ?" energiatodistus-id]))

(defn expire-energiatodistus-and-its-valvonta
"Sets voimassaolo-paattymisaika to two days ago"
[energiatodistus-id]
(jdbc/execute! ts/*db* ["update energiatodistus set voimassaolo_paattymisaika = CURRENT_DATE - INTERVAL '2 days' where id = ?" energiatodistus-id])
(jdbc/execute! ts/*db* ["update vo_toimenpide set create_time = CURRENT_DATE - INTERVAL '3 years' where energiatodistus_id = ?" energiatodistus-id]))
(expire-energiatodistus energiatodistus-id)
(expire-valvonta energiatodistus-id))

(defn test-data-set []
(let [laatijat (laatija-test-data/generate-and-insert! 4)
Expand Down Expand Up @@ -72,7 +80,7 @@
(time/now))
laatija-id-3)

(expire-energiatodistus! energiatodistus-id-4)
(expire-energiatodistus-and-its-valvonta energiatodistus-id-4)


{:laatijat laatijat
Expand All @@ -82,7 +90,7 @@
(let [{:keys [energiatodistukset]} (test-data-set)
ids (-> energiatodistukset keys sort)
[id-1 id-2 id-3 id-4] ids
expired-ids (service/get-currently-expired-todistus-ids ts/*db*)]
expired-ids (service/get-currently-expired-todistus-ids ts/*db* {:check-valvonta? true})]
(t/testing "Todistus with expiration date set by signing it today should not be expired."
(t/is (nil? (some #{id-1} expired-ids))))
(t/testing "Todistus with expiration time at year 1970 should be expired."
Expand Down Expand Up @@ -116,11 +124,18 @@
(let [{:keys [energiatodistukset]} (test-data-set)
ids (-> energiatodistukset keys sort)
paakayttaja-id (kayttaja-test-data/insert-paakayttaja!)
[id-1 id-2 id-3 id-4] ids
add-valvonta #(partial add-valvonta-and-modify-create-time paakayttaja-id % (time/now))
_ (doall (map #(%) (map add-valvonta ids)))
expired-ids (service/get-currently-expired-todistus-ids ts/*db*)]
expired-ids (service/get-currently-expired-todistus-ids ts/*db* {:check-valvonta? true})
expired-ids-without-checking-valvonta (service/get-currently-expired-todistus-ids ts/*db* {:check-valvonta? false})]
(t/testing "None of the energiatodistukset should be expired as they have a recent valvonta"
(t/is (empty? expired-ids)))))
(t/is (empty? expired-ids)))
(t/testing "Valvonta should not affect the expiration as its checking is skipped"
(t/is (nil? (some #{id-1} expired-ids-without-checking-valvonta)))
(t/is (some #{id-2} expired-ids-without-checking-valvonta))
(t/is (nil? (some #{id-3} expired-ids-without-checking-valvonta)))
(t/is (some #{id-4} expired-ids-without-checking-valvonta)))))

(t/deftest get-currently-expired-todistus-ids-with-old-valvonta-test
(let [{:keys [energiatodistukset]} (test-data-set)
Expand All @@ -129,7 +144,7 @@
[id-1 id-2 id-3 id-4] ids
add-valvonta #(partial add-valvonta-and-modify-create-time paakayttaja-id % (.minus (time/now) (Duration/ofDays 735)))
_ (doall (map #(%) (map add-valvonta ids)))
expired-ids (service/get-currently-expired-todistus-ids ts/*db*)]
expired-ids (service/get-currently-expired-todistus-ids ts/*db* {:check-valvonta? true})]
(t/testing "Valvonta should not affect the expiration as it is older than two years"
(t/is (nil? (some #{id-1} expired-ids)))
(t/is (some #{id-2} expired-ids))
Expand All @@ -142,7 +157,7 @@
paakayttaja-id (kayttaja-test-data/insert-paakayttaja!)
add-valvonta #(partial add-valvonta-and-modify-create-time paakayttaja-id % (.minus (time/now) (Duration/ofDays 720)))
_ (doall (map #(%) (map add-valvonta ids)))
expired-ids (service/get-currently-expired-todistus-ids ts/*db*)]
expired-ids (service/get-currently-expired-todistus-ids ts/*db* {:check-valvonta? true})]
(t/testing "None of the energiatodistukset should be expired as they have a recent valvonta"
(t/is (empty? expired-ids)))))

Expand Down Expand Up @@ -191,7 +206,7 @@
(t/is (false? (file-service/file-exists? ts/*aws-s3-client* lang-mu-pdf-fi-key)))
(t/is (false? (file-service/file-exists? ts/*aws-s3-client* lang-mu-pdf-sv-key))))

(expire-energiatodistus! energiatodistus-id-fi)
(expire-energiatodistus-and-its-valvonta energiatodistus-id-fi)
(service/destroy-expired-energiatodistukset! ts/*db* ts/*aws-s3-client* system-expiration-user)

(t/testing "Finnish version PDF should not exist after deleting it."
Expand All @@ -217,7 +232,7 @@
(t/is (false? (file-service/file-exists? ts/*aws-s3-client* lang-mu-pdf-fi-key)))
(t/is (false? (file-service/file-exists? ts/*aws-s3-client* lang-mu-pdf-sv-key))))

(expire-energiatodistus! energiatodistus-id-sv)
(expire-energiatodistus-and-its-valvonta energiatodistus-id-sv)
(service/destroy-expired-energiatodistukset! ts/*db* ts/*aws-s3-client* system-expiration-user)

(t/testing "Swedish version PDF should not exist after deleting it."
Expand All @@ -243,7 +258,7 @@
(t/is (true? (file-service/file-exists? ts/*aws-s3-client* lang-mu-pdf-fi-key)))
(t/is (true? (file-service/file-exists? ts/*aws-s3-client* lang-mu-pdf-sv-key))))

(expire-energiatodistus! energiatodistus-id-mu)
(expire-energiatodistus-and-its-valvonta energiatodistus-id-mu)
(service/destroy-expired-energiatodistukset! ts/*db* ts/*aws-s3-client* system-expiration-user)

(t/testing "Multilingual version PDFs should not exist after deleting it."
Expand Down Expand Up @@ -287,7 +302,7 @@
ids (-> energiatodistukset keys sort)
[id-1] ids
get-et-1 #(first (select-energiatodistus id-1))]
(expire-energiatodistus! id-1)
(expire-energiatodistus-and-its-valvonta id-1)
(service/destroy-expired-energiatodistukset! ts/*db* ts/*aws-s3-client* system-expiration-user)
(t/testing "The values are anonymized."
(t/is (empty? (->> (get-et-1)
Expand Down Expand Up @@ -318,7 +333,8 @@
laatija-id (-> (laatija-test-data/generate-and-insert! 1) keys first)
energiatodistus-add (energiatodistus-test-data/generate-add 2018 true)
[energiatodistus-id-1
energiatodistus-id-2] (energiatodistus-test-data/insert! [energiatodistus-add energiatodistus-add] laatija-id)
energiatodistus-id-2
energiatodistus-id-3] (energiatodistus-test-data/insert! (vec (repeat 3 energiatodistus-add)) laatija-id)
select-toimenpiteet #(jdbc/query ts/*db* ["select * from vo_toimenpide where energiatodistus_id = ?" %])
select-notes #(jdbc/query ts/*db* ["select * from vo_note where energiatodistus_id = ?" %])
select-tiedoksi #(jdbc/query ts/*db* ["select * from vo_tiedoksi where toimenpide_id in (select id from vo_toimenpide where energiatodistus_id = ?)" %])
Expand All @@ -328,6 +344,8 @@
select-notes-audit #(jdbc/query ts/*db* ["select * from audit.vo_note where energiatodistus_id = ?" %])
;; tiedoksi and virheet do not have audit tables.

select-tila-id #(jdbc/query ts/*db* ["select tila_id from energiatodistus where id = ?" %])

get-vo-toimenpiteet #(select-toimenpiteet %)
get-vo-notes #(select-notes %)
get-vo-tiedoksi #(select-tiedoksi %)
Expand Down Expand Up @@ -358,7 +376,11 @@
vo-toimenpide-2-id (:id (valvonta-oikeellisuus-service/add-toimenpide! (ts/db-user paakayttaja-id)
ts/*aws-s3-client*
(test-whoami/paakayttaja paakayttaja-id)
energiatodistus-id-2 audit-report))]
energiatodistus-id-2 audit-report))
vo-toimenpide-3-id (:id (valvonta-oikeellisuus-service/add-toimenpide! (ts/db-user paakayttaja-id)
ts/*aws-s3-client*
(test-whoami/paakayttaja paakayttaja-id)
energiatodistus-id-3 audit-report))]

(valvonta-oikeellisuus-service/add-note! ts/*db* energiatodistus-id-1 (:description vo_note))

Expand All @@ -381,6 +403,16 @@
energiatodistus-id-2
vo-toimenpide-2-id
{:inhibit-email? true})
(valvonta-oikeellisuus-service/add-note! ts/*db* energiatodistus-id-3 (:description vo_note))
(valvonta-oikeellisuus-service/update-toimenpide! (ts/db-user paakayttaja-id)
(test-whoami/paakayttaja paakayttaja-id)
energiatodistus-id-3 vo-toimenpide-3-id {:template-id 1})
(valvonta-oikeellisuus-service/publish-toimenpide! ts/*db*
ts/*aws-s3-client*
(test-whoami/paakayttaja paakayttaja-id)
energiatodistus-id-3
vo-toimenpide-3-id
{:inhibit-email? true})

(t/testing "There is some toimenpide before deletion."
(t/is (not (empty? (get-vo-toimenpiteet energiatodistus-id-1)))))
Expand All @@ -403,9 +435,15 @@
(t/is (true? (file-service/file-exists? ts/*aws-s3-client* file-key-et-1)))
(t/is (true? (file-service/file-exists? ts/*aws-s3-client* file-key-et-2)))))

(expire-energiatodistus! energiatodistus-id-1)
;; Keep the valvonta in energiatodistus 3
(expire-energiatodistus energiatodistus-id-3)
(expire-energiatodistus-and-its-valvonta energiatodistus-id-1)
(service/destroy-expired-energiatodistukset! ts/*db* ts/*aws-s3-client* system-expiration-user)

(t/testing "Energiatodistus 3 is 'vanhentunut' after running the expiration as it has an ongoing valvonta"
(let [tila-id (:tila_id (first (select-tila-id energiatodistus-id-3)))]
(t/is (tila-service/expired? {:tila-id tila-id}))))

(t/testing "Energiatodistus 1 valvonta documents are destroyed and energiatodistus 2 are not"
(let [file-key-et-1 (vo-asha-service/file-path energiatodistus-id-1 vo-toimenpide-1-id)
file-key-et-2 (vo-asha-service/file-path energiatodistus-id-2 vo-toimenpide-2-id)]
Expand Down Expand Up @@ -488,7 +526,7 @@
(t/is (not-empty (select-liitteet-audit energiatodistus-id-2))))

;; Destroy et-1 liiteet
(expire-energiatodistus! energiatodistus-id-1)
(expire-energiatodistus-and-its-valvonta energiatodistus-id-1)
(service/destroy-expired-energiatodistukset! ts/*db* ts/*aws-s3-client* system-expiration-user)

(t/testing "The liitteet for energiatodistus-1 are deleted but exist for energiatodistus-2"
Expand Down Expand Up @@ -594,7 +632,7 @@
(t/is (not (empty? (select-viesti-liite-audit viestiketju-1-id))))
(t/is (not (empty? (select-viesti-liite-audit viestiketju-2-id)))))

(expire-energiatodistus! energiatodistus-id-1)
(expire-energiatodistus-and-its-valvonta energiatodistus-id-1)
(service/destroy-expired-energiatodistukset! ts/*db* ts/*aws-s3-client* system-expiration-user)

(t/testing "Only viestiketju 2 exists after deletion"
Expand Down

0 comments on commit e8deb2b

Please sign in to comment.