A full-featured CRM built with React, shadcn/ui, and Supabase.
Stop interpolating user input into the PostgREST filter
Jeromedf5ebb9115ab
diff --git a/src/components/atomic-crm/providers/supabase/dataProvider.ts b/src/components/atomic-crm/providers/supabase/dataProvider.tsindex 2d902856..d2cac777 100644--- a/src/components/atomic-crm/providers/supabase/dataProvider.ts+++ b/src/components/atomic-crm/providers/supabase/dataProvider.ts@@ -130,7 +130,10 @@ const getDataProviderWithCustomMethods = () => {return {};}})();- throw new Error(errorDetails?.message || "Failed to create the user");+ throw Object.assign(+ new Error(errorDetails?.message || "Failed to create the user"),+ { code: errorDetails?.code, email: errorDetails?.email },+ );}return data.data;diff --git a/src/components/atomic-crm/sales/SalesCreate.tsx b/src/components/atomic-crm/sales/SalesCreate.tsxindex 2b6458f9..9d336c9c 100644--- a/src/components/atomic-crm/sales/SalesCreate.tsx+++ b/src/components/atomic-crm/sales/SalesCreate.tsx@@ -6,6 +6,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";import type { CrmDataProvider } from "../providers/types";import type { SalesFormData } from "../types";+import { getSalesErrorNotification } from "./salesErrorNotification";import { SalesInputs } from "./SalesInputs";export function SalesCreate() {@@ -28,15 +29,17 @@ export function SalesCreate() {redirect("/sales");},onError: (error) => {- notify(- error.message ||- translate("resources.sales.create.error", {- _: "An error occurred while creating the user.",- }),- {- type: "error",- },+ const { message, args } = getSalesErrorNotification(+ error,+ "resources.sales.create.error",);+ notify(message, {+ type: "error",+ messageArgs: {+ ...args,+ _: "An error occurred while creating the user.",+ },+ });},});const onSubmit: SubmitHandler<SalesFormData> = async (data) => {diff --git a/src/components/atomic-crm/sales/SalesEdit.tsx b/src/components/atomic-crm/sales/SalesEdit.tsxindex db9a7d3e..02be2238 100644--- a/src/components/atomic-crm/sales/SalesEdit.tsx+++ b/src/components/atomic-crm/sales/SalesEdit.tsx@@ -15,6 +15,7 @@ import { Card, CardContent } from "@/components/ui/card";import type { CrmDataProvider } from "../providers/types";import type { Sale, SalesFormData } from "../types";+import { getSalesErrorNotification } from "./salesErrorNotification";import { SalesInputs } from "./SalesInputs";function EditToolbar() {@@ -54,12 +55,14 @@ export function SalesEdit() {},});},- onError: () => {- notify("resources.sales.edit.error", {+ onError: (error) => {+ const { message, args } = getSalesErrorNotification(+ error,+ "resources.sales.edit.error",+ );+ notify(message, {type: "error",- messageArgs: {- _: "An error occurred. Please try again.",- },+ messageArgs: { ...args, _: "An error occurred. Please try again." },});},});diff --git a/src/components/atomic-crm/sales/salesUpdateError.test.ts b/src/components/atomic-crm/sales/salesErrorNotification.test.tssimilarity index 55%rename from src/components/atomic-crm/sales/salesUpdateError.test.tsrename to src/components/atomic-crm/sales/salesErrorNotification.test.tsindex 94b14e15..0ac0bd4f 100644--- a/src/components/atomic-crm/sales/salesUpdateError.test.ts+++ b/src/components/atomic-crm/sales/salesErrorNotification.test.ts@@ -1,10 +1,10 @@import { describe, it, expect } from "vitest";-import { getSalesUpdateNotification } from "./salesUpdateError";+import { getSalesErrorNotification } from "./salesErrorNotification";-describe("getSalesUpdateNotification", () => {+describe("getSalesErrorNotification", () => {it("names the address rejected as already taken", () => {expect(- getSalesUpdateNotification(+ getSalesErrorNotification(Object.assign(new Error("Secondary email already used"), {code: "secondary_email_taken",email: "[email protected]",@@ -18,7 +18,7 @@ describe("getSalesUpdateNotification", () => {it("names the address rejected as malformed", () => {expect(- getSalesUpdateNotification(+ getSalesErrorNotification(Object.assign(new Error("Invalid secondary email"), {code: "invalid_secondary_email",email: "not-an-email",@@ -32,22 +32,46 @@ describe("getSalesUpdateNotification", () => {it("falls back to the generic message for an unknown code", () => {expect(- getSalesUpdateNotification(+ getSalesErrorNotification(Object.assign(new Error("boom"), { code: "something_else" }),).message,).toBe("crm.profile.update_error");});it("falls back to the generic message for an error carrying no code", () => {- expect(getSalesUpdateNotification(new Error("boom")).message).toBe(+ expect(getSalesErrorNotification(new Error("boom")).message).toBe("crm.profile.update_error",);});it("does not throw when there is no error object at all", () => {- expect(getSalesUpdateNotification(undefined)).toEqual({+ expect(getSalesErrorNotification(undefined)).toEqual({message: "crm.profile.update_error",args: { email: "" },});});++ it("uses the caller's fallback so admin pages keep their own wording", () => {+ expect(+ getSalesErrorNotification(+ new Error("boom"),+ "resources.sales.create.error",+ ).message,+ ).toBe("resources.sales.create.error");+ });++ it("keeps the specific message over the caller's fallback", () => {+ expect(+ getSalesErrorNotification(+ Object.assign(new Error("taken"), {+ code: "email_taken",+ email: "[email protected]",+ }),+ "resources.sales.create.error",+ ),+ ).toEqual({+ message: "crm.profile.email_taken",+ args: { email: "[email protected]" },+ });+ });});diff --git a/src/components/atomic-crm/sales/salesUpdateError.ts b/src/components/atomic-crm/sales/salesErrorNotification.tssimilarity index 67%rename from src/components/atomic-crm/sales/salesUpdateError.tsrename to src/components/atomic-crm/sales/salesErrorNotification.tsindex 25b322f6..3f4b3f8b 100644--- a/src/components/atomic-crm/sales/salesUpdateError.ts+++ b/src/components/atomic-crm/sales/salesErrorNotification.ts@@ -1,4 +1,4 @@-export type SalesUpdateNotification = {+export type SalesErrorNotification = {message: string;args: Record<string, string>;};@@ -7,17 +7,17 @@ const MESSAGE_BY_CODE: Record<string, string> = {email_taken: "crm.profile.email_taken",secondary_email_taken: "crm.profile.secondary_email_taken",invalid_secondary_email: "crm.profile.secondary_email_invalid",- invalid_secondary_emails_payload: "crm.profile.update_error",};-export const getSalesUpdateNotification = (+export const getSalesErrorNotification = (error: unknown,-): SalesUpdateNotification => {+ fallbackMessage = "crm.profile.update_error",+): SalesErrorNotification => {const { code, email } = (error ?? {}) as { code?: string; email?: string };const message = code ? MESSAGE_BY_CODE[code] : undefined;return {- message: message ?? "crm.profile.update_error",+ message: message ?? fallbackMessage,args: { email: email ?? "" },};};diff --git a/src/components/atomic-crm/settings/ProfilePage.tsx b/src/components/atomic-crm/settings/ProfilePage.tsxindex 5f1e8b22..d30c3c05 100644--- a/src/components/atomic-crm/settings/ProfilePage.tsx+++ b/src/components/atomic-crm/settings/ProfilePage.tsx@@ -36,7 +36,7 @@ import {import ImageEditorField from "../misc/ImageEditorField";import type { CrmDataProvider } from "../providers/types";-import { getSalesUpdateNotification } from "../sales/salesUpdateError";+import { getSalesErrorNotification } from "../sales/salesErrorNotification";import type { Sale, SalesFormData } from "../types";export const ProfilePage = () => {@@ -72,7 +72,7 @@ export const ProfilePage = () => {});},onError: (error) => {- const { message, args } = getSalesUpdateNotification(error);+ const { message, args } = getSalesErrorNotification(error);notify(message, {type: "error",messageArgs: { ...args, _: "An error occurred. Please try again" },diff --git a/src/components/atomic-crm/settings/SettingsPageMobile.tsx b/src/components/atomic-crm/settings/SettingsPageMobile.tsxindex b091a424..e453227d 100644--- a/src/components/atomic-crm/settings/SettingsPageMobile.tsx+++ b/src/components/atomic-crm/settings/SettingsPageMobile.tsx@@ -47,7 +47,7 @@ import MobileHeader from "../layout/MobileHeader";import { ChangelogPage } from "../misc/ChangelogPage";import ImageEditorField from "../misc/ImageEditorField";import type { CrmDataProvider } from "../providers/types";-import { getSalesUpdateNotification } from "../sales/salesUpdateError";+import { getSalesErrorNotification } from "../sales/salesErrorNotification";import type { SalesFormData } from "../types";const ChangePasswordButton = () => {@@ -179,7 +179,7 @@ const ProfileSection = () => {});} catch (error) {queryClient.setQueryData(queryKey, previousData);- const { message, args } = getSalesUpdateNotification(error);+ const { message, args } = getSalesErrorNotification(error);notify(message, {type: "error",messageArgs: { ...args, _: "An error occurred. Please try again" },diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.tsindex 4cf50eae..cae366f8 100644--- a/supabase/functions/postmark/index.ts+++ b/supabase/functions/postmark/index.ts@@ -63,10 +63,11 @@ Deno.serve(async (req) => {const salesEmails =allSales.data- ?.flatMap((s: { email: string; secondary_emails: string[] | null }) => [+ ?.flatMap((s: { email: string; secondary_emails: unknown }) => [s.email,- ...(s.secondary_emails ?? []),+ ...(Array.isArray(s.secondary_emails) ? s.secondary_emails : []),])+ .filter((email: unknown): email is string => typeof email === "string").map((email: string) => email.toLowerCase()) ?? [];const firstToEmail = (ToFull[0]?.Email || "").toLowerCase();diff --git a/supabase/functions/users/index.ts b/supabase/functions/users/index.tsindex e427bbf3..39730da1 100644--- a/supabase/functions/users/index.ts+++ b/supabase/functions/users/index.ts@@ -60,11 +60,15 @@ async function findEmailUsedByAnotherSale(emails: string[],excludeSalesId?: number,) {- for (const email of emails) {- let query = supabaseAdmin- .from("sales")- .select("id")- .or(`email.eq.${email},secondary_emails.cs.${JSON.stringify([email])}`);+ const isTakenBy = async (+ column: "email" | "secondary_emails",+ email: string,+ ) => {+ const selected = supabaseAdmin.from("sales").select("id");+ let query =+ column === "email"+ ? selected.eq("email", email)+ : selected.contains("secondary_emails", JSON.stringify([email]));if (excludeSalesId !== undefined) {query = query.neq("id", excludeSalesId);@@ -77,7 +81,14 @@ async function findEmailUsedByAnotherSale(throw salesError;}- if (matches?.length) {+ return Boolean(matches?.length);+ };++ for (const email of emails) {+ if (+ (await isTakenBy("email", email)) ||+ (await isTakenBy("secondary_emails", email))+ ) {return email;}}