Skip to content

Commit

Permalink
Clarify code
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph committed Dec 18, 2024
1 parent 65e5316 commit 0347b02
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 85 deletions.
125 changes: 62 additions & 63 deletions front/lib/api/tracker.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,88 @@
import type { TrackerConfigurationType } from "@dust-tt/types";
import type { TrackerGenerationToProcess } from "@dust-tt/types";

import { sendEmailWithTemplate } from "@app/lib/api/email";

const TRACKER_FROM_EMAIL = "[email protected]";
const TRACKER_FROM_NAME = "Bob Tracker"; // 😬

export async function sendTrackerEmailWithNoGeneration(
tracker: TrackerConfigurationType
): Promise<void> {
const recipients = new Set(tracker.recipients);
if (!recipients.size) {
export async function sendTrackerEmail({
name,
recipients,
generations,
}: {
name: string;
recipients: string[];
generations: TrackerGenerationToProcess[];
}): Promise<void> {
const uniqueRecipients = new Set(recipients);
if (!uniqueRecipients.size) {
throw new Error("No recipients found for tracker");
}

const _sendTrackerEmailWithNoGenerationToRecipient = async (
recipient: string,
tracker: TrackerConfigurationType
): Promise<void> => {
await sendEmailWithTemplate({
to: recipient,
from: {
name: TRACKER_FROM_NAME,
email: TRACKER_FROM_EMAIL,
},
subject: `[Dust] Tracker ${tracker.name} check complete: No updates required.`,
body: `
<p>Tracker: ${tracker.name}.</p>
<p>No changes detected in watched documents. All maintained documents are current</p>
`,
});
};
const sendEmail =
generations.length > 0
? _sendTrackerWithGenerationEmail
: _sendTrackerDefaultEmail;

await Promise.all(
Array.from(recipients).map((recipient) =>
_sendTrackerEmailWithNoGenerationToRecipient(recipient, tracker)
sendEmail({ name, recipient, generations })
)
);
}

export async function sendTrackerEmailWithGenerations(
tracker: TrackerConfigurationType
): Promise<void> {
const recipients = new Set(tracker.recipients);
if (!recipients.size) {
throw new Error("No recipients found for tracker");
}

const generations = tracker.generations || [];
if (!generations?.length) {
throw new Error("No generations found for tracker");
}
const _sendTrackerDefaultEmail = async ({
name,
recipient,
}: {
name: string;
recipient: string;
}): Promise<void> => {
await sendEmailWithTemplate({
to: recipient,
from: {
name: TRACKER_FROM_NAME,
email: TRACKER_FROM_EMAIL,
},
subject: `[Dust] Tracker ${name} check complete: No updates required.`,
body: `
<p>Tracker: ${name}.</p>
<p>No changes detected in watched documents. All maintained documents are current</p>
`,
});
};

const _sendTrackerEmailWithNoGenerationToRecipient = async (
recipient: string,
tracker: TrackerConfigurationType
): Promise<void> => {
const generationsBody = generations
.map(
(generation) => `
const _sendTrackerWithGenerationEmail = async ({
name,
recipient,
generations,
}: {
name: string;
recipient: string;
generations: TrackerGenerationToProcess[];
}): Promise<void> => {
const generationsBody = generations
.map(
(generation) => `
<p>Generation: ${generation.id}.</p>
<p>Changes: ${generation.content}.</p>
<p>Document: ${generation.documentId}.</p>
`
)
.join("");
)
.join("");

await sendEmailWithTemplate({
to: recipient,
from: {
name: TRACKER_FROM_NAME,
email: TRACKER_FROM_EMAIL,
},
subject: `[Dust] Tracker ${tracker.name} check complete: Updates required.`,
body: `
<p>Tracker: ${tracker.name}.</p>
await sendEmailWithTemplate({
to: recipient,
from: {
name: TRACKER_FROM_NAME,
email: TRACKER_FROM_EMAIL,
},
subject: `[Dust] Tracker ${name} check complete: Updates required.`,
body: `
<p>Tracker: ${name}.</p>
<p>Suggested changes detected in watched documents: ${generations.length}.</p>
<p>Changes:</p>
${generationsBody}
`,
});
};

await Promise.all(
Array.from(recipients).map((recipient) =>
_sendTrackerEmailWithNoGenerationToRecipient(recipient, tracker)
)
);
}
});
};
31 changes: 9 additions & 22 deletions front/temporal/tracker/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import type {
import { ConnectorsAPI, CoreAPI, Err, Ok } from "@dust-tt/types";

import config from "@app/lib/api/config";
import {
sendTrackerEmailWithGenerations,
sendTrackerEmailWithNoGeneration,
} from "@app/lib/api/tracker";
import { sendTrackerEmail } from "@app/lib/api/tracker";
import { Authenticator } from "@app/lib/auth";
import { getDocumentDiff } from "@app/lib/document_upsert_hooks/hooks/data_source_helpers";
import { callDocTrackerRetrievalAction } from "@app/lib/document_upsert_hooks/hooks/tracker/actions/doc_tracker_retrieval";
Expand Down Expand Up @@ -370,22 +367,12 @@ export const processTrackerNotificationWorkflowActivity = async ({
auth,
trackerId
);
if (!tracker) {
if (!tracker || !tracker.recipients?.length) {
localLogger.error(
{
trackerId,
},
"[Tracker] Tracker not found."
);
return;
}

if (!tracker.recipients?.length) {
localLogger.error(
{
trackerId: tracker.id,
},
"[Tracker] No recipients found for tracker. Should not be possible from the UI."
"[Tracker] Tracker not found or found without recipient. Should not be possible from the UI."
);
return;
}
Expand All @@ -394,13 +381,13 @@ export const processTrackerNotificationWorkflowActivity = async ({
`[Tracker] Processing tracker ${trackerId} for workspace ${workspaceId}.`
);

// Send the tracker email(s).
const generations = tracker.generations || [];

if (generations.length) {
await sendTrackerEmailWithNoGeneration(tracker);
} else {
await sendTrackerEmailWithGenerations(tracker);
}
await sendTrackerEmail({
name: tracker.name,
recipients: tracker.recipients,
generations,
});

// Consume the tracker & associated generations.
await TrackerConfigurationResource.consumeGenerations({
Expand Down

0 comments on commit 0347b02

Please sign in to comment.