Changes
This commit is contained in:
@@ -2,8 +2,8 @@ import ical, {ICalCalendarMethod} from 'ical-generator';
|
||||
import express from "express"
|
||||
import {createClient} from "@supabase/supabase-js";
|
||||
import vCardsJS from "vcards-js"
|
||||
import {ImapFlow} from 'imapflow'
|
||||
import cors from 'cors'
|
||||
import axios from "axios"
|
||||
|
||||
const supabase = createClient(process.env.SUPABASE_URL,process.env.SUPABASE_SERVICE_ROLE_KEY)
|
||||
const app = express();
|
||||
@@ -15,6 +15,26 @@ app.use(cors())
|
||||
*
|
||||
* */
|
||||
|
||||
let goCardlessAccessToken = null
|
||||
let goCardlessRefreshToken = null
|
||||
const getGoCardlessToken = async () => {
|
||||
const {data,error} = await axios({
|
||||
url: "https://bankaccountdata.gocardless.com/api/v2/token/new/",
|
||||
method: "POST",
|
||||
data: {
|
||||
secret_id: process.env.GOCARDLESS_SECRET_ID,
|
||||
secret_key: process.env.GOCARDLESS_SECRET_KEY
|
||||
}
|
||||
})
|
||||
|
||||
if(error) throw error
|
||||
|
||||
console.log(data)
|
||||
goCardlessRefreshToken = data.refresh
|
||||
goCardlessAccessToken = data.access
|
||||
}
|
||||
|
||||
|
||||
app.get("/contacts/:tenantId", async (req,res) => {
|
||||
const tenantId = req.params.tenantId
|
||||
let contacts = (await supabase.from("contacts").select().eq("tenant", tenantId)).data
|
||||
@@ -85,95 +105,205 @@ app.get('/calendar/:userId', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/email/listInboxes", async (req,res) => {
|
||||
const client = new ImapFlow({
|
||||
host: 'imap.strato.de',
|
||||
port: 993,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: 'info@federspiel.tech',
|
||||
pass: 'fpGUpbQG34'
|
||||
},
|
||||
logger: {}
|
||||
});
|
||||
|
||||
await client.connect();
|
||||
let tree = await client.listTree();
|
||||
let folders = []
|
||||
tree.folders.forEach(mailbox => folders.push(mailbox.path))
|
||||
|
||||
res.json(folders)
|
||||
|
||||
})
|
||||
app.get("/email/listMessagesInInbox/:inbox", async (req,res) => {
|
||||
const inbox = req.params.inbox
|
||||
|
||||
const client = new ImapFlow({
|
||||
host: 'imap.strato.de',
|
||||
port: 993,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: 'info@federspiel.tech',
|
||||
pass: 'fpGUpbQG34'
|
||||
},
|
||||
logger: {}
|
||||
});
|
||||
|
||||
await client.connect();
|
||||
let messages = [];
|
||||
let mailbox = await client.mailboxOpen(inbox);
|
||||
|
||||
for await (let message of client.fetch('1:*', { envelope: true })) {
|
||||
console.log(`${message.uid}: ${message.envelope.subject}`);
|
||||
messages.push(message)
|
||||
//console.log(`${message.envelope}`);
|
||||
}
|
||||
console.log(messages)
|
||||
let messagesString = JSON.stringify(messages, (key,value) => typeof value === 'bigint' ? value.toString() : value)
|
||||
console.log(messagesString)
|
||||
|
||||
res.json(JSON.parse(messagesString))
|
||||
|
||||
})
|
||||
|
||||
app.get("/email/getContent/:inbox/:uid", async (req,res) => {
|
||||
const inbox = req.params.inbox
|
||||
const uid = req.params.uid
|
||||
|
||||
const client = new ImapFlow({
|
||||
host: 'imap.strato.de',
|
||||
port: 993,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: 'info@federspiel.tech',
|
||||
pass: 'fpGUpbQG34'
|
||||
},
|
||||
logger: {}
|
||||
});
|
||||
|
||||
await client.connect();
|
||||
let mailbox = await client.mailboxOpen(inbox);
|
||||
|
||||
|
||||
let data = await client.download(uid,'1.2');
|
||||
//console.log(data)
|
||||
|
||||
function streamToString (stream) {
|
||||
const chunks = [];
|
||||
return new Promise((resolve, reject) => {
|
||||
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
|
||||
stream.on('error', (err) => reject(err));
|
||||
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
||||
app.get("/banking/token", async (req,res) => {
|
||||
if(goCardlessAccessToken) {
|
||||
res.json({
|
||||
token: goCardlessAccessToken
|
||||
})
|
||||
} else {
|
||||
await getGoCardlessToken()
|
||||
res.json({
|
||||
token: goCardlessAccessToken
|
||||
})
|
||||
}
|
||||
})
|
||||
app.get('/banking/institutions/:bic?', async (req,res) => {
|
||||
|
||||
const result = await streamToString(data.content)
|
||||
|
||||
res.send(result)
|
||||
|
||||
const {data,error} = await axios({
|
||||
url:"https://bankaccountdata.gocardless.com/api/v2/institutions/?country=de",
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${goCardlessAccessToken}`
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
console.log(error)
|
||||
console.log(data)
|
||||
|
||||
if(req.params.bic) {
|
||||
let bank = data.find(i => i.bic.toLowerCase() === req.params.bic.toLowerCase())
|
||||
|
||||
if(bank) {
|
||||
res.json(bank)
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
}
|
||||
} else {
|
||||
res.json(data)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/banking/link', async (req,res) => {
|
||||
|
||||
const institutionId = req.query.institution_id
|
||||
const tenant = req.query.tenant
|
||||
|
||||
console.log(institutionId)
|
||||
console.log(req.query)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const {data,error} = await axios({
|
||||
url:"https://ob.gocardless.com/api/v2/requisitions/",
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${goCardlessAccessToken}`,
|
||||
accept: "application/json"
|
||||
},
|
||||
data: {
|
||||
redirect: "http://localhost:3000/banking",
|
||||
institution_id: institutionId,
|
||||
user_language: "de"
|
||||
}
|
||||
})
|
||||
|
||||
if(error) throw error
|
||||
console.log(data)
|
||||
console.log(error)
|
||||
|
||||
const {data: createAccountData, error: createAccountError} = await supabase
|
||||
.from("bankrequisitions")
|
||||
.insert({
|
||||
tenant: tenant,
|
||||
institutionId: institutionId,
|
||||
id: data.id,
|
||||
status: data.status
|
||||
})
|
||||
.select()
|
||||
.single()
|
||||
|
||||
//if(createAccountError) throw createAccountError
|
||||
console.log(createAccountData)
|
||||
console.log(createAccountError)
|
||||
|
||||
res.json({
|
||||
link: data.link
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
|
||||
app.get("/banking/link/refresh", async (req,res) => {
|
||||
const {data,error} = await supabase.from("bankrequisitions").select()
|
||||
|
||||
console.log(data)
|
||||
console.log(error)
|
||||
|
||||
const {data: listReqData, error: listReqError} = await axios({
|
||||
url:"https://ob.gocardless.com/api/v2/requisitions/",
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${goCardlessAccessToken}`,
|
||||
accept: "application/json"
|
||||
},
|
||||
})
|
||||
|
||||
console.log(listReqData)
|
||||
console.log(listReqError)
|
||||
|
||||
|
||||
|
||||
data.map(item => {
|
||||
let gcItem = listReqData.results.find(i => i.id === item.id)
|
||||
item.status = gcItem.status
|
||||
|
||||
return item
|
||||
})
|
||||
|
||||
for(const item of data) {
|
||||
const {data: updateReqData, error: updateReqError} = await supabase.from("bankrequisitions").update(item).eq("id",item.id)
|
||||
|
||||
console.log(updateReqData)
|
||||
console.log(updateReqError)
|
||||
}
|
||||
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
app.get("/banking/requisitions/:id?", async (req,res) => {
|
||||
const {data: listReqData, error: listReqError} = await axios({
|
||||
url:`https://ob.gocardless.com/api/v2/requisitions/${req.params.id ? req.params.id: ""}`,
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${goCardlessAccessToken}`,
|
||||
accept: "application/json"
|
||||
},
|
||||
})
|
||||
|
||||
console.log(listReqData)
|
||||
console.log(listReqError)
|
||||
|
||||
|
||||
|
||||
|
||||
if(listReqData){
|
||||
|
||||
if(listReqData.accounts) {
|
||||
let accounts = await Promise.all(listReqData.accounts.map(async (item) => {
|
||||
const {data,error} = await axios({
|
||||
url:"https://ob.gocardless.com/api/v2/accounts/" + item,
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${goCardlessAccessToken}`,
|
||||
accept: "application/json"
|
||||
},
|
||||
})
|
||||
|
||||
console.log(data)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
}))
|
||||
listReqData.accounts = accounts
|
||||
}
|
||||
|
||||
res.json(listReqData)
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
app.get("/banking/accounts/:id/:mode", async (req,res) => {
|
||||
const {data: listAccData, error: listAccError} = await axios({
|
||||
url:`https://ob.gocardless.com/api/v2/accounts/${req.params.id}/${req.params.mode}`,
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${goCardlessAccessToken}`,
|
||||
accept: "application/json"
|
||||
},
|
||||
})
|
||||
|
||||
console.log(listAccData)
|
||||
console.log(listAccError)
|
||||
|
||||
if(listAccData){
|
||||
res.json(listAccData)
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
if(!goCardlessAccessToken) {
|
||||
getGoCardlessToken()
|
||||
}
|
||||
|
||||
app.listen(3002);
|
||||
Reference in New Issue
Block a user