@@ -6,7 +6,12 @@ import no.nav.syfo.dialogmote.api.domain.DialogmoteStatusEndringDTO
6
6
import no.nav.syfo.dialogmote.database.domain.PMoteStatusEndret
7
7
import no.nav.syfo.dialogmote.domain.DialogmoteStatus
8
8
import no.nav.syfo.domain.PersonIdent
9
+ import java.sql.Connection
9
10
import java.sql.ResultSet
11
+ import java.sql.SQLException
12
+ import java.sql.Timestamp
13
+ import java.time.Instant
14
+ import java.time.LocalDate
10
15
import java.util.*
11
16
12
17
class MoteStatusEndretRepository (private val database : DatabaseInterface ) {
@@ -30,6 +35,64 @@ class MoteStatusEndretRepository(private val database: DatabaseInterface) {
30
35
}
31
36
}
32
37
38
+ fun getMoteStatusEndretNotPublished (): List <PMoteStatusEndret > {
39
+ return database.connection.use { connection ->
40
+ connection.prepareStatement(GET_MOTE_STATUS_ENDRET_NOT_PUBLISHED ).use {
41
+ it.executeQuery().toList { toPMoteStatusEndret() }
42
+ }
43
+ }
44
+ }
45
+
46
+ fun updateMoteStatusEndretPublishedAt (moteStatusEndretId : Int ) {
47
+ val now = Timestamp .from(Instant .now())
48
+ database.connection.use { connection ->
49
+ connection.prepareStatement(UPDATE_MOTE_STATUS_ENDRET_PUBLISHED_AT ).use {
50
+ it.setTimestamp(1 , now)
51
+ it.setTimestamp(2 , now)
52
+ it.setInt(3 , moteStatusEndretId)
53
+ it.execute()
54
+ }
55
+ connection.commit()
56
+ }
57
+ }
58
+
59
+ fun createMoteStatusEndring (
60
+ connection : Connection ,
61
+ commit : Boolean = true,
62
+ moteId : Int ,
63
+ opprettetAv : String ,
64
+ status : DialogmoteStatus ,
65
+ tilfelleStart : LocalDate ? ,
66
+ isBehandlerMotedeltaker : Boolean ,
67
+ ): Pair <Int , UUID > {
68
+ val now = Timestamp .from(Instant .now())
69
+ val startDate = tilfelleStart ? : LocalDate .EPOCH
70
+
71
+ val moteStatusEndringUuid = UUID .randomUUID()
72
+
73
+ val moteStatusEndringIdList = connection.prepareStatement(CREATE_MOTE_STATUS_ENDRING ).use {
74
+ it.setString(1 , moteStatusEndringUuid.toString())
75
+ it.setTimestamp(2 , now)
76
+ it.setTimestamp(3 , now)
77
+ it.setInt(4 , moteId)
78
+ it.setString(5 , status.name)
79
+ it.setString(6 , opprettetAv)
80
+ it.setTimestamp(7 , Timestamp .valueOf(startDate.atStartOfDay()))
81
+ it.setBoolean(8 , isBehandlerMotedeltaker)
82
+ it.executeQuery().toList { getInt(" id" ) }
83
+ }
84
+
85
+ if (moteStatusEndringIdList.size != 1 ) {
86
+ throw SQLException (" Creating MoteStatusEndring failed, no rows affected." )
87
+ }
88
+
89
+ if (commit) {
90
+ connection.commit()
91
+ }
92
+
93
+ return Pair (moteStatusEndringIdList.first(), moteStatusEndringUuid)
94
+ }
95
+
33
96
companion object {
34
97
private const val GET_MOTE_STATUS_ENDRINGER =
35
98
"""
@@ -42,6 +105,36 @@ class MoteStatusEndretRepository(private val database: DatabaseInterface) {
42
105
WHERE mda.personident = ?
43
106
ORDER BY mse.created_at DESC
44
107
"""
108
+
109
+ private const val CREATE_MOTE_STATUS_ENDRING =
110
+ """
111
+ INSERT INTO MOTE_STATUS_ENDRET (
112
+ id,
113
+ uuid,
114
+ created_at,
115
+ updated_at,
116
+ mote_id,
117
+ status,
118
+ opprettet_av,
119
+ tilfelle_start,
120
+ motedeltaker_behandler
121
+ ) VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id
122
+ """
123
+
124
+ private const val GET_MOTE_STATUS_ENDRET_NOT_PUBLISHED =
125
+ """
126
+ SELECT *
127
+ FROM MOTE_STATUS_ENDRET
128
+ WHERE published_at IS NULL
129
+ ORDER BY created_at ASC LIMIT 100
130
+ """
131
+
132
+ private const val UPDATE_MOTE_STATUS_ENDRET_PUBLISHED_AT =
133
+ """
134
+ UPDATE MOTE_STATUS_ENDRET
135
+ SET published_at = ?, updated_at = ?
136
+ WHERE id = ?
137
+ """
45
138
}
46
139
}
47
140
0 commit comments