fix: clean users.ts rewrite, add company_id to createUser, fix resetPassword link
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:
@@ -15,11 +15,11 @@ export async function getUsers() {
|
|||||||
|
|
||||||
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) {
|
if (!dbError && dbUsers) {
|
||||||
dbUsers.forEach((u) => {
|
dbUsers.forEach((u: any) => {
|
||||||
userMap[u.id] = {
|
userMap[u.id] = {
|
||||||
role: u.role,
|
role: u.role,
|
||||||
company_id: u.company_id || null,
|
company_id: u.company_id || null,
|
||||||
company_name: u.companies?.[0]?.name || null,
|
company_name: Array.isArray(u.companies) ? u.companies[0]?.name || null : u.companies?.name || null,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ export async function getUsers() {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createUser(data: { email: string; password?: string; role?: 'admin' | 'partner'; email_confirm?: boolean }) {
|
export async function createUser(data: { email: string; password?: string; role?: 'admin' | 'partner'; email_confirm?: boolean; company_id?: string }) {
|
||||||
const admin = createAdminClient();
|
const admin = createAdminClient();
|
||||||
const { data: { user }, error } = await admin.auth.admin.createUser({
|
const { data: { user }, error } = await admin.auth.admin.createUser({
|
||||||
email: data.email,
|
email: data.email,
|
||||||
@@ -45,7 +45,7 @@ export async function createUser(data: { email: string; password?: string; role?
|
|||||||
const role = data.role || 'partner';
|
const role = data.role || 'partner';
|
||||||
const { error: dbError } = await admin
|
const { error: dbError } = await admin
|
||||||
.from('users')
|
.from('users')
|
||||||
.upsert({ id: user.id, role }, { onConflict: 'id' });
|
.upsert({ id: user.id, role, company_id: data.company_id || null }, { onConflict: 'id' });
|
||||||
if (dbError) throw dbError;
|
if (dbError) throw dbError;
|
||||||
|
|
||||||
revalidatePath('/admin/users');
|
revalidatePath('/admin/users');
|
||||||
@@ -84,7 +84,8 @@ export async function resetPassword(email: string) {
|
|||||||
email,
|
email,
|
||||||
});
|
});
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
const resetLink = (data as any)?.action_link || (data as any)?.link || '';
|
const linkData = data as any;
|
||||||
|
const resetLink = linkData?.properties?.action_link || linkData?.action_link || linkData?.link || '';
|
||||||
if (!resetLink) throw new Error('Reset link not generated');
|
if (!resetLink) throw new Error('Reset link not generated');
|
||||||
await sendMail({
|
await sendMail({
|
||||||
to: email,
|
to: email,
|
||||||
@@ -106,15 +107,15 @@ export async function getPartners() {
|
|||||||
if (dbError) throw dbError;
|
if (dbError) throw dbError;
|
||||||
|
|
||||||
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 }> = {};
|
||||||
dbUsers.forEach((u) => {
|
dbUsers.forEach((u: any) => {
|
||||||
userMap[u.id] = {
|
userMap[u.id] = {
|
||||||
role: u.role,
|
role: u.role,
|
||||||
company_id: u.company_id || null,
|
company_id: u.company_id || null,
|
||||||
company_name: u.companies?.[0]?.name || null,
|
company_name: Array.isArray(u.companies) ? u.companies[0]?.name || null : u.companies?.name || null,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const partnerIds = new Set(dbUsers.map((u) => u.id));
|
const partnerIds = new Set(dbUsers.map((u: any) => u.id));
|
||||||
return users
|
return users
|
||||||
.filter((u) => partnerIds.has(u.id))
|
.filter((u) => partnerIds.has(u.id))
|
||||||
.map((u) => ({
|
.map((u) => ({
|
||||||
|
|||||||
Reference in New Issue
Block a user