All files / lib/communication/cron blog-reminders.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 1/1
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38            6x 6x 6x 6x     6x   6x   6x 6x           6x   4x 4x 4x 5x                  
import { getDb } from "../../../db";
import { createDal } from "../../../dal";
import { CommunicationGateway } from "../gateway";
import { BlogLifecycleHandler } from "../handlers/blog-lifecycle.handler";
 
export async function checkBlogApprovalReminders(env: CloudflareBindings): Promise<void> {
	const db = getDb(env.DB);
	const dal = createDal(db);
	const gateway = new CommunicationGateway(dal, env);
	const handler = new BlogLifecycleHandler(gateway, dal);
 
	// Find blogs pending approval for > 7 days
	const pendingBlogs = await dal.blogs.findPendingApprovalOlderThan(7);
 
	for (const blog of pendingBlogs) {
		// Check if reminder already sent in last 7 days
		const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
		const recentReminders = await dal.communicationLog.findRecentByEventAndMetadata(
			"blog_approval_reminder",
			sevenDaysAgo,
			`"blogId":"${blog.id}"`, // substring match on JSON metadata
		);
 
		if (recentReminders.length === 0) {
			// Find associated pros for this blog
			const blogProsMap = await dal.blogPros.findByBlogIds([blog.id]);
			const blogPros = blogProsMap.get(blog.id) ?? [];
			for (const bp of blogPros) {
				await handler.handleApprovalReminder({
					proId: bp.proId,
					blogId: blog.id,
					blogTitle: blog.title,
				});
			}
		}
	}
}