import { createLogger } from "@link-stack/logger"; import { Zammad, getOrCreateUser } from "../../lib/zammad.js"; const logger = createLogger('create-ticket-from-form'); export interface CreateTicketFromFormOptions { formData: any; receivedAt: string; } const createTicketFromFormTask = async ( options: CreateTicketFromFormOptions, ): Promise => { const { formData, receivedAt } = options; logger.info({ formData, receivedAt, formDataKeys: Object.keys(formData), }, 'Processing Formstack form submission'); // Extract data from Formstack payload const { FormID, UniqueID, name, signal_number, type_of_help_requested, type_of_organization, urgency_level, email_address, phone_number, address, description_of_issue, preferred_contact_method, available_times_for_contact, how_did_you_hear_about_us, preferred_language, } = formData; // Build full name const fullName = name ? `${name.first || ''} ${name.last || ''}`.trim() : 'Unknown'; // Build address string let addressString = ''; if (address) { const parts = [ address.address, address.city, address.state, address.zip, ].filter(Boolean); addressString = parts.join(', '); } // Build ticket title const title = `Help Request: ${type_of_help_requested || 'General'} - ${fullName}`; // Build ticket body with all form information const body = `

Contact Information

Request Details

${description_of_issue ? `

Description of Issue

${description_of_issue}

` : ''} ${how_did_you_hear_about_us ? `

How they heard about us: ${how_did_you_hear_about_us}

` : ''}

Form ID: ${FormID} | Submission ID: ${UniqueID} | Received: ${receivedAt}

`; // Get Zammad configuration from environment const zammadUrl = process.env.ZAMMAD_URL || 'http://zammad-nginx:8080'; const zammadToken = process.env.ZAMMAD_API_TOKEN; if (!zammadToken) { logger.error('ZAMMAD_API_TOKEN environment variable is not configured'); throw new Error('ZAMMAD_API_TOKEN is required'); } const zammad = Zammad({ token: zammadToken }, zammadUrl); try { // Get or create user based on phone number, Signal number, or email let customer; const contactInfo = signal_number || phone_number || email_address; if (contactInfo) { customer = await getOrCreateUser(zammad, contactInfo); } else { // Create user with just the name if no contact info customer = await zammad.user.create({ firstname: name?.first, lastname: name?.last, note: `User created from Formstack submission ${UniqueID}`, }); } logger.info({ customerId: customer.id, customerEmail: customer.email }, 'Customer identified/created'); // Create the ticket const ticket = await zammad.ticket.create({ title, group: "Users", // Default group - you may want to make this configurable note: `This ticket was created automatically from Formstack form ${FormID}.`, customer_id: customer.id, article: { body, subject: title, content_type: "text/html", type: "note", }, }); logger.info({ ticketId: ticket.id, customerId: customer.id, formId: FormID, submissionId: UniqueID, }, 'Zammad ticket created successfully'); } catch (error: any) { logger.error({ error: error.message, stack: error.stack, output: error.output, formId: FormID, submissionId: UniqueID, }, 'Failed to create Zammad ticket'); throw error; } }; export default createTicketFromFormTask;