Skip to content

Commit

Permalink
[data_source] - refactor: update email sending method for data source…
Browse files Browse the repository at this point in the history
… requests

 - Replace direct sendEmail call with API endpoint for sending request data source emails
 - Include WorkspaceType owner in the email sending payload to scope the request properly
 - Change parameter names to better reflect their purpose in the email sending context
 - Improve error handling for the email sending process by checking response status and providing error messages
 - Pass owner information when opening RequestDataSourcesModal for proper email request scoping
  • Loading branch information
Jules authored and Jules committed Aug 6, 2024
1 parent 48a0b5b commit 3750205
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
48 changes: 30 additions & 18 deletions front/components/data_source/RequestDataSourcesModal.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Button, DropdownMenu, Modal, TextArea } from "@dust-tt/sparkle";
import type { WorkspaceType } from "@dust-tt/types";
import React, { useContext, useState } from "react";

import { SendNotificationsContext } from "@app/components/sparkle/Notification";
import { CONNECTOR_CONFIGURATIONS } from "@app/lib/connector_providers";
import { sendEmail } from "@app/lib/email";
import logger from "@app/logger/logger";
import type { DataSourceIntegration } from "@app/pages/w/[wId]/builder/data-sources/managed";

Expand All @@ -12,38 +12,49 @@ type RequestDataSourceProps = {
onClose: () => void;
dataSourceIntegrations: DataSourceIntegration[];
currentUserEmail: string;
owner: WorkspaceType;
};

async function sendRequestDataSourceEmail({
email,
emailContent,
emailMessage,
dataSourceName,
ccEmail,
emailRequester,
owner,
}: {
email: string;
emailContent: string;
emailMessage: string;
dataSourceName: string;
ccEmail?: string;
emailRequester?: string;
owner: WorkspaceType;
}) {
const mail = {
from: {
name: "Dust team",
email: "team@dust.tt",
const res = await fetch(`/api/w/${owner.sId}/data_sources/request-email`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
subject: `[Dust] Request Data source - ${dataSourceName}`,
html: `<p>${ccEmail} has sent you a request regarding the connection ${dataSourceName}</p>
<p>Message:</p>
<p>${emailContent}</p>
<p>Email to: ${email}</p>`,
};
return sendEmail("belvezejules@gmail.com", mail);
body: JSON.stringify({
email,
emailMessage,
dataSourceName,
emailRequester,
}),
});

if (!res.ok) {
const errorData = await res.json();
throw new Error(errorData.error?.message || "Failed to send email");
}

return res.json();
}

export function RequestDataSourcesModal({
isOpen,
onClose,
dataSourceIntegrations,
currentUserEmail,
owner,
}: RequestDataSourceProps) {
const [selectedDataSourceIntegration, setSelectedDataSourceIntegration] =
useState<DataSourceIntegration | null>(null);
Expand Down Expand Up @@ -154,9 +165,10 @@ export function RequestDataSourcesModal({
try {
await sendRequestDataSourceEmail({
email: userEmail,
emailContent: message,
emailMessage: message,
dataSourceName: selectedDataSourceIntegration.name,
ccEmail: currentUserEmail,
emailRequester: currentUserEmail,
owner,
});
} catch (e) {
logger.error(
Expand Down
1 change: 1 addition & 0 deletions front/pages/w/[wId]/builder/data-sources/managed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ export default function DataSourcesView({
onClose={() => setIsRequestDataSourceModalOpen(false)}
dataSourceIntegrations={dataSourceIntegrations}
currentUserEmail={user.email}
owner={owner}
/>
</Page.Vertical>
</AppLayout>
Expand Down

0 comments on commit 3750205

Please sign in to comment.