Skip to content

Commit

Permalink
🐛 Legger inn serial id i annullering_arsak
Browse files Browse the repository at this point in the history
- primary key på vedtaksperiode_id lagrer bare 1 rad naturligvis
  • Loading branch information
jonashas committed Aug 9, 2024
1 parent 0b4b5d4 commit e390482
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/no/nav/helse/Annullering.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ data class Annullering(
@Language("PostgreSQL")
val årsakStatement = """
INSERT INTO annullering_arsak(
vedtaksperiode_id,
arsak,
key
key,
vedtaksperiode_id
) VALUES (?, ?, ?);
"""
annullering.arsaker.forEach { arsak ->
run(
queryOf(
årsakStatement,
annullering.vedtaksperiodeId,
arsak.arsak,
arsak.key
arsak.key,
annullering.vedtaksperiodeId
).asUpdate
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DROP TABLE annullering_arsak;
CREATE TABLE annullering_arsak
(
id SERIAL PRIMARY KEY,
arsak TEXT NOT NULL,
key VARCHAR(32) NOT NULL,
vedtaksperiode_id VARCHAR REFERENCES annullering (id) NOT NULL
);
26 changes: 10 additions & 16 deletions src/test/kotlin/no/nav/helse/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,35 @@ fun DataSource.annulleringer(): List<Annullering> {
this.run(
queryOf(
"""
SELECT a.saksbehandler, a.id, a.begrunnelser, a.kommentar, a.opprettet, aa.arsak, aa.key
SELECT a.saksbehandler, a.id, a.begrunnelser, a.kommentar, a.opprettet
FROM annullering a
LEFT JOIN annullering_arsak aa ON aa.vedtaksperiode_id = a.id
ORDER BY a.opprettet DESC
"""
)
.map {
val årsak = this.run(
val årsaker = this.run(
queryOf("""
SELECT arsak, key
FROM annullering_arsak
"""
).map {årsak -> årsak.annulleringÅrsak() }.asList)
WHERE vedtaksperiode_id = :annulleringId
""", mapOf(
"annulleringId" to it.string("id")
)).map {årsak -> årsak.annulleringÅrsak() }.asList)

it.annullering(årsak)
it.annullering(årsaker)
}.asList
)
}
}

fun Row.annullering(årsak: List<AnnulleringArsak>?) =
fun Row.annullering(årsaker: List<AnnulleringArsak>?) =
Annullering(
saksbehandler = uuid("saksbehandler"),
vedtaksperiodeId = UUID.fromString(string("id")),
begrunnelser = stringOrNull("arsak")?.let { listOf(string("arsak")) } ?: stringList("begrunnelser"),
begrunnelser = årsaker?.takeUnless { it.isEmpty() }?.let { it.map { årsak -> årsak.arsak } } ?: stringList("begrunnelser"),
kommentar = stringOrNull("kommentar"),
opprettet = localDateTime("opprettet"),
arsaker = årsak?.takeUnless { årsak.isEmpty() }?.let {
listOf(
AnnulleringArsak(
arsak = string("arsak"),
key = string("key")
)
)
} ?: emptyList()
arsaker = årsaker?.takeUnless { it.isEmpty() } ?: emptyList()
)

fun Row.annulleringÅrsak(): AnnulleringArsak =
Expand Down

0 comments on commit e390482

Please sign in to comment.