Skip to content

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

Propagate webhook failures and bound the secondary email list

Jerome823256b0a970
diff --git a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
index 0b973711..7cb65081 100644
--- a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
@@ -534,7 +534,10 @@ export const englishCrmMessages = {
email_taken: "%{email} is already used by another user",
no_secondary_emails: "None",
secondary_email_invalid: "%{email} is not a valid email address",
+ secondary_email_is_primary: "%{email} is already your main address",
secondary_email_taken: "%{email} is already used by another user",
+ too_many_secondary_emails:
+ "You cannot add more than 10 secondary email addresses",
secondary_emails_help:
"Other addresses you send emails from. Leave one empty to remove it.",
inbound: {
diff --git a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
index 089c82ea..64c13cbd 100644
--- a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
@@ -541,8 +541,11 @@ export const frenchCrmMessages = {
email_taken: "%{email} est déjà utilisée par un autre utilisateur",
no_secondary_emails: "Aucun",
secondary_email_invalid: "%{email} n'est pas une adresse e-mail valide",
+ secondary_email_is_primary: "%{email} est déjà votre adresse principale",
secondary_email_taken:
"%{email} est déjà utilisée par un autre utilisateur",
+ too_many_secondary_emails:
+ "Vous ne pouvez pas ajouter plus de 10 adresses e-mail secondaires",
secondary_emails_help:
"Autres adresses depuis lesquelles vous envoyez des e-mails. Laissez-en une vide pour la supprimer.",
inbound: {
diff --git a/src/components/atomic-crm/sales/SalesCreate.tsx b/src/components/atomic-crm/sales/SalesCreate.tsx
index 9d336c9c..c692045f 100644
--- a/src/components/atomic-crm/sales/SalesCreate.tsx
+++ b/src/components/atomic-crm/sales/SalesCreate.tsx
@@ -31,15 +31,12 @@ export function SalesCreate() {
onError: (error) => {
const { message, args } = getSalesErrorNotification(
error,
- "resources.sales.create.error",
+ error.message ||
+ translate("resources.sales.create.error", {
+ _: "An error occurred while creating the user.",
+ }),
);
- notify(message, {
- type: "error",
- messageArgs: {
- ...args,
- _: "An error occurred while creating the user.",
- },
- });
+ notify(message, { type: "error", messageArgs: args });
},
});
const onSubmit: SubmitHandler<SalesFormData> = async (data) => {
diff --git a/src/components/atomic-crm/sales/salesErrorNotification.test.ts b/src/components/atomic-crm/sales/salesErrorNotification.test.ts
index 0ac0bd4f..17dc45ee 100644
--- a/src/components/atomic-crm/sales/salesErrorNotification.test.ts
+++ b/src/components/atomic-crm/sales/salesErrorNotification.test.ts
@@ -51,6 +51,39 @@ describe("getSalesErrorNotification", () => {
});
});
+ it("names the address that is already the main one", () => {
+ expect(
+ getSalesErrorNotification(
+ Object.assign(new Error("already primary"), {
+ code: "secondary_email_is_primary",
+ email: "[email protected]",
+ }),
+ ),
+ ).toEqual({
+ message: "crm.profile.secondary_email_is_primary",
+ args: { email: "[email protected]" },
+ });
+ });
+
+ it("reports the cap without needing an address", () => {
+ expect(
+ getSalesErrorNotification(
+ Object.assign(new Error("too many"), {
+ code: "too_many_secondary_emails",
+ }),
+ ).message,
+ ).toBe("crm.profile.too_many_secondary_emails");
+ });
+
+ it("keeps the server reason when the caller passes it as fallback", () => {
+ expect(
+ getSalesErrorNotification(
+ new Error("Password should be at least 6 characters"),
+ "Password should be at least 6 characters",
+ ).message,
+ ).toBe("Password should be at least 6 characters");
+ });
+
it("uses the caller's fallback so admin pages keep their own wording", () => {
expect(
getSalesErrorNotification(
diff --git a/src/components/atomic-crm/sales/salesErrorNotification.ts b/src/components/atomic-crm/sales/salesErrorNotification.ts
index 3f4b3f8b..57dfd71b 100644
--- a/src/components/atomic-crm/sales/salesErrorNotification.ts
+++ b/src/components/atomic-crm/sales/salesErrorNotification.ts
@@ -6,7 +6,9 @@ export type SalesErrorNotification = {
const MESSAGE_BY_CODE: Record<string, string> = {
email_taken: "crm.profile.email_taken",
secondary_email_taken: "crm.profile.secondary_email_taken",
+ secondary_email_is_primary: "crm.profile.secondary_email_is_primary",
invalid_secondary_email: "crm.profile.secondary_email_invalid",
+ too_many_secondary_emails: "crm.profile.too_many_secondary_emails",
};
export const getSalesErrorNotification = (
diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.ts
index cae366f8..77925c51 100644
--- a/supabase/functions/postmark/index.ts
+++ b/supabase/functions/postmark/index.ts
@@ -130,7 +130,7 @@ Deno.serve(async (req) => {
});
}
- await addNoteToContact({
+ const errorResponse = await addNoteToContact({
salesEmail,
email,
domain,
@@ -141,6 +141,10 @@ Deno.serve(async (req) => {
companyName,
website,
});
+
+ if (errorResponse) {
+ return errorResponse;
+ }
}
return new Response("OK");
diff --git a/supabase/functions/users/index.ts b/supabase/functions/users/index.ts
index 39730da1..4a94b5f1 100644
--- a/supabase/functions/users/index.ts
+++ b/supabase/functions/users/index.ts
@@ -6,6 +6,7 @@ import { AuthMiddleware, UserMiddleware } from "../_shared/authentication.ts";
import { getUserSale } from "../_shared/getUserSale.ts";
import {
findInvalidEmail,
+ MAX_SECONDARY_EMAILS,
normalizeSecondaryEmails,
} from "./secondaryEmails.ts";
@@ -297,6 +298,14 @@ async function patchUser(req: Request, currentUserSale: any) {
});
}
+ if (normalizedSecondaryEmails.length > MAX_SECONDARY_EMAILS) {
+ return createErrorResponse(
+ 400,
+ `No more than ${MAX_SECONDARY_EMAILS} secondary emails are allowed`,
+ { code: "too_many_secondary_emails" },
+ );
+ }
+
const invalidEmail = findInvalidEmail(normalizedSecondaryEmails);
if (invalidEmail) {
return createErrorResponse(
@@ -306,6 +315,15 @@ async function patchUser(req: Request, currentUserSale: any) {
);
}
+ const primaryEmail = (email ?? sale.email).trim().toLowerCase();
+ if (normalizedSecondaryEmails.includes(primaryEmail)) {
+ return createErrorResponse(
+ 409,
+ `Secondary email is already the main address: ${primaryEmail}`,
+ { code: "secondary_email_is_primary", email: primaryEmail },
+ );
+ }
+
const takenEmail = await findEmailUsedByAnotherSale(
normalizedSecondaryEmails,
sales_id,
diff --git a/supabase/functions/users/secondaryEmails.ts b/supabase/functions/users/secondaryEmails.ts
index 5da7912c..2386d2e0 100644
--- a/supabase/functions/users/secondaryEmails.ts
+++ b/supabase/functions/users/secondaryEmails.ts
@@ -1,5 +1,7 @@
const EMAIL_PATTERN = /^[^\s@,]+@[^\s@,]+\.[^\s@,]+$/;
+export const MAX_SECONDARY_EMAILS = 10;
+
export const normalizeSecondaryEmails = (
secondaryEmails: unknown,
): string[] | undefined => {