|
| 1 | +"use server"; |
| 2 | +import { NEXT_PUBLIC_THIRDWEB_API_HOST } from "@/constants/public-envs"; |
| 3 | +import type { SupportTicket } from "../../app/(app)/team/[team_slug]/(team)/~/support/types/tickets"; |
| 4 | +import { getAuthToken, getAuthTokenWalletAddress } from "./auth-token"; |
| 5 | + |
| 6 | +const ESCALATION_FEEDBACK_RATING = 9999; |
| 7 | + |
| 8 | +export async function createSupportTicket(params: { |
| 9 | + message: string; |
| 10 | + teamSlug: string; |
| 11 | + teamId: string; |
| 12 | + title: string; |
| 13 | + conversationId?: string; |
| 14 | +}): Promise<{ data: SupportTicket } | { error: string }> { |
| 15 | + const token = await getAuthToken(); |
| 16 | + if (!token) { |
| 17 | + return { error: "No auth token available" }; |
| 18 | + } |
| 19 | + |
| 20 | + try { |
| 21 | + const walletAddress = await getAuthTokenWalletAddress(); |
| 22 | + |
| 23 | + const encodedTeamSlug = encodeURIComponent(params.teamSlug); |
| 24 | + const apiUrl = `${NEXT_PUBLIC_THIRDWEB_API_HOST}/v1/teams/${encodedTeamSlug}/support-conversations`; |
| 25 | + |
| 26 | + // Build the payload for creating a conversation |
| 27 | + // If the message does not already include wallet address, prepend it |
| 28 | + let message = params.message; |
| 29 | + if (!message.includes("Wallet address:")) { |
| 30 | + message = `Wallet address: ${String(walletAddress || "-")}\n${message}`; |
| 31 | + } |
| 32 | + |
| 33 | + const payload = { |
| 34 | + markdown: message.trim(), |
| 35 | + title: params.title, |
| 36 | + }; |
| 37 | + |
| 38 | + const body = JSON.stringify(payload); |
| 39 | + const headers: Record<string, string> = { |
| 40 | + Accept: "application/json", |
| 41 | + Authorization: `Bearer ${token}`, |
| 42 | + "Content-Type": "application/json", |
| 43 | + "Accept-Encoding": "identity", |
| 44 | + }; |
| 45 | + |
| 46 | + const response = await fetch(apiUrl, { |
| 47 | + body, |
| 48 | + headers, |
| 49 | + method: "POST", |
| 50 | + }); |
| 51 | + |
| 52 | + if (!response.ok) { |
| 53 | + const errorText = await response.text(); |
| 54 | + return { error: `API Server error: ${response.status} - ${errorText}` }; |
| 55 | + } |
| 56 | + |
| 57 | + const createdConversation: SupportTicket = await response.json(); |
| 58 | + |
| 59 | + // Escalate to SIWA feedback endpoint if conversationId is provided |
| 60 | + if (params.conversationId) { |
| 61 | + try { |
| 62 | + const siwaUrl = process.env.NEXT_PUBLIC_SIWA_URL; |
| 63 | + if (siwaUrl) { |
| 64 | + await fetch(`${siwaUrl}/v1/chat/feedback`, { |
| 65 | + method: "POST", |
| 66 | + headers: { |
| 67 | + "Content-Type": "application/json", |
| 68 | + Authorization: `Bearer ${token}`, |
| 69 | + ...(params.teamId ? { "x-team-id": params.teamId } : {}), |
| 70 | + }, |
| 71 | + body: JSON.stringify({ |
| 72 | + conversationId: params.conversationId, |
| 73 | + feedbackRating: ESCALATION_FEEDBACK_RATING, |
| 74 | + }), |
| 75 | + }); |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + // Log error but don't fail the ticket creation |
| 79 | + console.error("Failed to escalate to SIWA feedback:", error); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return { data: createdConversation }; |
| 84 | + } catch (error) { |
| 85 | + return { |
| 86 | + error: `Failed to create support ticket: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 87 | + }; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export async function sendMessageToTicket(request: { |
| 92 | + ticketId: string; |
| 93 | + teamSlug: string; |
| 94 | + teamId: string; |
| 95 | + message: string; |
| 96 | +}): Promise<{ success: true } | { error: string }> { |
| 97 | + if (!request.ticketId || !request.teamSlug) { |
| 98 | + return { error: "Ticket ID and team slug are required" }; |
| 99 | + } |
| 100 | + |
| 101 | + const token = await getAuthToken(); |
| 102 | + if (!token) { |
| 103 | + return { error: "No auth token available" }; |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + const encodedTeamSlug = encodeURIComponent(request.teamSlug); |
| 108 | + const encodedTicketId = encodeURIComponent(request.ticketId); |
| 109 | + const apiUrl = `${NEXT_PUBLIC_THIRDWEB_API_HOST}/v1/teams/${encodedTeamSlug}/support-conversations/${encodedTicketId}/messages`; |
| 110 | + |
| 111 | + // Append /unthread send for customer messages to ensure proper routing |
| 112 | + const messageWithUnthread = `${request.message.trim()}\n/unthread send`; |
| 113 | + const payload = { |
| 114 | + markdown: messageWithUnthread, |
| 115 | + }; |
| 116 | + |
| 117 | + const body = JSON.stringify(payload); |
| 118 | + const headers: Record<string, string> = { |
| 119 | + Accept: "application/json", |
| 120 | + Authorization: `Bearer ${token}`, |
| 121 | + "Content-Type": "application/json", |
| 122 | + "Accept-Encoding": "identity", |
| 123 | + ...(request.teamId ? { "x-team-id": request.teamId } : {}), |
| 124 | + }; |
| 125 | + |
| 126 | + const response = await fetch(apiUrl, { |
| 127 | + body, |
| 128 | + headers, |
| 129 | + method: "POST", |
| 130 | + }); |
| 131 | + |
| 132 | + if (!response.ok) { |
| 133 | + const errorText = await response.text(); |
| 134 | + return { error: `API Server error: ${response.status} - ${errorText}` }; |
| 135 | + } |
| 136 | + |
| 137 | + return { success: true }; |
| 138 | + } catch (error) { |
| 139 | + return { |
| 140 | + error: `Failed to send message: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
0 commit comments