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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | 29x 29x 29x 11x 19x 11x 7x 7x 23x 23x 23x 23x 23x 23x 23x 23x 4x 4x 19x 1x 18x 18x 18x 18x 18x 8x 23x 2x 2x 2x 1x 1x 8x 10x 10x 10x 10x 1x 9x 8x 3x 5x 4x 3x 1x 2x | import type { Dal } from "../../../dal";
import { ConversationService } from "../../../services/whatsapp/conversation.service";
import { resolveEnvironment } from "../../domain-utils";
import {
getWhatsAppIds,
getWhatsAppSafetyConfig,
type WhatsAppSafetyConfig,
} from "../../env-config";
import { createWhatsAppClient } from "../../whatsapp/client";
import type {
AdapterErrorCode,
AdapterResult,
CommunicationQueueMessage,
WhatsAppContent,
} from "../types";
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;
// DRY RUN.
//
// `dev` dry-runs only when the recipient was REDIRECTED: it has real
// secrets, and letting an allowlisted number receive a genuine message
// is the point of having a deployed dev environment to test delivery in.
//
// `local` dry-runs ALWAYS, unless a developer explicitly opts in. The
// old `&& wasRedirected` rule had a hole exactly the shape of a
// developer: the safety allowlist means "safe to really send to", so an
// allowlisted number is NOT redirected — and on a laptop that meant a
// live Meta call putting a real WhatsApp message on a real handset on
// every local login attempt. This is the number that also carries every
// user's login OTP, and its quality rating is shared, so casual local
// traffic is not free.
//
// The capability is kept rather than removed — set WA_LOCAL_REAL_SEND
// ("true") in .dev.vars to test genuine delivery from local. Only the
// DEFAULT changed, and it changed towards the failure that is cheap:
// a developer flipping a flag, rather than real people receiving test
// messages from someone's laptop.
const isLocal = env.ENVIRONMENT === "local";
const localRealSend =
(env as { WA_LOCAL_REAL_SEND?: string }).WA_LOCAL_REAL_SEND === "true";
const isDryRunEnv = isLocal || env.ENVIRONMENT === "dev";
if ((isLocal && !localRealSend) || (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(
resolveEnvironment(env.ENVIRONMENT),
);
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();
if (m.includes("rate limit") || m.includes("too many requests")) {
return "rate_limit";
}
if (m.includes("quota")) return "quota";
if (
m.includes("template") &&
(m.includes("reject") || m.includes("paused") || m.includes("disabled"))
) {
return "template_rejected";
}
if (m.includes("policy") || m.includes("blocked")) return "policy";
if (m.includes("content")) return "content_filtered";
if (m.includes("network") || m.includes("timeout") || m.includes("connect")) {
return "transport";
}
return "unknown";
}
|