50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
'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')
|
|
}
|