feat: move all email to defer queue #185

fix: use relative part for defer func #185

chore: move sendEmail into defer

fix: remove anon function to defer #185

feat: implement batch email sending with defer.run #185

chore: debug sendEmails #185

chore: add robots.txt

chore: use promise.all in sendEmails

chore: debug defer sendEmails #185

chore: try defer sendEmail without loop #105

chore: comment code

chore: change loop style #185

chore: use promise.all for vercel edge #185

feat: add await to sendEmailNotification #185

chore: use sendEmails #185

fix: debug sendEmails #185

fix: email not updating in LMS

fix: remove early return in sendEmails #185
This commit is contained in:
Rotimi Best
2024-03-01 14:10:40 +01:00
parent 3a22a73380
commit ff524fca27
24 changed files with 239 additions and 120 deletions
@@ -1,6 +1,10 @@
import type { Course } from '$lib/utils/types';
import { supabase } from '$lib/utils/functions/supabase';
import type { Reaction, FeedApi } from '$lib/utils/types/feed';
import type { Reaction, FeedApi, Feed } from '$lib/utils/types/feed';
export async function fetchNewsFeedReaction(feedId: Feed['id']) {
return supabase.from('course_newsfeed').select(`reaction`).eq('id', feedId).single();
}
export async function fetchNewsFeeds(courseId?: Course['id']) {
const response = await supabase
@@ -1,5 +1,4 @@
import nodemailer from 'nodemailer';
import { withEmailTemplate } from './template';
import {
SMTP_HOST,
SMTP_USER,
@@ -10,8 +9,19 @@ import {
let transporter: nodemailer.Transporter;
const getTransporter = async (isPersonal?: boolean): Promise<boolean> => {
export const getTransporter = async (
isPersonal?: boolean
): Promise<nodemailer.Transporter | null> => {
return await new Promise((resolve, reject) => {
// If email sending isn't configured
if (!SMTP_HOST) {
resolve(null);
}
if (transporter) {
return resolve(transporter);
}
transporter = nodemailer.createTransport({
host: SMTP_HOST,
port: 465,
@@ -22,49 +32,14 @@ const getTransporter = async (isPersonal?: boolean): Promise<boolean> => {
}
});
transporter.verify(function (error, success) {
transporter.verify(function (error) {
if (error) {
console.log('Transporter error', error);
reject(error);
reject(null);
} else {
console.log('Server is ready to take our messages');
resolve(success);
resolve(transporter);
}
});
});
};
export const sendEmail = async ({
from,
to,
subject,
content,
isPersonalEmail,
replyTo
}: {
from?: string;
to: string;
subject: string;
content: string;
isPersonalEmail?: boolean;
replyTo?: string;
}) => {
try {
await getTransporter(isPersonalEmail);
const info = await transporter.sendMail({
from: from || '"Best from ClassroomIO" <[email protected]>', // sender address
to, // list of receivers
subject, // Subject line
replyTo,
html: withEmailTemplate(content) // html body
});
console.log('Message sent: %s', info.messageId);
return info;
} catch (error) {
console.error({ error });
return error;
}
};
+2 -2
View File
@@ -29,7 +29,7 @@ export interface FeedApi {
};
created_at: string;
comment: Comment[];
reaction: Reaction[];
reaction: Reaction;
is_pinned: false;
}
@@ -41,6 +41,6 @@ export interface Feed {
};
created_at: string;
comment: Comment[];
reaction: Reaction[];
reaction: Reaction;
isPinned: boolean;
}