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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 2x 14x 14x 14x 14x 14x 1x 14x 14x 14x 13x 14x 14x 8x 8x 14x 1x 1x 13x 13x 2x 10x 14x 4x 2x 8x 5x 5x 5x 3x 2x 1x 14x 14x 16x | /**
* Notify admin (email + WhatsApp) on a terminal pro-import event.
*
* Best-effort: failure of either channel is logged but never throws.
* Notification must not block, retry, or mark the import as failed —
* the import itself is the source of truth.
*
* Routed via the existing `CommunicationGateway` so messages land in
* `communication_log` and pick up retries/suppression like every other
* outbound message.
*/
import { IMPORT_RESULT_TEMPLATE } from "../../../config/whatsapp-templates";
import type { Dal } from "../../../dal";
import type { CommunicationGateway } from "../gateway";
import type { CommunicationEventType, CommunicationRequest } from "../types";
export type ImportResultPayload = {
importId: string;
proId: string;
proName: string;
sourceUrl: string;
status: "completed" | "failed" | "cancelled";
submittedByUserId?: string | null;
projectsFound?: number;
photosDownloaded?: number;
errorCode?: string;
errorMessage?: string;
};
export type ImportResultHandlerEnv = {
PORTAL_URL?: string;
IMPORT_NOTIFY_EMAIL?: string;
IMPORT_NOTIFY_WHATSAPP?: string;
};
const EVENT_TYPE: Record<
ImportResultPayload["status"],
CommunicationEventType
> = {
completed: "pro_import_completed",
failed: "pro_import_failed",
cancelled: "pro_import_cancelled",
};
export class ImportResultHandler {
constructor(
private gateway: CommunicationGateway,
private dal: Dal,
) {}
async handle(
payload: ImportResultPayload,
env: ImportResultHandlerEnv,
): Promise<void> {
const eventType = EVENT_TYPE[payload.status];
const reviewUrl = buildReviewUrl(env.PORTAL_URL, payload.importId);
const submitter = payload.submittedByUserId
? await this.dal.users
.findById(payload.submittedByUserId)
.catch(() => null)
: null;
const requests: CommunicationRequest[] = [];
const recipientEmail = submitter?.email ?? env.IMPORT_NOTIFY_EMAIL ?? null;
if (recipientEmail) {
requests.push({
channel: "email",
recipient: recipientEmail,
eventType,
proId: payload.proId,
userId: submitter?.id,
content: {
template: "import-result",
subject: `Pro import for ${payload.proName} ${statusLabel(payload.status)}`,
props: {
proName: payload.proName,
sourceUrl: payload.sourceUrl,
status: payload.status,
projectsFound: payload.projectsFound,
photosDownloaded: payload.photosDownloaded,
errorCode: payload.errorCode,
errorMessage: payload.errorMessage,
reviewUrl,
},
recipientName: submitter?.name ?? "admin",
},
});
}
const recipientWhatsApp =
submitter && hasPhone(submitter)
? submitter.phone
: env.IMPORT_NOTIFY_WHATSAPP || null;
if (recipientWhatsApp) {
const summary = buildWhatsAppSummary(payload);
requests.push({
channel: "whatsapp",
recipient: recipientWhatsApp.replace(/\D/g, ""),
eventType,
proId: payload.proId,
userId: submitter?.id,
content: {
templateName: IMPORT_RESULT_TEMPLATE.name,
languageCode: IMPORT_RESULT_TEMPLATE.language,
components: [
{
type: "body",
parameters: [
{
type: "text",
text: clip(payload.proName, 60) || "(unknown)",
},
{ type: "text", text: payload.status },
{ type: "text", text: clip(summary, 200) || "—" },
],
},
],
},
});
}
if (requests.length === 0) {
console.warn(
`[ImportNotify] No recipients for import ${payload.importId}; skipping`,
);
return;
}
try {
await this.gateway.sendMany(requests);
} catch (err) {
console.error(
`[ImportNotify] gateway.sendMany failed for import ${payload.importId}:`,
err instanceof Error ? err.message : String(err),
);
}
}
}
function hasPhone(u: unknown): u is { phone: string } {
return (
typeof u === "object" &&
u !== null &&
"phone" in u &&
typeof (u as { phone: unknown }).phone === "string" &&
(u as { phone: string }).phone.length > 0
);
}
function statusLabel(status: ImportResultPayload["status"]): string {
if (status === "completed") return "ready for review";
if (status === "failed") return "failed";
return "cancelled";
}
function buildWhatsAppSummary(p: ImportResultPayload): string {
if (p.status === "completed") {
const projects = p.projectsFound ?? 0;
const photos = p.photosDownloaded ?? 0;
return `${projects} projects, ${photos} photos`;
}
if (p.errorMessage) return p.errorMessage;
if (p.errorCode) return p.errorCode;
return statusLabel(p.status);
}
function buildReviewUrl(
portalUrl: string | undefined,
importId: string,
): string {
const base =
portalUrl?.replace(/\/+$/, "") ?? "https://portal.interioring.com";
return `${base}/admin/imports/${importId}`;
}
function clip(s: string, n: number): string {
return s.length > n ? s.slice(0, n) : s;
}
|