feat: CC admins on orders and include partner info

This commit is contained in:
DanielS
2026-08-11 00:22:39 +02:00
parent 6177c0cde1
commit 3ba7c91e1f
2 changed files with 205 additions and 4 deletions

View File

@@ -68,7 +68,7 @@ export async function submitOrder(params: {
// Check if user is assigned to a company or is admin
const { data: dbUser } = await supabase
.from('users')
.select('role, company_id')
.select('role, company_id, first_name, last_name, email')
.eq('id', user.id)
.single()
@@ -77,6 +77,22 @@ export async function submitOrder(params: {
throw new Error('Sie müssen einer Firma zugewiesen sein, um eine Anfrage zu erstellen.')
}
let partnerCompanyName = ''
if (dbUser?.company_id) {
const { data: comp } = await supabase
.from('companies')
.select('name')
.eq('id', dbUser.company_id)
.single()
if (comp) {
partnerCompanyName = comp.name
}
} else if (isAdminUser) {
partnerCompanyName = 'Administrator / Direktbestellung'
}
const partnerUserName = dbUser ? [dbUser.first_name, dbUser.last_name].filter(Boolean).join(' ') : ''
const partnerUserEmail = dbUser?.email || user.email
// Fetch catalog directly from DB to prevent client tampering
const dbProducts = await getProducts()
const dbCategories = await getCategories()
@@ -267,7 +283,10 @@ export async function submitOrder(params: {
totalDetailsText,
totalDetailsHtml,
itemsDetailsText: itemsSection.text,
itemsDetailsHtml: itemsSection.html
itemsDetailsHtml: itemsSection.html,
partnerCompanyName,
partnerUserName,
partnerUserEmail
}, `${process.env.NEXT_PUBLIC_SITE_URL || 'https://staging.hephex.de'}`, false)
await sendMail({
@@ -283,6 +302,37 @@ export async function submitOrder(params: {
}
]
})
// Notify all admins
try {
const adminClient = createAdminClient()
const { data: admins } = await adminClient
.from('users')
.select('email')
.eq('role', 'admin')
if (admins) {
const adminEmails = admins.map((a: any) => a.email).filter(Boolean)
for (const adminEmail of adminEmails) {
if (adminEmail !== user.email) {
await sendMail({
to: adminEmail,
subject: `[Admin-Kopie] Anfragebestätigung ${orderNumber}`,
text: emailTemplate.text,
html: emailTemplate.html,
attachments: [
{
filename: `Anfragebestaetigung_${orderNumber}.pdf`,
content: buffer,
contentType: 'application/pdf',
}
]
})
}
}
}
} catch (adminMailError) {
console.error('Failed to notify admins of new order:', adminMailError)
}
} catch (mailError) {
console.error('Failed to send order confirmation mail:', mailError)
}
@@ -407,6 +457,29 @@ export async function updateOrderStatus(
}
await sendMail(mailOptions)
// Notify admins
try {
const adminClient = createAdminClient()
const { data: admins } = await adminClient
.from('users')
.select('email')
.eq('role', 'admin')
if (admins) {
const adminEmails = admins.map((a: any) => a.email).filter(Boolean)
for (const adminEmail of adminEmails) {
if (adminEmail !== user.email) {
await sendMail({
...mailOptions,
to: adminEmail,
subject: `[Admin-Kopie] Statusänderung Ihrer Anfrage ${orderNumber}`
})
}
}
}
} catch (adminMailError) {
console.error('Failed to notify admins of status update:', adminMailError)
}
}
} catch (mailError) {
console.error('Failed to send status update email:', mailError)
@@ -545,6 +618,30 @@ export async function rejectOrder(
text: statusEmail.text,
html: statusEmail.html
})
// Notify admins
try {
const adminClient = createAdminClient()
const { data: admins } = await adminClient
.from('users')
.select('email')
.eq('role', 'admin')
if (admins) {
const adminEmails = admins.map((a: any) => a.email).filter(Boolean)
for (const adminEmail of adminEmails) {
if (adminEmail !== orderUser.email) {
await sendMail({
to: adminEmail,
subject: `[Admin-Kopie] Anfrage abgelehnt: ${orderNumber}`,
text: statusEmail.text,
html: statusEmail.html
})
}
}
}
} catch (adminMailError) {
console.error('Failed to notify admins of order rejection:', adminMailError)
}
}
} catch (mailError) {
console.error('Failed to send rejection email:', mailError)
@@ -772,6 +869,32 @@ export async function updateOrder(
`
}
// Load partner details for update email
let partnerCompanyName = ''
let partnerUserName = ''
let partnerUserEmail = ''
if (order?.user_id) {
const { data: orderDbUser } = await supabase
.from('users')
.select('first_name, last_name, email, company_id')
.eq('id', order.user_id)
.single()
if (orderDbUser) {
partnerUserName = [orderDbUser.first_name, orderDbUser.last_name].filter(Boolean).join(' ')
partnerUserEmail = orderDbUser.email || ''
if (orderDbUser.company_id) {
const { data: comp } = await supabase
.from('companies')
.select('name')
.eq('id', orderDbUser.company_id)
.single()
if (comp) {
partnerCompanyName = comp.name
}
}
}
}
const itemsSection = buildEmailItemsSection(items)
const emailTemplate = getOrderEmailTemplate({
@@ -781,7 +904,10 @@ export async function updateOrder(
totalDetailsText,
totalDetailsHtml,
itemsDetailsText: itemsSection.text,
itemsDetailsHtml: itemsSection.html
itemsDetailsHtml: itemsSection.html,
partnerCompanyName,
partnerUserName,
partnerUserEmail
}, `${process.env.NEXT_PUBLIC_SITE_URL || 'https://staging.hephex.de'}`, true)
await sendMail({
@@ -797,6 +923,37 @@ export async function updateOrder(
}
]
})
// Notify admins
try {
const adminClient = createAdminClient()
const { data: admins } = await adminClient
.from('users')
.select('email')
.eq('role', 'admin')
if (admins) {
const adminEmails = admins.map((a: any) => a.email).filter(Boolean)
for (const adminEmail of adminEmails) {
if (adminEmail !== orderUserEmail) {
await sendMail({
to: adminEmail,
subject: `[Admin-Kopie] Anfrageänderung ${order.order_number}`,
text: emailTemplate.text,
html: emailTemplate.html,
attachments: [
{
filename: `Anfragebestaetigung_${order.order_number}.pdf`,
content: buffer,
contentType: 'application/pdf',
}
]
})
}
}
}
} catch (adminMailError) {
console.error('Failed to notify admins of order update:', adminMailError)
}
} catch (mailError) {
console.error('Failed to send order update confirmation mail:', mailError)
}