Many Changes
This commit is contained in:
179
tools/comServer/index.mjs
Normal file
179
tools/comServer/index.mjs
Normal file
@@ -0,0 +1,179 @@
|
||||
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'
|
||||
|
||||
const supabase = createClient(process.env.SUPABASE_URL,process.env.SUPABASE_SERVICE_ROLE_KEY)
|
||||
const app = express();
|
||||
app.use(cors())
|
||||
|
||||
/*
|
||||
* TODO: Implement Auth for the Endpoints
|
||||
*
|
||||
*
|
||||
* */
|
||||
|
||||
app.get("/contacts/:tenantId", async (req,res) => {
|
||||
const tenantId = req.params.tenantId
|
||||
let contacts = (await supabase.from("contacts").select().eq("tenant", tenantId)).data
|
||||
let customers = (await supabase.from("customers").select().eq("tenant", tenantId)).data
|
||||
let vendors = (await supabase.from("vendors").select().eq("tenant", tenantId)).data
|
||||
console.log(contacts)
|
||||
|
||||
let cardsString = ""
|
||||
|
||||
contacts.forEach(contact => {
|
||||
let vCard = vCardsJS();
|
||||
vCard.firstName = contact.firstName
|
||||
vCard.lastName = contact.lastName
|
||||
vCard.role = contact.role
|
||||
vCard.cellPhone = contact.phoneMobile
|
||||
vCard.workPhone = contact.phoneHome
|
||||
vCard.email = contact.email
|
||||
vCard.namePrefix = contact.salutation
|
||||
if(contact.customer) vCard.organization = customers.find(i => i.id === contact.customer).name
|
||||
cardsString += vCard.getFormattedString()
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
//set content-type and disposition including desired filename
|
||||
res.set('Content-Type', 'text/vcard; name="enesser.vcf"');
|
||||
res.set('Content-Disposition', 'inline; filename="enesser.vcf"');
|
||||
|
||||
//send the response
|
||||
res.send(cardsString);
|
||||
})
|
||||
|
||||
app.get('/calendar/:userId', async (req, res) => {
|
||||
|
||||
const userId = req.params.userId
|
||||
|
||||
let userExisting = ((await supabase.from("profiles").select('id').eq("id", userId)).data)
|
||||
console.log(userExisting)
|
||||
|
||||
if(userExisting) {
|
||||
let events = (await supabase.from("events").select()).data
|
||||
|
||||
console.log(events[0].resources)
|
||||
|
||||
events = events.filter(i => i.resources.filter(resource => resource.id === userId).length > 0)
|
||||
|
||||
const cal = ical({
|
||||
prodId: '//spaces.software//ical-generator/r/EN',
|
||||
events: events.map(event => {
|
||||
return {
|
||||
start: event.start,
|
||||
end: event.end,
|
||||
summary: event.title,
|
||||
description: ""
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/calendar; charset=utf-8',
|
||||
'Content-Disposition': 'attachment; filename="calendar.ics"'
|
||||
});
|
||||
|
||||
res.end(cal.toString());
|
||||
} else {
|
||||
res.sendStatus(404)
|
||||
}
|
||||
});
|
||||
|
||||
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')));
|
||||
})
|
||||
}
|
||||
|
||||
const result = await streamToString(data.content)
|
||||
|
||||
res.send(result)
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
app.listen(3002);
|
||||
Reference in New Issue
Block a user