mirror of
https://github.com/documenso/documenso.git
synced 2026-07-24 00:43:40 +10:00
feat: create plain customer (#2442)
Co-authored-by: Catalin Pit <catalinpit@gmail.com>
This commit is contained in:
@@ -20,6 +20,12 @@ export const submitSupportTicket = async ({
|
|||||||
organisationId,
|
organisationId,
|
||||||
teamId,
|
teamId,
|
||||||
}: SubmitSupportTicketOptions) => {
|
}: SubmitSupportTicketOptions) => {
|
||||||
|
if (!plainClient) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_SETUP, {
|
||||||
|
message: 'Support ticket system is not configured',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const user = await prisma.user.findFirst({
|
const user = await prisma.user.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: userId,
|
id: userId,
|
||||||
@@ -52,6 +58,29 @@ export const submitSupportTicket = async ({
|
|||||||
})
|
})
|
||||||
: null;
|
: 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 = `
|
const customMessage = `
|
||||||
Organisation: ${organisation.name} (${organisation.id})
|
Organisation: ${organisation.name} (${organisation.id})
|
||||||
Team: ${team ? `${team.name} (${team.id})` : 'No team provided'}
|
Team: ${team ? `${team.name} (${team.id})` : 'No team provided'}
|
||||||
@@ -60,12 +89,14 @@ ${message}`;
|
|||||||
|
|
||||||
const res = await plainClient.createThread({
|
const res = await plainClient.createThread({
|
||||||
title: subject,
|
title: subject,
|
||||||
customerIdentifier: { emailAddress: user.email },
|
customerIdentifier: { customerId: plainCustomer.data.customer.id },
|
||||||
components: [{ componentText: { text: customMessage } }],
|
components: [{ componentText: { text: customMessage } }],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.error) {
|
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;
|
return res;
|
||||||
|
|||||||
Reference in New Issue
Block a user