feat(branding): add global dynamic theme system and fix 2fa mail
This commit is contained in:
122
shop/lib/actions/branding.ts
Normal file
122
shop/lib/actions/branding.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
'use server'
|
||||
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { createAdminClient } from '@/lib/supabase/admin'
|
||||
import { revalidatePath } from 'next/cache'
|
||||
import type { BrandingSettings } from '@/lib/constants/branding'
|
||||
|
||||
export async function getBrandingSettings(): Promise<BrandingSettings> {
|
||||
try {
|
||||
const admin = createAdminClient()
|
||||
const { data } = await admin
|
||||
.from('settings')
|
||||
.select('*')
|
||||
.eq('id', 'branding')
|
||||
.maybeSingle()
|
||||
|
||||
if (!data) {
|
||||
return {
|
||||
companyName: '',
|
||||
logoUrl: '',
|
||||
developerFooter: 'B2B Shop made by hephex',
|
||||
street: '',
|
||||
zip: '',
|
||||
city: '',
|
||||
billingStreet: '',
|
||||
billingZip: '',
|
||||
billingCity: '',
|
||||
sameBillingAddress: true,
|
||||
colorScheme: 'modern_blue',
|
||||
primaryColor: '#2563eb',
|
||||
accentColor: '#38bdf8',
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
companyName: data.company_name || '',
|
||||
logoUrl: data.logo_url || '',
|
||||
developerFooter: data.developer_footer || 'B2B Shop made by hephex',
|
||||
street: data.street || '',
|
||||
zip: data.zip || '',
|
||||
city: data.city || '',
|
||||
billingStreet: data.billing_street || '',
|
||||
billingZip: data.billing_zip || '',
|
||||
billingCity: data.billing_city || '',
|
||||
sameBillingAddress: data.same_billing_address !== false,
|
||||
colorScheme: data.color_scheme || 'modern_blue',
|
||||
primaryColor: data.primary_color || '#2563eb',
|
||||
accentColor: data.accent_color || '#38bdf8',
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to load branding settings:', err)
|
||||
return {
|
||||
companyName: '',
|
||||
logoUrl: '',
|
||||
developerFooter: 'B2B Shop made by hephex',
|
||||
street: '',
|
||||
zip: '',
|
||||
city: '',
|
||||
billingStreet: '',
|
||||
billingZip: '',
|
||||
billingCity: '',
|
||||
sameBillingAddress: true,
|
||||
colorScheme: 'modern_blue',
|
||||
primaryColor: '#2563eb',
|
||||
accentColor: '#38bdf8',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveBrandingSettings(
|
||||
settings: BrandingSettings
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
try {
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
|
||||
if (user) {
|
||||
const { data: userData } = await supabase
|
||||
.from('users')
|
||||
.select('role')
|
||||
.eq('id', user.id)
|
||||
.single()
|
||||
|
||||
if (!userData || userData.role !== 'admin') {
|
||||
return { success: false, error: 'Keine Berechtigung (nur Admins)' }
|
||||
}
|
||||
}
|
||||
|
||||
const admin = createAdminClient()
|
||||
const { error } = await admin
|
||||
.from('settings')
|
||||
.upsert({
|
||||
id: 'branding',
|
||||
company_name: settings.companyName,
|
||||
logo_url: settings.logoUrl,
|
||||
developer_footer: settings.developerFooter,
|
||||
street: settings.street,
|
||||
zip: settings.zip,
|
||||
city: settings.city,
|
||||
billing_street: settings.sameBillingAddress ? settings.street : settings.billingStreet,
|
||||
billing_zip: settings.sameBillingAddress ? settings.zip : settings.billingZip,
|
||||
billing_city: settings.sameBillingAddress ? settings.city : settings.billingCity,
|
||||
same_billing_address: settings.sameBillingAddress,
|
||||
color_scheme: settings.colorScheme,
|
||||
primary_color: settings.primaryColor,
|
||||
accent_color: settings.accentColor,
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
|
||||
if (error) {
|
||||
console.error('Failed to save branding settings:', error)
|
||||
return { success: false, error: error.message }
|
||||
}
|
||||
|
||||
revalidatePath('/')
|
||||
revalidatePath('/admin/einstellungen')
|
||||
return { success: true }
|
||||
} catch (err: any) {
|
||||
console.error('Exception in saveBrandingSettings:', err)
|
||||
return { success: false, error: err.message || 'Unerwarteter Fehler' }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user