-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters