-
Notifications
You must be signed in to change notification settings - Fork 527
chore: Add support CRUD endpoints #6836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
"use server"; | ||
import "server-only"; | ||
|
||
import { getTeamById } from "@/api/team"; | ||
import { getUnthreadConversation } from "lib/unthread/get-conversation"; | ||
import { getRawAccount } from "../../../../account/settings/getAccount"; | ||
import { loginRedirect } from "../../../../login/loginRedirect"; | ||
|
||
type AddMessageToTicketResponse = | ||
| { | ||
success: false; | ||
message: string; | ||
} | ||
| { | ||
success: true; | ||
}; | ||
|
||
export async function addMessageToTicketAction(args: { | ||
teamId: string; | ||
conversationId: string; | ||
messageMarkdown: string; | ||
}): Promise<AddMessageToTicketResponse> { | ||
const { teamId, conversationId, messageMarkdown } = args; | ||
if (!teamId || !conversationId || !messageMarkdown) { | ||
return { | ||
success: false, | ||
message: "Missing required arguments.", | ||
}; | ||
} | ||
|
||
const account = await getRawAccount(); | ||
if (!account) { | ||
// User is not logged in. | ||
loginRedirect("/support"); | ||
} | ||
|
||
const team = await getTeamById(teamId); | ||
const customerId = team?.unthreadCustomerId; | ||
if (!customerId) { | ||
return { | ||
success: false, | ||
message: `Support customer for team ${teamId} not found.`, | ||
}; | ||
} | ||
|
||
if ( | ||
[ | ||
process.env.UNTHREAD_FREE_TIER_ID, | ||
process.env.UNTHREAD_GROWTH_TIER_ID, | ||
process.env.UNTHREAD_PRO_TIER_ID, | ||
].includes(customerId) | ||
) { | ||
// Disallow "shared" legacy customer IDs because this endpoint returns customer-specific data. | ||
return { | ||
success: false, | ||
message: `This endpoint is not supported for legacy customer ID ${customerId}`, | ||
}; | ||
} | ||
|
||
// Get the conversation first to confirm the user has permissions to update this ticket. | ||
const conversation = await getUnthreadConversation(conversationId); | ||
if (!conversation || conversation.customerId !== customerId) { | ||
return { | ||
success: false, | ||
message: "Ticket not found.", | ||
}; | ||
} | ||
|
||
// Get the conversation and messages. | ||
const res = await fetch( | ||
`https://api.unthread.io/api/conversations/${conversationId}/messages`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Api-Key": process.env.UNTHREAD_API_KEY ?? "", | ||
}, | ||
body: JSON.stringify({ | ||
body: { | ||
type: "markdown", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supports HTML and markdown. I'm assuming we'll have a markdown editor. Uploads are not supported yet. |
||
value: messageMarkdown, | ||
}, | ||
onBehalfOf: { | ||
email: account.email, | ||
name: account.name, | ||
id: customerId, | ||
}, | ||
}), | ||
}, | ||
); | ||
if (!res.ok) { | ||
console.error( | ||
"Error adding message to the ticket:", | ||
res.status, | ||
res.statusText, | ||
await res.text(), | ||
); | ||
return { | ||
success: false, | ||
message: "Error adding message to the ticket. Please try again later.", | ||
}; | ||
} | ||
|
||
return { success: true }; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,7 @@ export async function createTicketAction( | |
loginRedirect("/support"); | ||
} | ||
|
||
// @TODO: This needs to be updated to use team.unthreadCustomerId after all users are migrated. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to update to use |
||
const customerId = isValidPlan(team.supportPlan) | ||
? planToCustomerId[team.supportPlan] | ||
: // fallback to "free" tier | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
"use server"; | ||
import "server-only"; | ||
|
||
import { getTeamById } from "@/api/team"; | ||
import { getUnthreadConversation } from "lib/unthread/get-conversation"; | ||
import { getRawAccount } from "../../../../account/settings/getAccount"; | ||
import { loginRedirect } from "../../../../login/loginRedirect"; | ||
|
||
type GetTicketResponse = | ||
| { | ||
success: false; | ||
message: string; | ||
} | ||
| { | ||
success: true; | ||
result: UnthreadMessagesResponse; | ||
}; | ||
Comment on lines
+9
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this response format make sense? |
||
|
||
interface UnthreadMessage { | ||
ts: string; | ||
botId: string; | ||
botName: string; | ||
timestamp: string; | ||
threadTs: string; | ||
id: string; | ||
user: { | ||
id: string; | ||
name: string; | ||
email: string; | ||
photo?: string; | ||
} | null; | ||
conversation: { | ||
customerId: string; | ||
}; | ||
resolvedContent: ( | ||
| string | ||
| { | ||
type: "link"; | ||
id: string; | ||
name: string; | ||
} | ||
)[]; | ||
text: string; | ||
htmlContent: string; | ||
} | ||
|
||
interface UnthreadMessagesResponse { | ||
data: UnthreadMessage[]; | ||
totalCount: number; | ||
cursors: { | ||
hasNext: boolean; | ||
hasPrevious: boolean; | ||
next?: string; | ||
previous?: string; | ||
}; | ||
} | ||
|
||
export async function getTicketAction(args: { | ||
teamId: string; | ||
conversationId: string; | ||
cursor?: string; | ||
}): Promise<GetTicketResponse> { | ||
const { teamId, conversationId, cursor = "" } = args; | ||
if (!teamId || !conversationId) { | ||
return { | ||
success: false, | ||
message: "Missing required arguments.", | ||
}; | ||
} | ||
|
||
const account = await getRawAccount(); | ||
if (!account) { | ||
// User is not logged in. | ||
loginRedirect("/support"); | ||
} | ||
|
||
const team = await getTeamById(teamId); | ||
const customerId = team?.unthreadCustomerId; | ||
if (!customerId) { | ||
return { | ||
success: false, | ||
message: `Support customer for team ${teamId} not found.`, | ||
}; | ||
} | ||
|
||
if ( | ||
[ | ||
process.env.UNTHREAD_FREE_TIER_ID, | ||
process.env.UNTHREAD_GROWTH_TIER_ID, | ||
process.env.UNTHREAD_PRO_TIER_ID, | ||
].includes(customerId) | ||
) { | ||
// Disallow "shared" legacy customer IDs because this endpoint returns customer-specific data. | ||
return { | ||
success: false, | ||
message: `This endpoint is not supported for legacy customer ID ${customerId}`, | ||
}; | ||
} | ||
|
||
// Get the conversation first to confirm the user has permissions to update this ticket. | ||
const conversation = await getUnthreadConversation(conversationId); | ||
if (!conversation || conversation.customerId !== customerId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure we only return conversations for this customer. |
||
return { | ||
success: false, | ||
message: "Ticket not found.", | ||
}; | ||
} | ||
|
||
// Get the conversation and messages. | ||
const res = await fetch( | ||
`https://api.unthread.io/api/conversations/${conversationId}/messages/list`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Api-Key": process.env.UNTHREAD_API_KEY ?? "", | ||
}, | ||
body: JSON.stringify({ | ||
select: [ | ||
"ts", | ||
"botId", | ||
"botName", | ||
"text", | ||
"timestamp", | ||
"threadTs", | ||
"metadata", | ||
"user.id", | ||
"user.name", | ||
"user.email", | ||
"user.slackId", | ||
"user.photo", | ||
], | ||
order: ["ts"], | ||
descending: true, | ||
limit: 100, | ||
cursor, | ||
}), | ||
}, | ||
); | ||
if (!res.ok) { | ||
console.error( | ||
"Error retrieving ticket:", | ||
res.status, | ||
res.statusText, | ||
await res.text(), | ||
); | ||
return { | ||
success: false, | ||
message: "Error retrieving ticket. Please try again later.", | ||
}; | ||
} | ||
|
||
const json = (await res.json()) as UnthreadMessagesResponse; | ||
|
||
return { | ||
success: true, | ||
result: json, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure we only update conversations for this customer.