Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/emails/__tests__/getEmailFooter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@ describe("getEmailFooter", () => {
expect(footer).toContain("font-size:12px");
expect(footer).toContain("color:#6b7280");
});

it("includes artist workspace when artistName is provided", () => {
const footer = getEmailFooter("room-id", "Taylor Swift");
expect(footer).toContain("From Taylor Swift's workspace");
});

it("excludes artist line when artistName is not provided", () => {
const footer = getEmailFooter("room-id");
expect(footer).not.toContain("workspace");
});

it("includes artist workspace without roomId", () => {
const footer = getEmailFooter(undefined, "Drake");
expect(footer).toContain("From Drake's workspace");
expect(footer).not.toContain("chat.recoupable.com");
});
});
11 changes: 10 additions & 1 deletion lib/emails/getEmailFooter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
* Generates a standardized email footer HTML.
*
* @param roomId - Optional room ID for the chat link. If not provided, only the reply note is shown.
* @param artistName - Optional artist name to display in the footer.
* @returns HTML string for the email footer.
*/
export function getEmailFooter(roomId?: string): string {
export function getEmailFooter(roomId?: string, artistName?: string): string {
const artistLine = artistName
? `
<p style="font-size:12px;color:#6b7280;margin:0 0 4px;">
From ${artistName}'s workspace
</p>`.trim()
: "";

const replyNote = `
<p style="font-size:12px;color:#6b7280;margin:0 0 4px;">
Note: you can reply directly to this email to continue the conversation.
Expand All @@ -22,6 +30,7 @@ export function getEmailFooter(roomId?: string): string {

return `
<hr style="margin-top:24px;margin-bottom:16px;border:none;border-top:1px solid #e5e7eb;" />
${artistLine}
${replyNote}
${chatLink}`.trim();
}
4 changes: 3 additions & 1 deletion lib/emails/inbound/generateEmailResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChatRequestBody } from "@/lib/chat/validateChatRequest";
import getGeneralAgent from "@/lib/agents/generalAgent/getGeneralAgent";
import { getEmailRoomMessages } from "@/lib/emails/inbound/getEmailRoomMessages";
import { getEmailFooter } from "@/lib/emails/getEmailFooter";
import { selectRoomWithArtist } from "@/lib/supabase/rooms/selectRoomWithArtist";

/**
* Generates the assistant response HTML for an email, including:
Expand All @@ -29,8 +30,9 @@ export async function generateEmailResponse(
const chatResponse = await agent.generate({ messages });
const text = chatResponse.text;

const roomData = await selectRoomWithArtist(roomId);
const bodyHtml = marked(text);
const footerHtml = getEmailFooter(roomId);
const footerHtml = getEmailFooter(roomId, roomData?.artist_name || undefined);
const html = `${bodyHtml}\n\n${footerHtml}`;

return { text, html };
Expand Down
5 changes: 5 additions & 0 deletions lib/mcp/tools/__tests__/registerSendEmailTool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import { registerSendEmailTool } from "../registerSendEmailTool";
import { NextResponse } from "next/server";

const mockSendEmailWithResend = vi.fn();
const mockSelectRoomWithArtist = vi.fn();

vi.mock("@/lib/emails/sendEmail", () => ({
sendEmailWithResend: (...args: unknown[]) => mockSendEmailWithResend(...args),
}));

vi.mock("@/lib/supabase/rooms/selectRoomWithArtist", () => ({
selectRoomWithArtist: (...args: unknown[]) => mockSelectRoomWithArtist(...args),
}));

describe("registerSendEmailTool", () => {
let mockServer: McpServer;
let registeredHandler: (args: unknown) => Promise<unknown>;
Expand Down
4 changes: 3 additions & 1 deletion lib/mcp/tools/registerSendEmailTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getToolResultSuccess } from "@/lib/mcp/getToolResultSuccess";
import { getToolResultError } from "@/lib/mcp/getToolResultError";
import { RECOUP_FROM_EMAIL } from "@/lib/const";
import { getEmailFooter } from "@/lib/emails/getEmailFooter";
import { selectRoomWithArtist } from "@/lib/supabase/rooms/selectRoomWithArtist";
import { NextResponse } from "next/server";
import { marked } from "marked";

Expand All @@ -24,7 +25,8 @@ export function registerSendEmailTool(server: McpServer): void {
async (args: SendEmailInput) => {
const { to, cc = [], subject, text, html = "", headers = {}, room_id } = args;

const footer = getEmailFooter(room_id);
const roomData = room_id ? await selectRoomWithArtist(room_id) : null;
const footer = getEmailFooter(room_id, roomData?.artist_name || undefined);
const bodyHtml = html || (text ? marked(text) : "");
const htmlWithFooter = `${bodyHtml}\n\n${footer}`;

Expand Down
31 changes: 31 additions & 0 deletions lib/supabase/rooms/selectRoomWithArtist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import supabase from "../serverClient";

type RoomWithArtist = {
id: string;
artist_id: string | null;
artist_name: string | null;
};

/**
* Select a room with its associated artist name from accounts table.
*
* @param roomId - The room ID to query.
* @returns The room data with artist name, or null if not found.
*/
export async function selectRoomWithArtist(roomId: string): Promise<RoomWithArtist | null> {
const { data, error } = await supabase
.from("rooms")
.select("id, artist_id, accounts!rooms_artist_id_fkey(name)")
.eq("id", roomId)
.single();

if (error || !data) return null;

const account = Array.isArray(data.accounts) ? data.accounts[0] : data.accounts;

return {
id: data.id,
artist_id: data.artist_id,
artist_name: account?.name || null,
};
}