All files / lib/communication/handlers pro-account-status.handler.ts

100% Statements 14/14
100% Branches 10/10
100% Functions 3/3
100% Lines 12/12

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                                   10x      
import type { Dal } from "../../../dal";
import type { CommunicationGateway } from "../gateway";
import type { CommunicationEventType, CommunicationRequest } from "../types";
 
export type ProAccountStatusPayload = {
	proId: string;
	status: "published" | "archived";
	businessName: string;
	marketplaceUrl?: string;
	portalUrl?: string;
};
 
export class ProAccountStatusHandler {
	constructor(
		private gateway: CommunicationGateway,
		private dal: Dal,
	) {}
 
	async handle(payload: ProAccountStatusPayload): Promise<void> {
		const teamMembers = await this.dal.userTenantRoles.getProUsersWithContact(payload.proId);
		if (teamMembers.length === 0) return;
 
		const eventType: CommunicationEventType =
			payload.status === "published" ? "pro_account_published" : "pro_account_archived";
		const subject =
			payload.status === "published"
				? "Your Pro Account is Now Live on Interioring!"
				: "Your Pro Account Has Been Archived";
 
		const requests: CommunicationRequest[] = teamMembers.map((member) => ({
			channel: "email" as const,
			recipient: member.email,
			eventType,
			proId: payload.proId,
			userId: member.userId,
			content: {
				template: "pro-account-status",
				subject,
				props: {
					businessName: payload.businessName,
					status: payload.status,
					marketplaceUrl: payload.marketplaceUrl,
					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.status === "published" ? "int_pro_account_published" : "int_pro_account_archived";
			requests.push({
				channel: "whatsapp",
				recipient: pro.whatsapp,
				eventType,
				proId: payload.proId,
				content: {
					templateName,
					languageCode: "en",
					components: [
						{
							type: "body" as const,
							parameters: [{ type: "text" as const, text: payload.businessName }],
						},
					],
				},
			});
		}
 
		await this.gateway.sendMany(requests);
	}
}