feat: add company management and user-company assignment in admin panel
All checks were successful
Staging Build / build (push) Successful in 2m35s

This commit is contained in:
DanielS
2026-06-24 17:12:40 +02:00
parent 6b66cc5c41
commit 7a14aa0f75
8 changed files with 346 additions and 15 deletions

View File

@@ -0,0 +1,49 @@
'use server'
import { createAdminClient } from '@/lib/supabase/admin'
import { revalidatePath } from 'next/cache'
export async function getCompanies() {
const admin = createAdminClient()
const { data, error } = await admin
.from('companies')
.select('id, name, created_at')
.order('name', { ascending: true })
if (error) throw error
return data || []
}
export async function createCompany(name: string) {
const admin = createAdminClient()
const { data, error } = await admin
.from('companies')
.insert({ name: name.trim() })
.select()
.single()
if (error) throw error
revalidatePath('/admin/companies')
revalidatePath('/admin/users')
return data
}
export async function deleteCompany(id: string) {
const admin = createAdminClient()
const { error } = await admin
.from('companies')
.delete()
.eq('id', id)
if (error) throw error
revalidatePath('/admin/companies')
revalidatePath('/admin/users')
}
export async function assignCompanyToUser(userId: string, companyId: string | null) {
const admin = createAdminClient()
const { error } = await admin
.from('users')
.update({ company_id: companyId })
.eq('id', userId)
if (error) throw error
revalidatePath('/admin/users')
revalidatePath('/admin/einstellungen')
}

View File

@@ -10,18 +10,24 @@ export async function getUsers() {
const { data: dbUsers, error: dbError } = await admin
.from('users')
.select('id, role')
.select('id, role, company_id, companies(id, name)')
const roleMap: Record<string, string> = {}
const userMap: Record<string, { role: string; company_id: string | null; company_name: string | null }> = {}
if (!dbError && dbUsers) {
dbUsers.forEach(u => {
roleMap[u.id] = u.role
dbUsers.forEach((u: any) => {
userMap[u.id] = {
role: u.role,
company_id: u.company_id || null,
company_name: u.companies?.name || null,
}
})
}
return users.map(u => ({
...u,
role: roleMap[u.id] || 'partner'
role: userMap[u.id]?.role || 'partner',
company_id: userMap[u.id]?.company_id || null,
company_name: userMap[u.id]?.company_name || null,
}))
}
@@ -90,15 +96,26 @@ export async function getPartners() {
const { data: dbUsers, error: dbError } = await admin
.from('users')
.select('id, role')
.select('id, role, company_id, companies(id, name)')
.in('role', ['partner', 'gesperrt'])
if (dbError) throw dbError
const roleMap: Record<string, string> = {}
dbUsers.forEach(u => { roleMap[u.id] = u.role })
const userMap: Record<string, { role: string; company_id: string | null; company_name: string | null }> = {}
dbUsers.forEach((u: any) => {
userMap[u.id] = {
role: u.role,
company_id: u.company_id || null,
company_name: u.companies?.name || null,
}
})
const partnerIds = new Set(dbUsers.map(u => u.id))
return users
.filter(u => partnerIds.has(u.id))
.map(u => ({ ...u, role: roleMap[u.id] || 'partner' }))
.map(u => ({
...u,
role: userMap[u.id]?.role || 'partner',
company_id: userMap[u.id]?.company_id || null,
company_name: userMap[u.id]?.company_name || null,
}))
}