43 lines
1.3 KiB
TypeScript
43 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('*')
|
|
if (error) throw error
|
|
return data
|
|
}
|
|
|
|
export async function createCompany(name: string) {
|
|
const admin = createAdminClient()
|
|
const { data, error } = await admin.from('companies').insert({ name }).select().single()
|
|
if (error) throw error
|
|
revalidatePath('/admin/companies')
|
|
return data
|
|
}
|
|
|
|
export async function updateCompany(id: string, name: string) {
|
|
const admin = createAdminClient()
|
|
const { data, error } = await admin.from('companies').update({ name }).eq('id', id).select().single()
|
|
if (error) throw error
|
|
revalidatePath('/admin/companies')
|
|
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')
|
|
return true
|
|
}
|
|
|
|
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')
|
|
return true
|
|
}
|