summaryrefslogtreecommitdiff
path: root/sandbox/src/main/kotlin/tech/libeufin/sandbox/Helpers.kt
blob: bd884ab1455ee2d7a3b106c4e90979a5e4733d7b (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
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2020 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.sandbox

import io.ktor.http.HttpStatusCode
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction

/**
 * Helps to communicate Camt values without having
 * to parse the XML each time one is needed.
 */
data class SandboxCamt(
    val camtMessage: String,
    val messageId: String,
    /**
     * That is the number of SECONDS since Epoch.  This
     * value is exactly what goes into the Camt document.
     */
    val creationTime: Long
)

fun SandboxAssert(condition: Boolean, reason: String) {
    if (!condition) throw SandboxError(HttpStatusCode.InternalServerError, reason)
}

fun getOrderTypeFromTransactionId(transactionID: String): String {
    val uploadTransaction = transaction {
        EbicsUploadTransactionEntity.findById(transactionID)
    } ?: throw SandboxError(
        /**
         * NOTE: at this point, it might even be the server's fault.
         * For example, if it failed to store a ID earlier.
         */
        HttpStatusCode.NotFound,
        "Could not retrieve order type for transaction: $transactionID"
    )
    return uploadTransaction.orderType
}

fun getBankAccountFromIban(iban: String): BankAccountEntity {
    return transaction {
        BankAccountEntity.find(
            BankAccountsTable.iban eq iban
        )
    }.firstOrNull() ?: throw SandboxError(
        HttpStatusCode.NotFound,
        "Did not find a bank account for ${iban}"
    )
}

fun getBankAccountFromLabel(label: String): BankAccountEntity {
    return transaction {
        BankAccountEntity.find(
            BankAccountsTable.label eq label
        )
    }.firstOrNull() ?: throw SandboxError(
        HttpStatusCode.NotFound,
        "Did not find a bank account for label ${label}"
    )
}

fun getBankAccountFromSubscriber(subscriber: EbicsSubscriberEntity): BankAccountEntity {
    return transaction {
        subscriber.bankAccount ?: throw SandboxError(
            HttpStatusCode.NotFound,
            "Subscriber doesn't have any bank account"
        )
    }
}

/**
 * Fetch a configuration for Sandbox, corresponding to the host that runs the service.
 */
fun getSandboxConfig(hostname: String?): SandboxConfigEntity {
    var ret: SandboxConfigEntity? = transaction {
        if (hostname == null) {
            SandboxConfigEntity.all().firstOrNull()
        } else {
            SandboxConfigEntity.find {
                SandboxConfigsTable.hostname eq hostname
            }.firstOrNull()
        }
    }
    if (ret == null) throw SandboxError(
        HttpStatusCode.InternalServerError,
        "Serving from a non configured host"
    )
    return ret
}

fun getEbicsSubscriberFromDetails(userID: String, partnerID: String, hostID: String): EbicsSubscriberEntity {
    return transaction {
        EbicsSubscriberEntity.find {
            (EbicsSubscribersTable.userId eq userID) and (EbicsSubscribersTable.partnerId eq partnerID) and
                    (EbicsSubscribersTable.hostId eq hostID)
        }.firstOrNull() ?: throw SandboxError(
            HttpStatusCode.NotFound,
            "Ebics subscriber not found"
        )
    }
}

/**
 * FIXME: commenting out until a solution for i18n is found.
 *
private fun initJinjava(): Jinjava {
    class JinjaFunctions {
        // Used by templates to retrieve configuration values.
        fun settings_value(name: String): String {
            return "foo"
        }
        fun gettext(translatable: String): String {
            // temporary, just to make the compiler happy.
            return translatable
        }
        fun url(name: String): String {
            val map = mapOf<String, String>(
                "login" to "todo",
                "profile" to "todo",
                "register" to "todo",
                "public-accounts" to "todo"
            )
            return map[name] ?: throw SandboxError(HttpStatusCode.InternalServerError, "URL name unknown")
        }
    }
    val jinjava = Jinjava()
    val settingsValueFunc = ELFunctionDefinition(
        "tech.libeufin.sandbox", "settings_value",
        JinjaFunctions::class.java, "settings_value", String::class.java
    )
    val gettextFuncAlias = ELFunctionDefinition(
        "tech.libeufin.sandbox", "_",
        JinjaFunctions::class.java, "gettext", String::class.java
    )
    val gettextFunc = ELFunctionDefinition(
        "", "gettext",
        JinjaFunctions::class.java, "gettext", String::class.java
    )
    val urlFunc = ELFunctionDefinition(
        "tech.libeufin.sandbox", "url",
        JinjaFunctions::class.java, "url", String::class.java
    )

    jinjava.globalContext.registerFunction(settingsValueFunc)
    jinjava.globalContext.registerFunction(gettextFunc)
    jinjava.globalContext.registerFunction(gettextFuncAlias)
    jinjava.globalContext.registerFunction(urlFunc)

    return jinjava
}

val jinjava = initJinjava()

fun renderTemplate(templateName: String, context: Map<String, String>): String {
    val template = Resources.toString(Resources.getResource(
        "templates/$templateName"), Charsets.UTF_8
    )
    return jinjava.render(template, context)
} **/