feat: create plain customer (#2442)

Co-authored-by: Catalin Pit <catalinpit@gmail.com>
This commit is contained in:
Ephraim Duncan
2026-02-09 00:24:45 +00:00
committed by GitHub
parent da89ce7c9a
commit e3b0087be6
@@ -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;