mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
use new mail service in dashboard
This commit is contained in:
@@ -1,193 +1,35 @@
|
||||
import { TransactionalEmailsApi, SendSmtpEmail } from '@getbrevo/brevo';
|
||||
import { WELCOME_EMAIL } from './email_templates/WelcomeEmail';
|
||||
import { LIMIT_50_EMAIL } from './email_templates/Limit50Email';
|
||||
import { LIMIT_90_EMAIL } from './email_templates/Limit90Email';
|
||||
import { LIMIT_MAX_EMAIL } from './email_templates/LimitMaxEmail';
|
||||
import { PURCHASE_EMAIL } from './email_templates/PurchaseEmail';
|
||||
import { ANOMALY_VISITS_EVENTS_EMAIL } from './email_templates/AnomalyUsageEmail';
|
||||
import { ANOMALY_DOMAIN_EMAIL } from './email_templates/AnomalyDomainEmail';
|
||||
import { CONFIRM_EMAIL } from './email_templates/ConfirmEmail';
|
||||
import { RESET_PASSWORD_EMAIL } from './email_templates/ResetPasswordEmail';
|
||||
const templateMap = {
|
||||
confirm: '/confirm',
|
||||
welcome: '/welcome',
|
||||
purchase: '/purchase',
|
||||
reset_password: '/reset_password',
|
||||
anomaly_domain: '/anomaly/domain',
|
||||
anomaly_visits_events: '/anomaly_visits_events',
|
||||
limit_50: '/limit/50',
|
||||
limit_90: '/limit/90',
|
||||
limit_max: '/limit/max',
|
||||
} as const;
|
||||
|
||||
class EmailService {
|
||||
export type EmailTemplate = keyof typeof templateMap;
|
||||
export type EmailServerInfo = { url: string, body: Record<string, any>, headers: Record<string, string> };
|
||||
|
||||
private apiInstance = new TransactionalEmailsApi();
|
||||
type EmailData =
|
||||
| { template: 'confirm', data: { target: string, link: string } }
|
||||
| { template: 'welcome', data: { target: string } }
|
||||
| { template: 'purchase', data: { target: string, projectName: string } }
|
||||
| { template: 'reset_password', data: { target: string, newPassword: string } }
|
||||
| { template: 'anomaly_domain', data: { target: string, projectName: string, domains: string[] } }
|
||||
| { template: 'anomaly_visits_events', data: { target: string, projectName: string, data: any[] } }
|
||||
| { template: 'limit_50', data: { target: string, projectName: string } }
|
||||
| { template: 'limit_90', data: { target: string, projectName: string } }
|
||||
| { template: 'limit_max', data: { target: string, projectName: string } };
|
||||
|
||||
init(apiKey: string) {
|
||||
this.apiInstance.setApiKey(0, apiKey);
|
||||
export class EmailService {
|
||||
static getEmailServerInfo<T extends EmailTemplate>(template: T, data: Extract<EmailData, { template: T }>['data']): EmailServerInfo {
|
||||
return {
|
||||
url: `https://mail-service.litlyx.com/send${templateMap[template]}`,
|
||||
body: data,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
};
|
||||
}
|
||||
|
||||
async sendLimitEmail50(target: string, projectName: string) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "⚡ You've reached 50% limit on Litlyx";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "help@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
|
||||
sendSmtpEmail.htmlContent = LIMIT_50_EMAIL
|
||||
.replace(/\[Project Name\]/, projectName)
|
||||
.toString();
|
||||
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendLimitEmail90(target: string, projectName: string) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "⚡ You've reached 90% limit on Litlyx";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "help@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
sendSmtpEmail.htmlContent = LIMIT_90_EMAIL
|
||||
.replace(/\[Project Name\]/, projectName)
|
||||
.toString();
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendLimitEmailMax(target: string, projectName: string) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "🚨 You've reached your limit on Litlyx!";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "help@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
sendSmtpEmail.htmlContent = LIMIT_MAX_EMAIL
|
||||
.replace(/\[Project Name\]/, projectName)
|
||||
.toString();
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendWelcomeEmail(target: string) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "Welcome to Litlyx!";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "help@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
sendSmtpEmail.htmlContent = WELCOME_EMAIL;
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendPurchaseEmail(target: string, projectName: string) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "Thank You for Upgrading Your Litlyx Plan!";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "help@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
sendSmtpEmail.htmlContent = PURCHASE_EMAIL
|
||||
.replace(/\[Project Name\]/, projectName)
|
||||
.toString();
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendAnomalyVisitsEventsEmail(target: string, projectName: string,
|
||||
data: {
|
||||
visits: { _id: string, count: number }[],
|
||||
events: { _id: string, count: number }[]
|
||||
}) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "🔍 Unexpected Activity Detected by our AI";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "help@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
sendSmtpEmail.htmlContent = ANOMALY_VISITS_EVENTS_EMAIL
|
||||
.replace(/\[Project Name\]/, projectName)
|
||||
.replace(/\[ENTRIES\]/,
|
||||
[
|
||||
...data.visits.map(e => (`<li> Visits in date ${new Date(e._id).toLocaleDateString('en-EN')} [ ${e.count} ] </li>`)),
|
||||
...data.events.map(e => (`<li> Events in date ${new Date(e._id).toLocaleDateString('en-EN')} [ ${e.count} ] </li>`))
|
||||
]
|
||||
.join('')
|
||||
)
|
||||
.toString();
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendAnomalyDomainEmail(target: string, projectName: string, domains: string[]) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "🔍 Suspicious dns detected by our AI";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "help@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
sendSmtpEmail.htmlContent = ANOMALY_DOMAIN_EMAIL
|
||||
.replace(/\[Project Name\]/, projectName)
|
||||
.replace(/\[CURRENT_DATE\]/, new Date().toLocaleDateString('en-EN'))
|
||||
// .replace(/\[DNS_ENTRIES\]/,
|
||||
// domains.map(e => (`<li> ${e} </li>`)).join('<br>')
|
||||
.replace(/\[DNS_ENTRIES\]/, domains[0])
|
||||
.toString();
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async sendConfirmEmail(target: string, link: string) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "Confirm your email";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "no-reply@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
sendSmtpEmail.htmlContent = CONFIRM_EMAIL
|
||||
.replace(/\[CONFIRM_LINK\]/, link)
|
||||
.toString();
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendResetPasswordEmail(target: string, newPassword: string) {
|
||||
try {
|
||||
const sendSmtpEmail = new SendSmtpEmail();
|
||||
sendSmtpEmail.subject = "Password reset";
|
||||
sendSmtpEmail.sender = { "name": "Litlyx", "email": "no-reply@litlyx.com" };
|
||||
sendSmtpEmail.to = [{ "email": target }];
|
||||
sendSmtpEmail.htmlContent = RESET_PASSWORD_EMAIL
|
||||
.replace(/\[NEW_PASSWORD\]/, newPassword)
|
||||
.toString();
|
||||
await this.apiInstance.sendTransacEmail(sendSmtpEmail);
|
||||
return true;
|
||||
} catch (ex) {
|
||||
console.error('ERROR SENDING EMAIL', ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
const instance = new EmailService();
|
||||
export default instance;
|
||||
}
|
||||
Reference in New Issue
Block a user