2025-07-24 15:29:41 +00:00
|
|
|
import * as Sentry from "@sentry/node"
|
|
|
|
|
import { prisma } from "@codeflash-ai/common"
|
2025-07-24 16:10:17 +00:00
|
|
|
import { loadAndRenderHtml, sendEmail } from "../resend/email-service.js"
|
2025-07-24 15:29:41 +00:00
|
|
|
import { Response, Request } from "express"
|
2025-12-15 16:02:20 +00:00
|
|
|
import { logger } from "../utils/logger.js"
|
2026-01-19 17:33:57 +00:00
|
|
|
import { internalServerError } from "../exceptions/index.js"
|
2025-07-24 15:29:41 +00:00
|
|
|
|
|
|
|
|
export async function sendOptimizationCompletedEmail(req: Request, res: Response): Promise<void> {
|
|
|
|
|
const user = await prisma.users.findUnique({
|
|
|
|
|
where: { user_id: (req as any).userId },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (user?.email && user?.name) {
|
|
|
|
|
const { repo, owner } = req.body
|
|
|
|
|
const showRepo = owner && repo
|
|
|
|
|
try {
|
2025-07-24 16:38:14 +00:00
|
|
|
const html = await loadAndRenderHtml("./resend/completed_optimization_email_template.html", {
|
2025-07-24 15:29:41 +00:00
|
|
|
userName: user.github_username,
|
|
|
|
|
...{
|
|
|
|
|
PR: showRepo
|
|
|
|
|
? ` <div style="text-align: center; margin: 30px 0;">
|
|
|
|
|
<a href="https://github.com/${owner}/${repo}/pulls/app%2Fcodeflash-ai" class="cta-button">
|
|
|
|
|
<span class="pr-icon">🔀</span>
|
|
|
|
|
View Pull Requests
|
|
|
|
|
</a>
|
|
|
|
|
</div>
|
|
|
|
|
</div>`
|
|
|
|
|
: "",
|
|
|
|
|
repoHtml: showRepo
|
|
|
|
|
? `<a
|
|
|
|
|
href="https://github.com/${owner}/${repo}"
|
|
|
|
|
target="_blank"
|
|
|
|
|
style="text-decoration: none; color: inherit"
|
|
|
|
|
>
|
|
|
|
|
<div class="repo-info">
|
|
|
|
|
<img
|
|
|
|
|
class="repo-avatar"
|
|
|
|
|
src="https://github.com/${owner}.png?size=20"
|
|
|
|
|
alt="${owner}'s avatar"
|
|
|
|
|
/>
|
|
|
|
|
<div class="repo-name-text">${owner}/${repo}</div>
|
|
|
|
|
</div>
|
|
|
|
|
</a>`
|
|
|
|
|
: "",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
await sendEmail({
|
2026-04-13 16:03:05 +00:00
|
|
|
to: String(user.email),
|
2025-07-24 15:29:41 +00:00
|
|
|
subject: `Codeflash: Optimization Completed${showRepo ? ` For ${owner}/${repo}` : ""}`,
|
|
|
|
|
html,
|
|
|
|
|
})
|
2025-12-15 16:02:20 +00:00
|
|
|
|
|
|
|
|
logger.info("Optimization completed email sent successfully", req, {
|
|
|
|
|
userEmail: user.email,
|
|
|
|
|
repo: showRepo ? `${owner}/${repo}` : undefined,
|
|
|
|
|
})
|
|
|
|
|
|
2025-07-24 15:29:41 +00:00
|
|
|
res.status(200).json({ status: "success", message: "Email has been successfully sent." })
|
|
|
|
|
} catch (error) {
|
2025-12-15 16:02:20 +00:00
|
|
|
logger.errorWithSentry(
|
|
|
|
|
"Failed to send optimization completed email",
|
|
|
|
|
req,
|
|
|
|
|
{
|
|
|
|
|
userEmail: user.email,
|
|
|
|
|
repo: showRepo ? `${owner}/${repo}` : undefined,
|
|
|
|
|
},
|
|
|
|
|
error as Error,
|
|
|
|
|
)
|
2025-07-24 15:29:41 +00:00
|
|
|
Sentry.captureException(error)
|
2026-01-19 17:33:57 +00:00
|
|
|
throw internalServerError("Failed to send optimization completed email")
|
2025-07-24 15:29:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|