add customer before creating ticket

This commit is contained in:
Will Freeman
2026-04-23 09:43:18 -06:00
parent 7cf14e6e92
commit b0d1e5299d
2 changed files with 40 additions and 2 deletions
+39 -1
View File
@@ -44,15 +44,53 @@ export interface CreateTicketPayload {
}
export class ZammadClient {
private async upsertCustomer(name: string, email: string): Promise<number> {
const normalizedEmail = email.toLowerCase();
// Search for existing user by email
const searchResponse = await fetch(
`${ZAMMAD_URL}/api/v1/users/search?query=${encodeURIComponent(normalizedEmail)}&limit=1`,
{
headers: { 'Authorization': `Token token=${ZAMMAD_TOKEN}` },
}
);
if (searchResponse.ok) {
const users = await searchResponse.json() as Array<{ id: number; email: string }>;
const match = users.find(u => u.email?.toLowerCase() === normalizedEmail);
if (match) return match.id;
}
// Create the customer if not found
const createResponse = await fetch(`${ZAMMAD_URL}/api/v1/users`, {
method: 'POST',
headers: {
'Authorization': `Token token=${ZAMMAD_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ firstname: name, email: normalizedEmail, roles: ['Customer'] }),
});
if (!createResponse.ok) {
const text = await createResponse.text();
throw new Error(`Zammad customer creation failed: ${createResponse.status} ${text}`);
}
const user = await createResponse.json() as { id: number };
return user.id;
}
async createTicket(payload: CreateTicketPayload): Promise<void> {
const { name, email, topic, subject, message } = payload;
const group = TOPIC_GROUP_MAP[topic];
const customerId = await this.upsertCustomer(name, email);
const body = JSON.stringify({
title: subject,
group,
priority: topic === 'media' ? '3 high' : '2 normal',
customer: email,
customer_id: customerId,
article: {
subject,
body: message,
+1 -1
View File
@@ -48,7 +48,7 @@ export class BoundingBox implements BoundingBoxLiteral {
}
const apiService = axios.create({
baseURL: window.location.hostname === "localhost" ? "http://localhost:3000" : "https://api.deflock.org",
baseURL: window.location.hostname === "localhost" ? "http://localhost:3420" : "https://api.deflock.org",
headers: {
"Content-Type": "application/json",
},