fix email env

This commit is contained in:
Emily
2024-06-02 16:01:57 +02:00
parent 49064f709d
commit 6fb3c9c652
6 changed files with 75 additions and 39 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
steps

View File

@@ -2,7 +2,16 @@ import { TProjectCount } from "@schema/ProjectsCounts";
import { ProjectModel } from "@schema/ProjectSchema"; import { ProjectModel } from "@schema/ProjectSchema";
import { UserModel } from "@schema/UserSchema"; import { UserModel } from "@schema/UserSchema";
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema"; import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
import { sendLimitEmail50 } from '@services/EmailService'; import EmailService from '@services/EmailService';
import { requireEnv } from "../../shared/utilts/requireEnv";
EmailService.createTransport(
requireEnv('EMAIL_SERVICE'),
requireEnv('EMAIL_HOST'),
requireEnv('EMAIL_USER'),
requireEnv('EMAIL_PASS'),
);
export async function checkLimitsForEmail(projectCounts: TProjectCount) { export async function checkLimitsForEmail(projectCounts: TProjectCount) {
@@ -13,7 +22,7 @@ export async function checkLimitsForEmail(projectCounts: TProjectCount) {
if (!project) return; if (!project) return;
const owner = await UserModel.findById(project.owner); const owner = await UserModel.findById(project.owner);
if (!owner) return; if (!owner) return;
await sendLimitEmail50(owner.email); await EmailService.sendLimitEmail50(owner.email);
await LimitNotifyModel.updateOne({ project_id: projectCounts._id }, { limit1: true, limit2: false, limit3: false }, { upsert: true }); await LimitNotifyModel.updateOne({ project_id: projectCounts._id }, { limit1: true, limit2: false, limit3: false }, { upsert: true });
} }

View File

@@ -32,6 +32,10 @@ export default defineNuxtConfig({
AI_ORG: process.env.AI_ORG, AI_ORG: process.env.AI_ORG,
AI_PROJECT: process.env.AI_PROJECT, AI_PROJECT: process.env.AI_PROJECT,
AI_KEY: process.env.AI_KEY, AI_KEY: process.env.AI_KEY,
EMAIL_SERVICE: process.env.EMAIL_SERVICE,
EMAIL_HOST: process.env.EMAIL_HOST,
EMAIL_USER: process.env.EMAIL_USER,
EMAIL_PASS: process.env.EMAIL_PASS,
AUTH_JWT_SECRET: process.env.AUTH_JWT_SECRET, AUTH_JWT_SECRET: process.env.AUTH_JWT_SECRET,
GOOGLE_AUTH_CLIENT_ID: process.env.GOOGLE_AUTH_CLIENT_ID, GOOGLE_AUTH_CLIENT_ID: process.env.GOOGLE_AUTH_CLIENT_ID,
GOOGLE_AUTH_CLIENT_SECRET: process.env.GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_SECRET: process.env.GOOGLE_AUTH_CLIENT_SECRET,

View File

@@ -2,7 +2,7 @@
import { OAuth2Client } from 'google-auth-library'; import { OAuth2Client } from 'google-auth-library';
import { createUserJwt } from '~/server/AuthManager'; import { createUserJwt } from '~/server/AuthManager';
import { UserModel } from '@schema/UserSchema'; import { UserModel } from '@schema/UserSchema';
import { sendWelcomeEmail } from '@services/EmailService'; import EmailService from '@services/EmailService';
const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig() const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig()
@@ -47,7 +47,7 @@ export default defineEventHandler(async event => {
const savedUser = await newUser.save(); const savedUser = await newUser.save();
setImmediate(() => { setImmediate(() => {
if (payload.email) sendWelcomeEmail(payload.email); if (payload.email) EmailService.sendWelcomeEmail(payload.email);
}); });
return { error: false, access_token: createUserJwt({ email: savedUser.email, name: savedUser.name }) } return { error: false, access_token: createUserJwt({ email: savedUser.email, name: savedUser.name }) }

View File

@@ -1,5 +1,7 @@
import mongoose from "mongoose"; import mongoose from "mongoose";
import { Redis } from "~/server/services/CacheService"; import { Redis } from "~/server/services/CacheService";
import EmailService from '@services/EmailService';
const config = useRuntimeConfig(); const config = useRuntimeConfig();
let connection: mongoose.Mongoose; let connection: mongoose.Mongoose;
@@ -7,6 +9,14 @@ export default async () => {
console.log('[SERVER] Initializing'); console.log('[SERVER] Initializing');
EmailService.createTransport(
config.EMAIL_SERVICE,
config.EMAIL_HOST,
config.EMAIL_USER,
config.EMAIL_PASS,
);
if (!connection || connection.connection.readyState == mongoose.ConnectionStates.disconnected) { if (!connection || connection.connection.readyState == mongoose.ConnectionStates.disconnected) {
console.log('[DATABASE] Connecting'); console.log('[DATABASE] Connecting');
connection = await mongoose.connect(config.MONGO_CONNECTION_STRING); connection = await mongoose.connect(config.MONGO_CONNECTION_STRING);

View File

@@ -1,14 +1,8 @@
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import { requireEnv } from '../utilts/requireEnv'; import type SMTPTransport from 'nodemailer/lib/smtp-transport';
const transport = nodemailer.createTransport({
service: requireEnv('EMAIL_SERVICE'),
host: requireEnv('EMAIL_HOST'),
auth: {
user: requireEnv('EMAIL_USER'),
pass: requireEnv('EMAIL_PASS')
}
});
const TemplateEmail50 = ` const TemplateEmail50 = `
<!DOCTYPE html> <!DOCTYPE html>
@@ -228,32 +222,50 @@ const TemplateEmailWelcome = `
</html> </html>
` `
export async function sendLimitEmail50(target: string) {
try { class EmailService {
await transport.sendMail({
from: 'helplitlyx@gmail.com', private transport: nodemailer.Transporter<SMTPTransport.SentMessageInfo>;
to: target,
subject: 'Project limit 50%', createTransport(service: string, host: string, user: string, pass: string) {
html: TemplateEmail50 this.transport = nodemailer.createTransport({
service, host, auth: { user, pass }
}); });
return true;
} catch (ex) {
console.error('ERROR SENDING EMAIL', ex);
return false;
} }
async sendLimitEmail50(target: string) {
try {
if (!this.transport) return console.error('Transport not created');
await this.transport.sendMail({
from: 'helplitlyx@gmail.com',
to: target,
subject: 'Project limit 50%',
html: TemplateEmail50
});
return true;
} catch (ex) {
console.error('ERROR SENDING EMAIL', ex);
return false;
}
}
async sendWelcomeEmail(target: string) {
try {
if (!this.transport) return console.error('Transport not created');
await this.transport.sendMail({
from: 'helplitlyx@gmail.com',
to: target,
subject: 'Welcome to Litlyx',
html: TemplateEmailWelcome
});
return true;
} catch (ex) {
console.error('ERROR SENDING EMAIL', ex);
return false;
}
}
} }
export async function sendWelcomeEmail(target: string) { const instance = new EmailService();
try { export default instance;
await transport.sendMail({
from: 'helplitlyx@gmail.com',
to: target,
subject: 'Welcome to Litlyx',
html: TemplateEmailWelcome
});
return true;
} catch (ex) {
console.error('ERROR SENDING EMAIL', ex);
return false;
}
}