Fix typings and company name access
All checks were successful
Staging Build / build (push) Successful in 2m38s
All checks were successful
Staging Build / build (push) Successful in 2m38s
This commit is contained in:
@@ -1,95 +1,91 @@
|
||||
'use server'
|
||||
'use server';
|
||||
|
||||
import { createAdminClient } from '@/lib/supabase/admin'
|
||||
import { createAdminClient } from '@/lib/supabase/admin';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { sendMail } from '@/utils/mail';
|
||||
|
||||
export async function getUsers() {
|
||||
const admin = createAdminClient()
|
||||
const { data: { users }, error } = await admin.auth.admin.listUsers()
|
||||
if (error) throw error
|
||||
const admin = createAdminClient();
|
||||
const { data: { users }, error } = await admin.auth.admin.listUsers();
|
||||
if (error) throw error;
|
||||
|
||||
const { data: dbUsers, error: dbError } = await admin
|
||||
.from('users')
|
||||
.select('id, role, company_id, companies(id, name)')
|
||||
.select('id, role, company_id, companies(id, name)');
|
||||
|
||||
const userMap: Record<string, { role: string; company_id: string | null; company_name: string | null }> = {}
|
||||
const userMap: Record<string, { role: string; company_id: string | null; company_name: string | null }> = {};
|
||||
if (!dbError && dbUsers) {
|
||||
dbUsers.forEach((u: any) => {
|
||||
dbUsers.forEach((u) => {
|
||||
userMap[u.id] = {
|
||||
role: u.role,
|
||||
company_id: u.company_id || null,
|
||||
company_name: u.companies?.name || null,
|
||||
}
|
||||
})
|
||||
company_name: u.companies?.[0]?.name || null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return users.map(u => ({
|
||||
return users.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,
|
||||
}))
|
||||
}));
|
||||
}
|
||||
|
||||
export async function createUser(data: { email: string; password?: string; role?: 'admin' | 'partner'; email_confirm?: boolean }) {
|
||||
const admin = createAdminClient()
|
||||
const admin = createAdminClient();
|
||||
const { data: { user }, error } = await admin.auth.admin.createUser({
|
||||
email: data.email,
|
||||
password: data.password || Math.random().toString(36).slice(-10),
|
||||
email_confirm: data.email_confirm ?? true,
|
||||
})
|
||||
if (error) throw error
|
||||
if (!user) throw new Error('Benutzer konnte nicht erstellt werden.')
|
||||
});
|
||||
if (error) throw error;
|
||||
if (!user) throw new Error('Benutzer konnte nicht erstellt werden.');
|
||||
|
||||
// Upsert user role in public.users table
|
||||
const role = data.role || 'partner'
|
||||
const role = data.role || 'partner';
|
||||
const { error: dbError } = await admin
|
||||
.from('users')
|
||||
.upsert({ id: user.id, role }, { onConflict: 'id' })
|
||||
.upsert({ id: user.id, role }, { onConflict: 'id' });
|
||||
if (dbError) throw dbError;
|
||||
|
||||
if (dbError) throw dbError
|
||||
|
||||
revalidatePath('/admin/users')
|
||||
return user
|
||||
revalidatePath('/admin/users');
|
||||
return user;
|
||||
}
|
||||
|
||||
export async function deleteUser(id: string) {
|
||||
const admin = createAdminClient()
|
||||
const { error } = await admin.auth.admin.deleteUser(id)
|
||||
if (error) throw error
|
||||
revalidatePath('/admin/users')
|
||||
const admin = createAdminClient();
|
||||
const { error } = await admin.auth.admin.deleteUser(id);
|
||||
if (error) throw error;
|
||||
revalidatePath('/admin/users');
|
||||
}
|
||||
|
||||
export async function updateUserRole(id: string, role: 'admin' | 'partner' | 'gesperrt') {
|
||||
const admin = createAdminClient()
|
||||
const admin = createAdminClient();
|
||||
const { error } = await admin
|
||||
.from('users')
|
||||
.upsert({ id, role }, { onConflict: 'id' })
|
||||
if (error) throw error
|
||||
revalidatePath('/admin/users')
|
||||
.upsert({ id, role }, { onConflict: 'id' });
|
||||
if (error) throw error;
|
||||
revalidatePath('/admin/users');
|
||||
}
|
||||
|
||||
export async function updateUserStatus(id: string, ban: boolean) {
|
||||
const admin = createAdminClient()
|
||||
const admin = createAdminClient();
|
||||
const { error } = await admin.auth.admin.updateUserById(id, {
|
||||
ban_duration: ban ? '876000h' : '0h', // 100 years or none
|
||||
})
|
||||
if (error) throw error
|
||||
revalidatePath('/admin/users')
|
||||
ban_duration: ban ? '876000h' : '0h',
|
||||
});
|
||||
if (error) throw error;
|
||||
revalidatePath('/admin/users');
|
||||
}
|
||||
|
||||
export async function resetPassword(email: string) {
|
||||
const admin = createAdminClient();
|
||||
// Generate password recovery link
|
||||
const { data, error } = await admin.auth.admin.generateLink({
|
||||
type: 'recovery',
|
||||
email,
|
||||
});
|
||||
if (error) throw error;
|
||||
const resetLink = data?.action_link || data?.link || '';
|
||||
const resetLink = (data as any)?.action_link || (data as any)?.link || '';
|
||||
if (!resetLink) throw new Error('Reset link not generated');
|
||||
// Send email via configured SMTP
|
||||
await sendMail({
|
||||
to: email,
|
||||
subject: 'Passwort zurücksetzen',
|
||||
@@ -97,43 +93,34 @@ export async function resetPassword(email: string) {
|
||||
html: `<p>Klicken Sie <a href="${resetLink}">hier</a>, um Ihr Passwort zurückzusetzen.</p>`,
|
||||
});
|
||||
}
|
||||
const admin = createAdminClient()
|
||||
// Generate a reset link or send email
|
||||
const { error } = await admin.auth.admin.generateLink({
|
||||
type: 'recovery',
|
||||
email,
|
||||
})
|
||||
if (error) throw error
|
||||
// Usually you'd send this link or let Supabase handle it via regular auth.resetPasswordForEmail
|
||||
}
|
||||
|
||||
export async function getPartners() {
|
||||
const admin = createAdminClient()
|
||||
const { data: { users }, error } = await admin.auth.admin.listUsers()
|
||||
if (error) throw error
|
||||
const admin = createAdminClient();
|
||||
const { data: { users }, error } = await admin.auth.admin.listUsers();
|
||||
if (error) throw error;
|
||||
|
||||
const { data: dbUsers, error: dbError } = await admin
|
||||
.from('users')
|
||||
.select('id, role, company_id, companies(id, name)')
|
||||
.in('role', ['partner', 'gesperrt'])
|
||||
if (dbError) throw dbError
|
||||
.in('role', ['partner', 'gesperrt']);
|
||||
if (dbError) throw dbError;
|
||||
|
||||
const userMap: Record<string, { role: string; company_id: string | null; company_name: string | null }> = {}
|
||||
dbUsers.forEach((u: any) => {
|
||||
const userMap: Record<string, { role: string; company_id: string | null; company_name: string | null }> = {};
|
||||
dbUsers.forEach((u) => {
|
||||
userMap[u.id] = {
|
||||
role: u.role,
|
||||
company_id: u.company_id || null,
|
||||
company_name: u.companies?.name || null,
|
||||
}
|
||||
})
|
||||
company_name: u.companies?.[0]?.name || null,
|
||||
};
|
||||
});
|
||||
|
||||
const partnerIds = new Set(dbUsers.map(u => u.id))
|
||||
const partnerIds = new Set(dbUsers.map((u) => u.id));
|
||||
return users
|
||||
.filter(u => partnerIds.has(u.id))
|
||||
.map(u => ({
|
||||
.filter((u) => partnerIds.has(u.id))
|
||||
.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,
|
||||
}))
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user