Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Merge branch 'main' into highrise-import

React-Admin CI6ffe3c6ef47f
diff --git a/src/components/atomic-crm/providers/supabase/dataProvider.ts b/src/components/atomic-crm/providers/supabase/dataProvider.ts
index 7bee8572..6a10c439 100644
--- a/src/components/atomic-crm/providers/supabase/dataProvider.ts
+++ b/src/components/atomic-crm/providers/supabase/dataProvider.ts
@@ -111,7 +111,14 @@ const dataProviderWithCustomMethods = {
if (!data || error) {
console.error("salesCreate.error", error);
- throw new Error("Failed to create account manager");
+ const errorDetails = await (async () => {
+ try {
+ return (await error?.context?.json()) ?? {};
+ } catch {
+ return {};
+ }
+ })();
+ throw new Error(errorDetails?.message || "Failed to create the user");
}
return data.data;
diff --git a/src/components/atomic-crm/sales/SalesCreate.tsx b/src/components/atomic-crm/sales/SalesCreate.tsx
index 61d77294..24585b29 100644
--- a/src/components/atomic-crm/sales/SalesCreate.tsx
+++ b/src/components/atomic-crm/sales/SalesCreate.tsx
@@ -24,8 +24,8 @@ export function SalesCreate() {
);
redirect("/sales");
},
- onError: () => {
- notify("An error occurred while creating the user.", {
+ onError: (error) => {
+ notify(error.message || "An error occurred while creating the user.", {
type: "error",
});
},
diff --git a/supabase/functions/_shared/utils.ts b/supabase/functions/_shared/utils.ts
index d4399cf2..b0a01551 100644
--- a/supabase/functions/_shared/utils.ts
+++ b/supabase/functions/_shared/utils.ts
@@ -1,7 +1,11 @@
import { corsHeaders } from "./cors.ts";
-export function createErrorResponse(status: number, message: string) {
- return new Response(JSON.stringify({ status, message }), {
+export function createErrorResponse(
+ status: number,
+ message: string,
+ custom: Record<string, unknown> = {},
+) {
+ return new Response(JSON.stringify({ status, message, ...custom }), {
headers: { "Content-Type": "application/json", ...corsHeaders },
status,
});
diff --git a/supabase/functions/users/index.ts b/supabase/functions/users/index.ts
index 91edc1ef..ae28ca75 100644
--- a/supabase/functions/users/index.ts
+++ b/supabase/functions/users/index.ts
@@ -97,32 +97,72 @@ async function inviteUser(req: Request, currentUserSale: any) {
}
user = data[0];
- const sale = await createSale(user.id, {
- email,
- password,
- first_name,
- last_name,
- disabled,
- administrator,
- });
+ try {
+ const { data: existingSale, error: salesError } = await supabaseAdmin
+ .from("sales")
+ .select("*")
+ .eq("user_id", user.id);
+ if (salesError) {
+ return createErrorResponse(
+ salesError.status,
+ salesError.message,
+ {
+ code: salesError.code,
+ },
+ );
+ }
+ if (existingSale.length > 0) {
+ return createErrorResponse(
+ 400,
+ 'A sales for this email already exists',
+ );
+ }
- return new Response(
- JSON.stringify({
- data: sale,
- }),
- {
- headers: { "Content-Type": "application/json", ...corsHeaders },
- },
- );
+ const sale = await createSale(user.id, {
+ email,
+ password,
+ first_name,
+ last_name,
+ disabled,
+ administrator,
+ });
+
+ return new Response(
+ JSON.stringify({
+ data: sale,
+ }),
+ {
+ headers: { "Content-Type": "application/json", ...corsHeaders },
+ },
+ );
+ } catch (error) {
+ return createErrorResponse(
+ (error as any).status ?? 500,
+ (error as Error).message,
+ {
+ code: (error as any).code,
+ },
+ );
+ }
} else {
- const { error: emailError } =
- await supabaseAdmin.auth.admin.inviteUserByEmail(email);
- if (!user || userError) {
+ if (userError) {
console.error(`Error inviting user: user_error=${userError}`);
+ return createErrorResponse(
+ userError.status,
+ userError.message,
+ {
+ code: userError.code,
+ },
+ );
+ }
+ if (!data?.user) {
+ console.error("Error inviting user: undefined user");
return createErrorResponse(500, "Internal Server Error");
}
+ const { error: emailError } =
+ await supabaseAdmin.auth.admin.inviteUserByEmail(email);
- if (!user || userError || emailError) {
+ if (emailError) {
console.error(`Error inviting user, email_error=${emailError}`);
return createErrorResponse(500, "Failed to send invitation mail");
}