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 | 19x 19x 19x 9x 15x 9x 6x 6x 13x 13x 13x 13x 13x 13x 2x 2x 11x 1x 10x 10x 10x 10x 10x 8x 13x 2x 2x 2x 1x 1x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x | import { createWhatsAppClient } from "../../whatsapp/client";
import { ConversationService } from "../../../services/whatsapp/conversation.service";
import type { Dal } from "../../../dal";
import type {
AdapterResult,
AdapterErrorCode,
CommunicationQueueMessage,
WhatsAppContent,
} from "../types";
import { resolveEnvironment } from "../../domain-utils";
import {
getWhatsAppIds,
getWhatsAppSafetyConfig,
type WhatsAppSafetyConfig,
} from "../../env-config";
import {
buildWhatsAppPayload,
buildWhatsAppPreviewText,
} from "../whatsapp/payload-builder";
/**
* Determine the actual recipient for a WhatsApp message, applying non-prod safety guards.
*
* Rules:
* - Production: always use the original recipient
* - Non-prod, no override number configured: use original recipient
* - Non-prod, recipient is in allowed list: use original recipient
* - Non-prod, recipient is NOT in allowlist: redirect to override number
*
* The optional safetyConfig parameter allows tests to inject specific values.
*/
export function applySafetyGuard(
recipient: string,
env: CloudflareBindings,
safetyConfig?: WhatsAppSafetyConfig,
): string {
const config =
safetyConfig ?? getWhatsAppSafetyConfig(resolveEnvironment(env.ENVIRONMENT));
const { overrideNumber, allowedNumbers } = config;
if (!overrideNumber) return recipient;
const normalizedRecipient = recipient.replace(/\D/g, "");
const normalizedAllowed = allowedNumbers.map((n) => n.replace(/\D/g, ""));
if (normalizedAllowed.includes(normalizedRecipient)) return recipient;
console.log(
`[WA_SAFETY] Non-prod: redirecting message from ${recipient} to ${overrideNumber}`,
);
return overrideNumber;
}
export class WhatsAppAdapter {
async send(
message: CommunicationQueueMessage,
env: CloudflareBindings,
dal?: Dal,
): Promise<AdapterResult> {
const { recipient, content } = message;
const waContent = content as WhatsAppContent;
// Apply safety guard to determine actual recipient
const actualRecipient = applySafetyGuard(recipient, env);
const wasRedirected = actualRecipient !== recipient;
// Non-production environment + recipient was overridden → DRY RUN
const isDryRunEnv = env.ENVIRONMENT === "local" || env.ENVIRONMENT === "dev";
if (isDryRunEnv && wasRedirected) {
console.log(
`[WA_DRY_RUN] ${env.ENVIRONMENT} mode — message NOT sent (would go to ${actualRecipient}):`,
JSON.stringify(waContent),
);
return {
status: "sent",
actualRecipient,
provider: "whatsapp_dry_run",
previewText: buildWhatsAppPreviewText(waContent),
};
}
// Check WhatsApp access token is configured (IDs come from env-config)
if (!env.WHATSAPP_ACCESS_TOKEN) {
return {
status: "failed",
actualRecipient,
provider: "whatsapp_cloud_api",
errorMessage: "WhatsApp not configured",
errorCode: "config",
};
}
try {
const { phoneNumberId, wabaId } = getWhatsAppIds();
const client = createWhatsAppClient({
phoneNumberId,
accessToken: env.WHATSAPP_ACCESS_TOKEN,
wabaId,
environment: env.ENVIRONMENT,
});
const payload = buildWhatsAppPayload(actualRecipient, waContent);
const result = await client.sendRaw(
payload as Parameters<typeof client.sendRaw>[0],
);
const wamid = result.messages[0]?.id;
// Persist to waMessages for chat UI (non-critical)
if (dal) {
try {
const conversationService = new ConversationService(dal);
const conversation =
await conversationService.getOrCreateConversation(actualRecipient);
await conversationService.addOutboundMessage(
conversation.id,
"template",
JSON.stringify(payload),
{
templateName: waContent.templateName,
wamid,
status: "sent",
},
);
} catch (persistError) {
console.error(
"[WA_ADAPTER] Failed to persist message to conversation (non-critical):",
persistError,
);
}
}
return {
status: "sent",
actualRecipient,
provider: "whatsapp_cloud_api",
externalId: wamid,
previewText: buildWhatsAppPreviewText(waContent),
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
status: "failed",
actualRecipient,
provider: "whatsapp_cloud_api",
previewText: buildWhatsAppPreviewText(waContent),
errorMessage,
errorCode: classifyWhatsAppError(errorMessage),
};
}
}
}
/**
* Meta Cloud API error code → AdapterErrorCode mapping. The queue consumer
* uses errorCode to decide retry vs permanent-fail (policy/template_rejected
* must NOT be retried — they'd just fail again and erode WABA quality).
*/
function classifyWhatsAppError(message: string): AdapterErrorCode {
const m = message.toLowerCase();
Iif (m.includes("rate limit") || m.includes("too many requests")) {
return "rate_limit";
}
Iif (m.includes("quota")) return "quota";
Iif (
m.includes("template") &&
(m.includes("reject") || m.includes("paused") || m.includes("disabled"))
) {
return "template_rejected";
}
Iif (m.includes("policy") || m.includes("blocked")) return "policy";
Iif (m.includes("content")) return "content_filtered";
if (
m.includes("network") ||
m.includes("timeout") ||
m.includes("connect")
) {
return "transport";
}
return "unknown";
}
|