All files / lib/communication/handlers website-build-result.handler.ts

100% Statements 15/15
100% Branches 14/14
100% Functions 3/3
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                              13x 13x       11x 11x   10x 11x   30x                                         11x 10x 9x   9x             9x                         10x      
import type { Dal } from "../../../dal";
import type { CommunicationGateway } from "../gateway";
import type { CommunicationEventType, CommunicationRequest } from "../types";
 
export type WebsiteBuildResultPayload = {
	proId: string;
	success: boolean;
	businessName: string;
	websiteUrl?: string;
	errorMessage?: string;
	portalUrl?: string;
};
 
export class WebsiteBuildResultHandler {
	constructor(
		private gateway: CommunicationGateway,
		private dal: Dal,
	) {}
 
	async handle(payload: WebsiteBuildResultPayload): Promise<void> {
		const teamMembers = await this.dal.userTenantRoles.getProUsersWithContact(payload.proId);
		if (teamMembers.length === 0) return;
 
		const eventType: CommunicationEventType = payload.success ? "website_build_success" : "website_build_failed";
		const subject = payload.success ? "Your Website Is Live!" : "Website Build Failed";
 
		const requests: CommunicationRequest[] = teamMembers.map((member) => ({
			channel: "email" as const,
			recipient: member.email,
			eventType,
			proId: payload.proId,
			userId: member.userId,
			content: {
				template: "website-build-result",
				subject,
				props: {
					success: payload.success,
					websiteUrl: payload.websiteUrl,
					errorMessage: payload.errorMessage,
					businessName: payload.businessName,
					portalUrl: payload.portalUrl,
				},
				recipientName: member.name,
			},
		}));
 
		// WhatsApp to pro's business number (if configured)
		const pro = await this.dal.pros.findById(payload.proId);
		if (pro?.whatsapp) {
			const templateName = payload.success ? "int_website_build_success" : "int_website_build_failed";
			const parameters =
				payload.success && payload.websiteUrl
					? [
							{ type: "text" as const, text: payload.businessName },
							{ type: "text" as const, text: payload.websiteUrl },
						]
					: [{ type: "text" as const, text: payload.businessName }];
 
			requests.push({
				channel: "whatsapp",
				recipient: pro.whatsapp,
				eventType,
				proId: payload.proId,
				content: {
					templateName,
					languageCode: "en",
					components: [{ type: "body" as const, parameters }],
				},
			});
		}
 
		await this.gateway.sendMany(requests);
	}
}