summaryrefslogtreecommitdiff
path: root/bank/src/main/kotlin/tech/libeufin/bank/db/WithdrawalDAO.kt
blob: 4b069339ccb4dc7ff85718512b41bcabb7f85b1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2023 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

package tech.libeufin.bank.db

import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import tech.libeufin.bank.*
import tech.libeufin.common.*
import java.time.Instant
import java.util.*

/** Data access logic for withdrawal operations */
class WithdrawalDAO(private val db: Database) {
    /** Result status of withdrawal operation creation */
    enum class WithdrawalCreationResult {
        Success,
        UnknownAccount,
        AccountIsExchange,
        BalanceInsufficient
    }

    /** Create a new withdrawal operation */
    suspend fun create(
        login: String,
        uuid: UUID,
        amount: TalerAmount,
        now: Instant
    ): WithdrawalCreationResult = db.serializable { conn ->
        val stmt = conn.prepareStatement("""
            SELECT
                out_account_not_found,
                out_account_is_exchange,
                out_balance_insufficient
            FROM create_taler_withdrawal(?, ?, (?,?)::taler_amount, ?);
        """)
        stmt.setString(1, login)
        stmt.setObject(2, uuid)
        stmt.setLong(3, amount.value)
        stmt.setInt(4, amount.frac)
        stmt.setLong(5, now.micros())
        stmt.executeQuery().use {
            when {
                !it.next() ->
                    throw internalServerError("No result from DB procedure create_taler_withdrawal")
                it.getBoolean("out_account_not_found") -> WithdrawalCreationResult.UnknownAccount
                it.getBoolean("out_account_is_exchange") -> WithdrawalCreationResult.AccountIsExchange
                it.getBoolean("out_balance_insufficient") -> WithdrawalCreationResult.BalanceInsufficient
                else -> WithdrawalCreationResult.Success
            }
        }
    }

    /** Abort withdrawal operation [uuid] */
    suspend fun abort(uuid: UUID): AbortResult = db.serializable { conn ->
        val stmt = conn.prepareStatement("""
            SELECT
                out_no_op,
                out_already_confirmed
            FROM abort_taler_withdrawal(?)
        """)
        stmt.setObject(1, uuid)
        stmt.executeQuery().use {
            when {
                !it.next() ->
                    throw internalServerError("No result from DB procedure abort_taler_withdrawal")
                it.getBoolean("out_no_op") -> AbortResult.UnknownOperation
                it.getBoolean("out_already_confirmed") -> AbortResult.AlreadyConfirmed
                else -> AbortResult.Success
            }
        }
    }

    /** Result withdrawal operation selection */
    sealed interface WithdrawalSelectionResult {
        data class Success(val status: WithdrawalStatus): WithdrawalSelectionResult
        data object UnknownOperation: WithdrawalSelectionResult
        data object AlreadySelected: WithdrawalSelectionResult
        data object RequestPubReuse: WithdrawalSelectionResult
        data object UnknownAccount: WithdrawalSelectionResult
        data object AccountIsNotExchange: WithdrawalSelectionResult
    }

    /** Set details ([exchangePayto] & [reservePub]) for withdrawal operation [uuid] */
    suspend fun setDetails(
        uuid: UUID,
        exchangePayto: Payto,
        reservePub: EddsaPublicKey
    ): WithdrawalSelectionResult = db.serializable { conn ->
        val stmt = conn.prepareStatement("""
            SELECT
                out_no_op,
                out_already_selected,
                out_reserve_pub_reuse,
                out_account_not_found,
                out_account_is_not_exchange,
                out_status
            FROM select_taler_withdrawal(?, ?, ?, ?);
        """
        )
        stmt.setObject(1, uuid)
        stmt.setBytes(2, reservePub.raw)
        stmt.setString(3, "Taler withdrawal $reservePub")
        stmt.setString(4, exchangePayto.canonical)
        stmt.executeQuery().use {
            when {
                !it.next() ->
                    throw internalServerError("No result from DB procedure select_taler_withdrawal")
                it.getBoolean("out_no_op") -> WithdrawalSelectionResult.UnknownOperation
                it.getBoolean("out_already_selected") -> WithdrawalSelectionResult.AlreadySelected
                it.getBoolean("out_reserve_pub_reuse") -> WithdrawalSelectionResult.RequestPubReuse
                it.getBoolean("out_account_not_found") -> WithdrawalSelectionResult.UnknownAccount
                it.getBoolean("out_account_is_not_exchange") -> WithdrawalSelectionResult.AccountIsNotExchange
                else -> WithdrawalSelectionResult.Success(WithdrawalStatus.valueOf(it.getString("out_status")))
            }
        }
    }

    /** Result status of withdrawal operation confirmation */
    enum class WithdrawalConfirmationResult {
        Success,
        UnknownOperation,
        UnknownExchange,
        BalanceInsufficient,
        NotSelected,
        AlreadyAborted,
        TanRequired
    }

    /** Confirm withdrawal operation [uuid] */
    suspend fun confirm(
        login: String,
        uuid: UUID,
        now: Instant,
        is2fa: Boolean
    ): WithdrawalConfirmationResult = db.serializable { conn ->
        val stmt = conn.prepareStatement("""
            SELECT
              out_no_op,
              out_exchange_not_found,
              out_balance_insufficient,
              out_not_selected,
              out_aborted,
              out_tan_required
            FROM confirm_taler_withdrawal(?,?,?,?);
        """
        )
        stmt.setString(1, login)
        stmt.setObject(2, uuid)
        stmt.setLong(3, now.micros())
        stmt.setBoolean(4, is2fa)
        stmt.executeQuery().use {
            when {
                !it.next() ->
                    throw internalServerError("No result from DB procedure confirm_taler_withdrawal")
                it.getBoolean("out_no_op") -> WithdrawalConfirmationResult.UnknownOperation
                it.getBoolean("out_exchange_not_found") -> WithdrawalConfirmationResult.UnknownExchange
                it.getBoolean("out_balance_insufficient") -> WithdrawalConfirmationResult.BalanceInsufficient
                it.getBoolean("out_not_selected") -> WithdrawalConfirmationResult.NotSelected
                it.getBoolean("out_aborted") -> WithdrawalConfirmationResult.AlreadyAborted
                it.getBoolean("out_tan_required") -> WithdrawalConfirmationResult.TanRequired
                else -> WithdrawalConfirmationResult.Success
            }
        }
    }

    /** Get withdrawal operation [uuid] linked account username */
    suspend fun getUsername(uuid: UUID): String? = db.conn { conn -> 
        val stmt = conn.prepareStatement("""
            SELECT login
            FROM taler_withdrawal_operations
                JOIN bank_accounts ON wallet_bank_account=bank_account_id
                JOIN customers ON customer_id=owning_customer_id
            WHERE withdrawal_uuid=?
        """)
        stmt.setObject(1, uuid)
        stmt.oneOrNull { it.getString(1) }
    }

    private suspend fun <T> poll(
        uuid: UUID, 
        params: StatusParams, 
        status: (T) -> WithdrawalStatus,
        load: suspend () -> T?
    ): T? {
        return if (params.polling.poll_ms > 0) {
            db.notifWatcher.listenWithdrawals(uuid) { flow ->
                coroutineScope {
                    // Start buffering notification before loading transactions to not miss any
                    val polling = launch {
                        withTimeoutOrNull(params.polling.poll_ms) {
                            flow.first { it != params.old_state }
                        }
                    }    
                    // Initial loading
                    val init = load()
                    // Long polling if there is no operation or its not confirmed
                    if (init?.run { status(this) == params.old_state } != false) {
                        polling.join()
                        load()
                    } else {
                        polling.cancel()
                        init
                    }
                }
            }
        } else {
            load()
        }
    }

    /** Pool public info of operation [uuid] */
    suspend fun pollInfo(uuid: UUID, params: StatusParams): WithdrawalPublicInfo? = 
        poll(uuid, params, status = { it.status }) {
            db.conn { conn ->
                val stmt = conn.prepareStatement("""
                    SELECT
                    CASE 
                        WHEN confirmation_done THEN 'confirmed'
                        WHEN aborted THEN 'aborted'
                        WHEN selection_done THEN 'selected'
                        ELSE 'pending'
                    END as status
                    ,(amount).val as amount_val
                    ,(amount).frac as amount_frac
                    ,selection_done     
                    ,aborted     
                    ,confirmation_done     
                    ,reserve_pub
                    ,selected_exchange_payto
                    ,login
                    FROM taler_withdrawal_operations
                        JOIN bank_accounts ON wallet_bank_account=bank_account_id
                        JOIN customers ON customer_id=owning_customer_id
                    WHERE withdrawal_uuid=?
                """)
                stmt.setObject(1, uuid)
                stmt.oneOrNull {
                    WithdrawalPublicInfo(
                        status = WithdrawalStatus.valueOf(it.getString("status")),
                        amount = it.getAmount("amount", db.bankCurrency),
                        username = it.getString("login"),
                        selected_exchange_account = it.getString("selected_exchange_payto"),
                        selected_reserve_pub = it.getBytes("reserve_pub")?.run(::EddsaPublicKey)
                    )
                }
            }
        }

    /** Pool public status of operation [uuid] */
    suspend fun pollStatus(uuid: UUID, params: StatusParams, wire: WireMethod): BankWithdrawalOperationStatus? =
        poll(uuid, params, status = { it.status }) {
            db.conn { conn ->
                val stmt = conn.prepareStatement("""
                    SELECT
                      CASE 
                        WHEN confirmation_done THEN 'confirmed'
                        WHEN aborted THEN 'aborted'
                        WHEN selection_done THEN 'selected'
                        ELSE 'pending'
                      END as status
                      ,(amount).val as amount_val
                      ,(amount).frac as amount_frac
                      ,selection_done     
                      ,aborted     
                      ,confirmation_done      
                      ,internal_payto_uri
                      ,reserve_pub
                      ,selected_exchange_payto 
                    FROM taler_withdrawal_operations
                        JOIN bank_accounts ON (wallet_bank_account=bank_account_id)
                    WHERE withdrawal_uuid=?
                """)
                stmt.setObject(1, uuid)
                stmt.oneOrNull {
                    BankWithdrawalOperationStatus(
                        status = WithdrawalStatus.valueOf(it.getString("status")),
                        amount = it.getAmount("amount", db.bankCurrency),
                        selection_done = it.getBoolean("selection_done"),
                        transfer_done = it.getBoolean("confirmation_done"),
                        aborted = it.getBoolean("aborted"),
                        sender_wire = it.getString("internal_payto_uri"),
                        confirm_transfer_url = null,
                        suggested_exchange = null,
                        selected_exchange_account = it.getString("selected_exchange_payto"),
                        selected_reserve_pub = it.getBytes("reserve_pub")?.run(::EddsaPublicKey),
                        wire_types = listOf(
                            when (wire) {
                                WireMethod.IBAN -> "iban"
                                WireMethod.X_TALER_BANK -> "x-taler-bank"
                            } 
                        )
                    )
                }
            }
        }
}