Skip to content

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

have addNoteToContact return a 500 response when getOrCreateContactFromEmailInfo fails

ThieryMichelef5b520df241
diff --git a/supabase/functions/postmark/addNoteToContact.test.ts b/supabase/functions/postmark/addNoteToContact.test.ts
index 95ffc230..7fd2e63d 100644
--- a/supabase/functions/postmark/addNoteToContact.test.ts
+++ b/supabase/functions/postmark/addNoteToContact.test.ts
@@ -1,6 +1,7 @@
// @vitest-environment node
import { describe, it, expect, vi, beforeEach } from "vitest";
import {
+ addNoteToContact,
getOrCreateCompanyFromDomain,
getOrCreateContactFromEmailInfo,
} from "./addNoteToContact";
@@ -287,4 +288,149 @@ describe("addNoteToContact", () => {
);
});
});
+
+ describe("addNoteToContact", () => {
+ const baseParams = {
+ salesEmail: "[email protected]",
+ email: "[email protected]",
+ domain: "acme.com",
+ firstName: "Alice",
+ lastName: "Smith",
+ noteContent: "A note",
+ attachments: [],
+ };
+
+ it("creates a note and returns undefined on success", async () => {
+ const salesRecord = { id: 1, email: "[email protected]" };
+ const existingContact = {
+ id: 10,
+ first_name: "Alice",
+ last_name: "Smith",
+ };
+
+ mockFrom
+ .mockReturnValueOnce({
+ // 1st call: fetch sales → found
+ select: () => ({
+ eq: () => ({
+ neq: () => ({
+ maybeSingle: () =>
+ Promise.resolve({ data: salesRecord, error: null }),
+ }),
+ }),
+ }),
+ })
+ .mockReturnValueOnce({
+ // 2nd call: fetch contact → found
+ select: () => ({
+ contains: () => ({
+ maybeSingle: () =>
+ Promise.resolve({ data: existingContact, error: null }),
+ }),
+ }),
+ })
+ .mockReturnValueOnce({
+ // 3rd call: insert note into contact_notes → success
+ insert: () => Promise.resolve({ error: null }),
+ })
+ .mockReturnValueOnce({
+ // 4th call: update contacts.last_seen
+ update: () => ({
+ eq: () => Promise.resolve({ error: null }),
+ }),
+ });
+
+ const result = await addNoteToContact(baseParams);
+
+ expect(result).toBeUndefined();
+ expect(mockFrom).toHaveBeenCalledTimes(4);
+ expect(mockFrom).toHaveBeenNthCalledWith(3, "contact_notes");
+ expect(mockFrom).toHaveBeenNthCalledWith(4, "contacts");
+ });
+
+ it("returns 500 when inserting the note into contact_notes fails", async () => {
+ const salesRecord = { id: 1, email: "[email protected]" };
+ const existingContact = {
+ id: 10,
+ first_name: "Alice",
+ last_name: "Smith",
+ };
+
+ mockFrom
+ .mockReturnValueOnce({
+ // 1st call: fetch sales → found
+ select: () => ({
+ eq: () => ({
+ neq: () => ({
+ maybeSingle: () =>
+ Promise.resolve({ data: salesRecord, error: null }),
+ }),
+ }),
+ }),
+ })
+ .mockReturnValueOnce({
+ // 2nd call: fetch contact → found
+ select: () => ({
+ contains: () => ({
+ maybeSingle: () =>
+ Promise.resolve({ data: existingContact, error: null }),
+ }),
+ }),
+ })
+ .mockReturnValueOnce({
+ // 3rd call: insert note into contact_notes → fails
+ insert: () =>
+ Promise.resolve({ error: { message: "Insert failed" } }),
+ });
+
+ const response = await addNoteToContact(baseParams);
+
+ expect(response).toBeInstanceOf(Response);
+ expect(response!.status).toBe(500);
+ expect(await response!.text()).toBe(
+ "Could not add note to contact [email protected], sales [email protected]",
+ );
+ expect(mockFrom).toHaveBeenCalledTimes(3);
+ });
+
+ it("returns 500 when getOrCreateContactFromEmailInfo throws", async () => {
+ const salesRecord = { id: 1, email: "[email protected]" };
+ const consoleSpy = vi
+ .spyOn(console, "error")
+ .mockImplementation(() => {});
+
+ mockFrom
+ .mockReturnValueOnce({
+ // 1st call: fetch sales → found
+ select: () => ({
+ eq: () => ({
+ neq: () => ({
+ maybeSingle: () =>
+ Promise.resolve({ data: salesRecord, error: null }),
+ }),
+ }),
+ }),
+ })
+ .mockReturnValueOnce({
+ // 2nd call: fetch contact → DB error, causes getOrCreateContactFromEmailInfo to throw
+ select: () => ({
+ contains: () => ({
+ maybeSingle: () =>
+ Promise.resolve({ data: null, error: { message: "DB error" } }),
+ }),
+ }),
+ });
+
+ const response = await addNoteToContact(baseParams);
+
+ expect(response).toBeInstanceOf(Response);
+ expect(response!.status).toBe(500);
+ expect(await response!.text()).toBe(
+ "Could not get or create contact from database, email: [email protected], sales: [email protected]",
+ );
+ expect(consoleSpy).toHaveBeenCalled();
+
+ consoleSpy.mockRestore();
+ });
+ });
});
diff --git a/supabase/functions/postmark/addNoteToContact.ts b/supabase/functions/postmark/addNoteToContact.ts
index 7627b340..b9f71e59 100644
--- a/supabase/functions/postmark/addNoteToContact.ts
+++ b/supabase/functions/postmark/addNoteToContact.ts
@@ -133,13 +133,36 @@ export const addNoteToContact = async ({
);
}
- const contact = await getOrCreateContactFromEmailInfo({
+ const { contact, error } = await getOrCreateContactFromEmailInfo({
email,
firstName,
lastName,
salesId: sales.id,
domain,
- });
+ })
+ .then((contact) => ({
+ contact,
+ }))
+ .catch((error) => {
+ return {
+ error,
+ };
+ });
+
+ if (error) {
+ console.error(
+ "Error in getOrCreateContactFromEmailInfo for email:",
+ email,
+ "sales:",
+ salesEmail,
+ "error:",
+ error,
+ );
+ return new Response(
+ `Could not get or create contact from database, email: ${email}, sales: ${salesEmail}`,
+ { status: 500 },
+ );
+ }
// Add note to contact
const { error: createNoteError } = await supabaseAdmin