feat(portal): a WhatsApp Admin can open a chat someone else owns

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-02 11:03:56 +08:00
1 parent f7bfea44c7
commit 2cbd939567
3 files changed
+30 -2

No files matched your search

@@ -49,6 +49,22 @@ describe("WhatsAppButton", () => {
expect(screen.getByRole("status")).toHaveTextContent("Sam Lee owns this chat.");
});
it("lets a WhatsApp Admin open a chat someone else owns", async () => {
m.modules = ["BO_WHATSAPP", "BO_WHATSAPP_ADMIN"];
m.startChat.mockResolvedValue(conv(9, "Sam Lee"));
render(<WhatsAppButton target={{ leadUuid: "L1" }} mobile="+60123" />);
await press();
expect(screen.getByRole("status")).toHaveTextContent("Sam Lee owns this chat.");
expect(screen.getByRole("link", { name: "Open chat" })).toHaveAttribute("href", "/whatsapp?c=c-1");
});
it("offers no link to staff who do not own the chat", async () => {
m.startChat.mockResolvedValue(conv(9, "Sam Lee"));
render(<WhatsAppButton target={{ leadUuid: "L1" }} mobile="+60123" />);
await press();
expect(screen.queryByRole("link", { name: "Open chat" })).toBeNull();
});
it("says WhatsApp is not set up when the company has no number", async () => {
m.startChat.mockRejectedValue(new ApiError("x", 409, { code: "WHATSAPP_NOT_CONFIGURED" }));
render(<WhatsAppButton target={{ leadUuid: "L1" }} mobile="+60123" />);
@@ -1,6 +1,7 @@
"use client";
import { useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { MessageCircle } from "lucide-react";
import { Button } from "@/app/components/ui/button";
@@ -19,16 +20,21 @@ export function WhatsAppButton({ target, mobile }: { target: Target; mobile: str
const { id, hasModule } = useAdminAuth();
const [busy, setBusy] = useState(false);
const [message, setMessage] = useState<string | null>(null);
// Set when someone else owns the chat and the user is a WhatsApp Admin, who may open it anyway.
const [openHref, setOpenHref] = useState<string | null>(null);
if (!hasModule("BO_WHATSAPP") || !mobile) return null;
const start = () => {
setBusy(true);
setMessage(null);
setOpenHref(null);
startChat(target)
.then((c) => {
if (c.ownerUserId != null && c.ownerUserId !== id) setMessage(t("WaOwnedBy", { name: c.ownerName ?? "" }));
else router.push(`/whatsapp?c=${encodeURIComponent(c.uuid)}`);
const href = `/whatsapp?c=${encodeURIComponent(c.uuid)}`;
if (c.ownerUserId == null || c.ownerUserId === id) return router.push(href);
setMessage(t("WaOwnedBy", { name: c.ownerName ?? "" }));
if (hasModule("BO_WHATSAPP_ADMIN")) setOpenHref(href);
})
.catch((err: unknown) => {
const code = err instanceof ApiError ? (err.data as { code?: string } | undefined)?.code : undefined;
@@ -54,6 +60,11 @@ export function WhatsAppButton({ target, mobile }: { target: Target; mobile: str
</PopoverAnchor>
<PopoverContent className="w-64 text-sm">
<p role="status">{message}</p>
{openHref && (
<Link href={openHref} className="mt-2 inline-block font-medium text-primary underline-offset-4 hover:underline">
{t("WaOpenChat")}
</Link>
)}
</PopoverContent>
</Popover>
);
@@ -396,6 +396,7 @@ export const DEFAULT_MESSAGES: Record<string, string> = {
BO_WHATSAPP_ADMIN: "WhatsApp admin",
WaUnread: "{n} unread",
WaOwnedBy: "{name} owns this chat.",
WaOpenChat: "Open chat",
WaNotConfigured: "WhatsApp is not set up for this company.",
WaStartFailed: "The chat could not be started.",
BO_EMAILS: "Emails",