refactor(web): reuse send api + access-control test helpers

This commit is contained in:
Peter Steinberger
2026-02-16 17:22:03 +00:00
parent 94a4dd0189
commit 291275982c
2 changed files with 26 additions and 26 deletions

View File

@@ -3,6 +3,20 @@ import type { ActiveWebSendOptions } from "../active-listener.js";
import { recordChannelActivity } from "../../infra/channel-activity.js";
import { toWhatsappJid } from "../../utils.js";
function recordWhatsAppOutbound(accountId: string) {
recordChannelActivity({
channel: "whatsapp",
accountId,
direction: "outbound",
});
}
function resolveOutboundMessageId(result: unknown): string {
return typeof result === "object" && result && "key" in result
? String((result as { key?: { id?: string } }).key?.id ?? "unknown")
: "unknown";
}
export function createWebSendApi(params: {
sock: {
sendMessage: (jid: string, content: AnyMessageContent) => Promise<unknown>;
@@ -51,15 +65,8 @@ export function createWebSendApi(params: {
}
const result = await params.sock.sendMessage(jid, payload);
const accountId = sendOptions?.accountId ?? params.defaultAccountId;
recordChannelActivity({
channel: "whatsapp",
accountId,
direction: "outbound",
});
const messageId =
typeof result === "object" && result && "key" in result
? String((result as { key?: { id?: string } }).key?.id ?? "unknown")
: "unknown";
recordWhatsAppOutbound(accountId);
const messageId = resolveOutboundMessageId(result);
return { messageId };
},
sendPoll: async (
@@ -74,15 +81,8 @@ export function createWebSendApi(params: {
selectableCount: poll.maxSelections ?? 1,
},
} as AnyMessageContent);
recordChannelActivity({
channel: "whatsapp",
accountId: params.defaultAccountId,
direction: "outbound",
});
const messageId =
typeof result === "object" && result && "key" in result
? String((result as { key?: { id?: string } }).key?.id ?? "unknown")
: "unknown";
recordWhatsAppOutbound(params.defaultAccountId);
const messageId = resolveOutboundMessageId(result);
return { messageId };
},
sendReaction: async (