Added Helpdesk

Added M2M Auth
This commit is contained in:
2025-10-31 16:27:56 +01:00
parent bbd5bbab9b
commit 2eb19b36a6
7 changed files with 232 additions and 7 deletions

56
src/utils/helpers.ts Normal file
View File

@@ -0,0 +1,56 @@
// 🔧 Hilfsfunktionen
import {FastifyInstance} from "fastify";
export function extractDomain(email: string) {
if (!email) return null
const parts = email.split('@')
return parts.length === 2 ? parts[1].toLowerCase() : null
}
export async function findCustomerOrContactByEmailOrDomain(server:FastifyInstance, fromMail: string, tenantId: number) {
const sender = fromMail.toLowerCase()
const senderDomain = extractDomain(sender)
if (!senderDomain) return null
// 1⃣ Direkter Match über contacts
const { data: contactMatch } = await server.supabase
.from('contacts')
.select('id, customer')
.eq('email', sender)
.eq('tenant', tenantId)
.maybeSingle()
if (contactMatch?.customer) {
return { customer: contactMatch.customer, contact: contactMatch.id }
}
// 2⃣ Kunden nach Domain oder Rechnungs-E-Mail durchsuchen
const { data: customers, error } = await server.supabase
.from('customers')
.select('id, infoData')
.eq('tenant', tenantId)
if (error) {
server.log.error(`[Helpdesk] Fehler beim Laden der Kunden: ${error.message}`)
return null
}
for (const c of customers || []) {
const info = c.infoData || {}
const email = info.email?.toLowerCase()
const invoiceEmail = info.invoiceEmail?.toLowerCase()
const emailDomain = extractDomain(email)
const invoiceDomain = extractDomain(invoiceEmail)
if (
sender === email ||
sender === invoiceEmail ||
senderDomain === emailDomain ||
senderDomain === invoiceDomain
) {
return { customer: c.id, contact: null }
}
}
return null
}