11"use server" ;
22import "server-only" ;
33
4- import { COOKIE_ACTIVE_ACCOUNT , COOKIE_PREFIX_TOKEN } from "@/constants/cookie " ;
5- import { API_SERVER_URL } from "@/constants/env " ;
6- import { cookies } from "next/headers " ;
7- import { redirect } from "next/navigation " ;
4+ import { getTeamById } from "@/api/team " ;
5+ import { getRawAccount } from "../../../../account/settings/getAccount " ;
6+ import { getAuthTokenWalletAddress } from "../../../../api/lib/getAuthToken " ;
7+ import { loginRedirect } from "../../../../login/loginRedirect " ;
88
99type State = {
1010 success : boolean ;
@@ -71,42 +71,46 @@ export async function createTicketAction(
7171 _previousState : State ,
7272 formData : FormData ,
7373) {
74- const cookieManager = await cookies ( ) ;
75- const activeAccount = cookieManager . get ( COOKIE_ACTIVE_ACCOUNT ) ?. value ;
76- const token = activeAccount
77- ? cookieManager . get ( COOKIE_PREFIX_TOKEN + activeAccount ) ?. value
78- : null ;
79- if ( ! activeAccount || ! token ) {
80- // user is not logged in, make them log in
81- redirect ( `/login?next=${ encodeURIComponent ( "/support" ) } ` ) ;
74+ const teamId = formData . get ( "teamId" ) ?. toString ( ) ;
75+
76+ if ( ! teamId ) {
77+ return {
78+ success : false ,
79+ message : "teamId is required" ,
80+ } ;
8281 }
83- const accountRes = await fetch ( `${ API_SERVER_URL } /v1/account/me` , {
84- method : "GET" ,
85- headers : {
86- Authorization : `Bearer ${ token } ` ,
87- } ,
88- } ) ;
89- if ( accountRes . status !== 200 ) {
90- // user is not logged in, make them log in
91- redirect ( `/login?next=${ encodeURIComponent ( "/support" ) } ` ) ;
82+
83+ const team = await getTeamById ( teamId ) ;
84+
85+ if ( ! team ) {
86+ return {
87+ success : false ,
88+ message : `Team with id "${ teamId } " not found` ,
89+ } ;
9290 }
9391
94- const account = ( await accountRes . json ( ) ) as {
95- data : { name : string ; email : string ; plan : string ; id : string } ;
96- } ;
92+ const [ walletAddress , account ] = await Promise . all ( [
93+ getAuthTokenWalletAddress ( ) ,
94+ getRawAccount ( ) ,
95+ ] ) ;
96+
97+ if ( ! walletAddress || ! account ) {
98+ loginRedirect ( "/support" ) ;
99+ }
97100
98- const customerId = isValidPlan ( account . data . plan )
99- ? planToCustomerId [ account . data . plan ]
100- : undefined ;
101+ const customerId = isValidPlan ( team . supportPlan )
102+ ? planToCustomerId [ team . supportPlan ]
103+ : // fallback to "free" tier
104+ planToCustomerId . free ;
101105
102106 const product = formData . get ( "product" ) ?. toString ( ) || "" ;
103107 const problemArea = formData . get ( "extraInfo_Problem_Area" ) ?. toString ( ) || "" ;
104108
105109 const title = prepareEmailTitle (
106110 product ,
107111 problemArea ,
108- account . data . email ,
109- account . data . name ,
112+ account . email || "" ,
113+ account . name || "" ,
110114 ) ;
111115
112116 const keyVal : Record < string , string > = { } ;
@@ -117,10 +121,10 @@ export async function createTicketAction(
117121 const markdown = prepareEmailBody ( {
118122 product,
119123 markdownInput : keyVal . markdown || "" ,
120- email : account . data . email ,
121- name : account . data . name ,
124+ email : account . email || "" ,
125+ name : account . name || "" ,
122126 extraInfoInput : keyVal ,
123- walletAddress : activeAccount ,
127+ walletAddress : walletAddress ,
124128 } ) ;
125129
126130 const content = {
@@ -129,9 +133,9 @@ export async function createTicketAction(
129133 markdown,
130134 status : "open" ,
131135 onBehalfOf : {
132- email : account . data . email ,
133- name : account . data . name ,
134- id : account . data . id ,
136+ email : account . email ,
137+ name : account . name ,
138+ id : account . id ,
135139 } ,
136140 customerId,
137141 emailInboxId : process . env . UNTHREAD_EMAIL_INBOX_ID ,
0 commit comments