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
|
/*
* 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 tech.libeufin.bank.*
import tech.libeufin.common.*
import java.time.Instant
/** Data access logic for cashout operations */
class CashoutDAO(private val db: Database) {
/** Result of cashout operation creation */
sealed interface CashoutCreationResult {
data class Success(val id: Long): CashoutCreationResult
data object BadConversion: CashoutCreationResult
data object AccountNotFound: CashoutCreationResult
data object AccountIsExchange: CashoutCreationResult
data object BalanceInsufficient: CashoutCreationResult
data object RequestUidReuse: CashoutCreationResult
data object NoCashoutPayto: CashoutCreationResult
data object TanRequired: CashoutCreationResult
}
/** Create a new cashout operation */
suspend fun create(
login: String,
requestUid: ShortHashCode,
amountDebit: TalerAmount,
amountCredit: TalerAmount,
subject: String,
now: Instant,
is2fa: Boolean
): CashoutCreationResult = db.serializable { conn ->
val stmt = conn.prepareStatement("""
SELECT
out_bad_conversion,
out_account_not_found,
out_account_is_exchange,
out_balance_insufficient,
out_request_uid_reuse,
out_no_cashout_payto,
out_tan_required,
out_cashout_id
FROM cashout_create(?,?,(?,?)::taler_amount,(?,?)::taler_amount,?,?,?)
""")
stmt.setString(1, login)
stmt.setBytes(2, requestUid.raw)
stmt.setLong(3, amountDebit.value)
stmt.setInt(4, amountDebit.frac)
stmt.setLong(5, amountCredit.value)
stmt.setInt(6, amountCredit.frac)
stmt.setString(7, subject)
stmt.setLong(8, now.toDbMicros() ?: throw faultyTimestampByBank())
stmt.setBoolean(9, is2fa)
stmt.executeQuery().use {
when {
!it.next() ->
throw internalServerError("No result from DB procedure cashout_create")
it.getBoolean("out_bad_conversion") -> CashoutCreationResult.BadConversion
it.getBoolean("out_account_not_found") -> CashoutCreationResult.AccountNotFound
it.getBoolean("out_account_is_exchange") -> CashoutCreationResult.AccountIsExchange
it.getBoolean("out_balance_insufficient") -> CashoutCreationResult.BalanceInsufficient
it.getBoolean("out_request_uid_reuse") -> CashoutCreationResult.RequestUidReuse
it.getBoolean("out_no_cashout_payto") -> CashoutCreationResult.NoCashoutPayto
it.getBoolean("out_tan_required") -> CashoutCreationResult.TanRequired
else -> CashoutCreationResult.Success(it.getLong("out_cashout_id"))
}
}
}
/** Get status of cashout operation [id] owned by [login] */
suspend fun get(id: Long, login: String): CashoutStatusResponse? = db.conn { conn ->
val stmt = conn.prepareStatement("""
SELECT
(amount_debit).val as amount_debit_val
,(amount_debit).frac as amount_debit_frac
,(amount_credit).val as amount_credit_val
,(amount_credit).frac as amount_credit_frac
,cashout_operations.subject
,creation_time
,transaction_date as confirmation_date
,tan_channel
,CASE tan_channel
WHEN 'sms' THEN phone
WHEN 'email' THEN email
END as tan_info
FROM cashout_operations
JOIN bank_accounts ON bank_account=bank_account_id
JOIN customers ON owning_customer_id=customer_id
LEFT JOIN bank_account_transactions ON local_transaction=bank_transaction_id
WHERE cashout_id=? AND login=? AND deleted_at IS NULL
""")
stmt.setLong(1, id)
stmt.setString(2, login)
stmt.oneOrNull {
CashoutStatusResponse(
status = CashoutStatus.confirmed,
amount_debit = it.getAmount("amount_debit", db.bankCurrency),
amount_credit = it.getAmount("amount_credit", db.fiatCurrency!!),
subject = it.getString("subject"),
creation_time = it.getTalerTimestamp("creation_time"),
confirmation_time = when (val timestamp = it.getLong("confirmation_date")) {
0L -> null
else -> TalerProtocolTimestamp(timestamp.microsToJavaInstant() ?: throw faultyTimestampByBank())
},
tan_channel = it.getString("tan_channel")?.run { TanChannel.valueOf(this) },
tan_info = it.getString("tan_info"),
)
}
}
/** Get a page of all cashout operations */
suspend fun pageAll(params: PageParams): List<GlobalCashoutInfo> =
db.page(params, "cashout_id", """
SELECT
cashout_id
,login
FROM cashout_operations
JOIN bank_accounts ON bank_account=bank_account_id
JOIN customers ON owning_customer_id=customer_id
WHERE deleted_at IS NULL AND
""") {
GlobalCashoutInfo(
cashout_id = it.getLong("cashout_id"),
username = it.getString("login"),
status = CashoutStatus.confirmed
)
}
/** Get a page of all cashout operations owned by [login] */
suspend fun pageForUser(params: PageParams, login: String): List<CashoutInfo> =
db.page(params, "cashout_id", """
SELECT cashout_id
FROM cashout_operations
JOIN bank_accounts ON bank_account=bank_account_id
JOIN customers ON owning_customer_id=customer_id
WHERE login = ? AND deleted_at IS NULL AND
""",
bind = {
setString(1, login)
1
}
) {
CashoutInfo(
cashout_id = it.getLong("cashout_id"),
status = CashoutStatus.confirmed
)
}
}
|