feat: agregar soporte para el envío de plantillas de WhatsApp a través de Meta, incluyendo nueva funcionalidad y adaptadores

This commit is contained in:
2026-09-19 10:50:56 -03:00
parent 728d0a711c
commit 40bc65396f
17 changed files with 177 additions and 32 deletions
@@ -190,3 +190,37 @@ export async function dispatchSystem(params: {
return { success: false, error: msg };
}
}
export async function dispatchMetaWap(params: {
phoneNumber: string;
templateName: string;
languageCode: string;
templateVariables?: string[];
}): Promise<DispatchResult> {
try {
if (!params.phoneNumber.trim() || !params.templateName.trim() || !params.languageCode.trim()) {
return { success: false, error: "Meta WAP dispatch requires phoneNumber, templateName, and languageCode" };
}
const axios = (await import("axios")).default;
const response = await axios.post(
buildApiUrl("/notifications/send-whatsapp-template"),
{
phoneNumber: params.phoneNumber,
templateName: params.templateName,
languageCode: params.languageCode,
templateVariables: params.templateVariables,
},
{
headers: { "Content-Type": "application/json" },
}
);
logDone(`Meta WAP dispatched for phone ${params.phoneNumber}`);
return { success: true, providerResponse: JSON.stringify(response.data) };
} catch (error) {
const msg = formatDispatchError(error);
logError(`Meta WAP dispatch failed for ${params.phoneNumber}:`, msg);
return { success: false, error: msg };
}
}
@@ -1,7 +1,7 @@
import { logDone, logError, logIntent } from "../../config/logger.js";
import { NotificationJobModel, INotificationJob } from "./NotificationJob.Adapter.Mongoose.js";
import { WhatsAppThrottleClass } from "./WhatsAppThrottle.js";
import { dispatchWhatsApp, dispatchEmail, dispatchSystem } from "./ChannelDispatchers.js";
import { dispatchWhatsApp, dispatchEmail, dispatchSystem, dispatchMetaWap } from "./ChannelDispatchers.js";
import { resolveNotificationContent } from "./NotificationContentResolver.js";
const POLL_INTERVAL_MS = 5000;
@@ -217,6 +217,14 @@ export class JobProcessor {
code: String(job.appointmentId),
});
break;
case "meta_wap":
result = await dispatchMetaWap({
phoneNumber: content.phoneNumber || "",
templateName: (job.payload as any)?.templateName || "",
languageCode: (job.payload as any)?.languageCode || "en_US",
templateVariables: (job.payload as any)?.templateVariables,
});
break;
default:
result = { success: false, error: `Unknown channel: ${job.channel}` };
}
@@ -1,6 +1,6 @@
import mongoose, { Document, Model, Schema, model } from "mongoose";
export type NotificationChannel = "whatsapp" | "email" | "system";
export type NotificationChannel = "whatsapp" | "meta_wap" | "email" | "system";
export type NotificationJobStatus = "pending" | "processing" | "sent" | "failed" | "cancelled";
export type NotificationJobType = "creation" | "reminder" | "update" | "cancellation";
@@ -69,7 +69,7 @@ export class NotificationJobModel {
channel: {
type: String,
required: true,
enum: ["whatsapp", "email", "system"],
enum: ["whatsapp", "meta_wap", "email", "system"],
},
type: {
type: String,