feat: zentrale selfhost-dienste bereitstellen
This commit is contained in:
@@ -7,6 +7,7 @@ import { insertHistoryItem } from "../utils/history"
|
||||
import { decrypt, encrypt } from "../utils/crypt"
|
||||
import { DE_BANK_CODE_TO_NAME } from "../utils/deBankCodes"
|
||||
import { DE_BANK_CODE_TO_BIC } from "../utils/deBankBics"
|
||||
import { centralServicesClient } from "../modules/push-server.client"
|
||||
|
||||
import {
|
||||
bankrequisitions,
|
||||
@@ -902,6 +903,7 @@ export default async function bankingRoutes(server: FastifyInstance) {
|
||||
const goCardLessBaseUrl = secrets.GOCARDLESS_BASE_URL
|
||||
const goCardLessSecretId = secrets.GOCARDLESS_SECRET_ID
|
||||
const goCardLessSecretKey = secrets.GOCARDLESS_SECRET_KEY
|
||||
const useCentralBanking = Boolean(secrets.FEDEO_CENTRAL_SERVICES_ENABLED && centralServicesClient.configured())
|
||||
|
||||
let tokenData: any = null
|
||||
|
||||
@@ -918,6 +920,7 @@ export default async function bankingRoutes(server: FastifyInstance) {
|
||||
}
|
||||
|
||||
const checkToken = async () => {
|
||||
if (useCentralBanking) return
|
||||
if (!tokenData) return await getToken()
|
||||
|
||||
const expired = dayjs(tokenData.created_at)
|
||||
@@ -935,24 +938,23 @@ export default async function bankingRoutes(server: FastifyInstance) {
|
||||
// ------------------------------------------------------------------
|
||||
server.get("/banking/link/:institutionid", async (req, reply) => {
|
||||
try {
|
||||
await checkToken()
|
||||
|
||||
const { institutionid } = req.params as { institutionid: string }
|
||||
const tenantId = req.user?.tenant_id
|
||||
|
||||
if (!tenantId) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const { data } = await axios.post(
|
||||
`${goCardLessBaseUrl}/requisitions/`,
|
||||
{
|
||||
redirect: "https://app.fedeo.de/settings/banking",
|
||||
institution_id: institutionid,
|
||||
user_language: "de",
|
||||
},
|
||||
{
|
||||
headers: { Authorization: `Bearer ${tokenData.access}` },
|
||||
}
|
||||
)
|
||||
const redirect = new URL("/settings/banking", secrets.API_BASE_URL).toString()
|
||||
let data: any
|
||||
if (useCentralBanking) {
|
||||
data = await centralServicesClient.createBankingRequisition({ institutionId: institutionid, redirect, userLanguage: "de" })
|
||||
} else {
|
||||
await checkToken()
|
||||
;({ data } = await axios.post(
|
||||
`${goCardLessBaseUrl}/requisitions/`,
|
||||
{ redirect, institution_id: institutionid, user_language: "de" },
|
||||
{ headers: { Authorization: `Bearer ${tokenData.access}` } }
|
||||
))
|
||||
}
|
||||
|
||||
// DB: Requisition speichern
|
||||
await server.db.insert(bankrequisitions).values({
|
||||
@@ -977,12 +979,16 @@ export default async function bankingRoutes(server: FastifyInstance) {
|
||||
const { bic } = req.params as { bic: string }
|
||||
if (!bic) return reply.code(400).send("BIC missing")
|
||||
|
||||
await checkToken()
|
||||
|
||||
const { data } = await axios.get(
|
||||
`${goCardLessBaseUrl}/institutions/?country=de`,
|
||||
{ headers: { Authorization: `Bearer ${tokenData.access}` } }
|
||||
)
|
||||
let data: any[]
|
||||
if (useCentralBanking) {
|
||||
data = await centralServicesClient.bankingInstitutions("DE")
|
||||
} else {
|
||||
await checkToken()
|
||||
;({ data } = await axios.get(
|
||||
`${goCardLessBaseUrl}/institutions/?country=de`,
|
||||
{ headers: { Authorization: `Bearer ${tokenData.access}` } }
|
||||
))
|
||||
}
|
||||
|
||||
const bank = data.find((i: any) => i.bic.toLowerCase() === bic.toLowerCase())
|
||||
|
||||
@@ -1004,21 +1010,23 @@ export default async function bankingRoutes(server: FastifyInstance) {
|
||||
const { reqId } = req.params as { reqId: string }
|
||||
if (!reqId) return reply.code(400).send("Requisition ID missing")
|
||||
|
||||
await checkToken()
|
||||
|
||||
const { data } = await axios.get(
|
||||
`${goCardLessBaseUrl}/requisitions/${reqId}`,
|
||||
{ headers: { Authorization: `Bearer ${tokenData.access}` } }
|
||||
)
|
||||
let data: any
|
||||
if (useCentralBanking) {
|
||||
data = await centralServicesClient.getBankingRequisition(reqId)
|
||||
} else {
|
||||
await checkToken()
|
||||
;({ data } = await axios.get(
|
||||
`${goCardLessBaseUrl}/requisitions/${reqId}`,
|
||||
{ headers: { Authorization: `Bearer ${tokenData.access}` } }
|
||||
))
|
||||
}
|
||||
|
||||
// Load account details
|
||||
if (data.accounts) {
|
||||
data.accounts = await Promise.all(
|
||||
data.accounts.map(async (accId: string) => {
|
||||
const { data: acc } = await axios.get(
|
||||
`${goCardLessBaseUrl}/accounts/${accId}`,
|
||||
{ headers: { Authorization: `Bearer ${tokenData.access}` } }
|
||||
)
|
||||
if (useCentralBanking) return centralServicesClient.getBankingAccount(accId)
|
||||
const { data: acc } = await axios.get(`${goCardLessBaseUrl}/accounts/${accId}`, { headers: { Authorization: `Bearer ${tokenData.access}` } })
|
||||
return acc
|
||||
})
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user