feat: add first_name and last_name to users and implement inline editing in user-list
All checks were successful
Staging Build / build (push) Successful in 2m40s
All checks were successful
Staging Build / build (push) Successful in 2m40s
This commit is contained in:
@@ -13,6 +13,10 @@ export async function getUsers() {
|
||||
.from('users')
|
||||
.select('id, role, company_id, companies(id, name)');
|
||||
|
||||
const { data: dbProfiles, error: profilesError } = await admin
|
||||
.from('profiles')
|
||||
.select('id, first_name, last_name');
|
||||
|
||||
const userMap: Record<string, { role: string; company_id: string | null; company_name: string | null }> = {};
|
||||
if (!dbError && dbUsers) {
|
||||
dbUsers.forEach((u: any) => {
|
||||
@@ -24,15 +28,27 @@ export async function getUsers() {
|
||||
});
|
||||
}
|
||||
|
||||
const profileMap: Record<string, { first_name: string | null; last_name: string | null }> = {};
|
||||
if (!profilesError && dbProfiles) {
|
||||
dbProfiles.forEach((p: any) => {
|
||||
profileMap[p.id] = {
|
||||
first_name: p.first_name || null,
|
||||
last_name: p.last_name || null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
first_name: profileMap[u.id]?.first_name || null,
|
||||
last_name: profileMap[u.id]?.last_name || null,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function createUser(data: { email: string; role?: 'admin' | 'partner'; email_confirm?: boolean; company_id?: string }) {
|
||||
export async function createUser(data: { email: string; role?: 'admin' | 'partner'; email_confirm?: boolean; company_id?: string; first_name?: string; last_name?: string }) {
|
||||
const admin = createAdminClient();
|
||||
const { data: { user }, error } = await admin.auth.admin.createUser({
|
||||
email: data.email,
|
||||
@@ -48,6 +64,15 @@ export async function createUser(data: { email: string; role?: 'admin' | 'partne
|
||||
.upsert({ id: user.id, role, company_id: data.company_id || null }, { onConflict: 'id' });
|
||||
if (dbError) throw dbError;
|
||||
|
||||
const { error: profileError } = await admin
|
||||
.from('profiles')
|
||||
.upsert({
|
||||
id: user.id,
|
||||
first_name: data.first_name || null,
|
||||
last_name: data.last_name || null,
|
||||
}, { onConflict: 'id' });
|
||||
if (profileError) throw profileError;
|
||||
|
||||
// Generate recovery link to let user set password
|
||||
try {
|
||||
const { data: linkData, error: linkError } = await admin.auth.admin.generateLink({
|
||||
@@ -196,3 +221,12 @@ export async function getPartners() {
|
||||
company_name: userMap[u.id]?.company_name || null,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function updateUserProfile(id: string, data: { first_name: string | null; last_name: string | null }) {
|
||||
const admin = createAdminClient();
|
||||
const { error } = await admin
|
||||
.from('profiles')
|
||||
.upsert({ id, first_name: data.first_name, last_name: data.last_name }, { onConflict: 'id' });
|
||||
if (error) throw error;
|
||||
revalidatePath('/admin/users');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user