Skip to content

The Open Source DocuSign Alternative.

feat: create plain customer (#2442)

Ephraim Duncane3b0087be62d
diff --git a/packages/lib/server-only/user/submit-support-ticket.ts b/packages/lib/server-only/user/submit-support-ticket.ts
index 91c55800d..283cfd7b8 100644
--- a/packages/lib/server-only/user/submit-support-ticket.ts
+++ b/packages/lib/server-only/user/submit-support-ticket.ts
@@ -20,6 +20,12 @@ export const submitSupportTicket = async ({
organisationId,
teamId,
}: SubmitSupportTicketOptions) => {
+ if (!plainClient) {
+ throw new AppError(AppErrorCode.NOT_SETUP, {
+ message: 'Support ticket system is not configured',
+ });
+ }
+
const user = await prisma.user.findFirst({
where: {
id: userId,
@@ -52,6 +58,29 @@ export const submitSupportTicket = async ({
})
: null;
+ // Ensure the customer exists in Plain before creating a thread
+ const plainCustomer = await plainClient.upsertCustomer({
+ identifier: {
+ emailAddress: user.email,
+ },
+ onCreate: {
+ // If the user doesn't have a name, default to their email
+ fullName: user.name || user.email,
+ email: {
+ email: user.email,
+ isVerified: !!user.emailVerified,
+ },
+ },
+ // No need to update the customer if it already exists
+ onUpdate: {},
+ });
+
+ if (plainCustomer.error) {
+ throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
+ message: `Failed to create customer in support system: ${plainCustomer.error.message}`,
+ });
+ }
+
const customMessage = `
Organisation: ${organisation.name} (${organisation.id})
Team: ${team ? `${team.name} (${team.id})` : 'No team provided'}
@@ -60,12 +89,14 @@ ${message}`;
const res = await plainClient.createThread({
title: subject,
- customerIdentifier: { emailAddress: user.email },
+ customerIdentifier: { customerId: plainCustomer.data.customer.id },
components: [{ componentText: { text: customMessage } }],
});
if (res.error) {
- throw new Error(res.error.message);
+ throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
+ message: `Failed to create support ticket: ${res.error.message}`,
+ });
}
return res;