Next release WIP #

This commit is contained in:
Darren Clarke 2025-10-27 21:02:19 +01:00
parent 7d7944fa90
commit 20078ccacc
10 changed files with 219 additions and 94 deletions

View file

@ -19,11 +19,13 @@ export interface Ticket {
export interface ZammadClient {
ticket: {
create: (data: any) => Promise<Ticket>;
update: (id: number, data: any) => Promise<Ticket>;
};
user: {
search: (data: any) => Promise<User[]>;
create: (data: any) => Promise<User>;
};
get: (path: string) => Promise<any>;
}
export type ZammadCredentials =
@ -73,6 +75,10 @@ export const Zammad = (
const { payload: result } = await wreck.post("tickets", { payload });
return result as Ticket;
},
update: async (id, payload) => {
const { payload: result } = await wreck.put(`tickets/${id}`, { payload });
return result as Ticket;
},
},
user: {
search: async (query) => {
@ -85,6 +91,10 @@ export const Zammad = (
return result as User;
},
},
get: async (path) => {
const { payload: result } = await wreck.get(path);
return result;
},
};
};

View file

@ -19,53 +19,67 @@ const createTicketFromFormTask = async (
formDataKeys: Object.keys(formData),
}, 'Processing Formstack form submission');
// Extract data from Formstack payload
// Extract data from Formstack payload - matching Python ngo-isac-uploader field names
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,
Name,
Email,
Phone,
'Signal Account': signalAccount,
City,
State,
'Zip Code': zipCode,
'What organization are you affiliated with and/or employed by (if applicable)?': organization,
'What type of support do you wish to receive (to the extent you know)?': typeOfSupport,
'Is there a specific deadline associated with this request (e.g., a legal or legislative deadline)?': specificDeadline,
'Please provide the deadline': deadline,
'Do you have an insurance provider that provides coverage for the types of services you seek (e.g., public official, professional liability insurance, litigation insurance)?': hasInsuranceProvider,
'Have you approached the insurance provider for assistance?': approachedProvider,
'Are you seeking help on behalf of an individual or an organization?': typeOfUser,
'What is the structure of the organization?': orgStructure,
'Are you currently a candidate for elected office, a government officeholder, or a government employee?': governmentAffiliated,
'Where did you hear about the Democracy Protection Network?': whereHeard,
'Do you or the organization work on behalf of any of the following communities or issues? Please select all that apply.': relatedIssues,
'Do you or the organization engage in any of the following types of work? Please select all that apply.': typeOfWork,
'Why are you seeking support? Please briefly describe the circumstances that have brought you to the DPN, including, as applicable, dates, places, and the people or entities involved. We coordinate crisis-response services and some resilience-building services (e.g., assistance establishing good-governance or security practices). If you are seeking resilience-building services, please note that in the text box below.': descriptionOfIssue,
'What is your preferred communication method?': preferredContactMethod,
} = formData;
// Build full name
const fullName = name
? `${name.first || ''} ${name.last || ''}`.trim()
: 'Unknown';
// Build full name - matching Python pattern
const firstName = Name?.first || '';
const lastName = Name?.last || '';
const fullName = (firstName && lastName)
? `${firstName} ${lastName}`.trim()
: firstName || lastName || 'Unknown';
// Get organization name from form data
const organizationName = type_of_organization || '';
// Build ticket title - matching ngo-isac-uploader pattern
// Build ticket title - exactly matching Python ngo-isac-uploader pattern
// Pattern: [Name] - [Organization] - [Type of support]
let title = fullName;
if (organizationName) {
title += ` - ${organizationName}`;
if (organization) {
title += ` - ${organization}`;
}
if (type_of_help_requested) {
title += ` - ${type_of_help_requested}`;
if (typeOfSupport) {
// Handle array format (Formstack sends arrays for multi-select)
const supportText = Array.isArray(typeOfSupport) ? typeOfSupport.join(', ') : typeOfSupport;
title += ` - ${supportText}`;
}
// Build article body - only description and metadata
// All other fields go into custom Zammad ticket fields
const body = description_of_issue
? `<p>${description_of_issue}</p>
// Build article body - format all fields as HTML like Python does
const formatAllFields = (data: any): string => {
let html = '';
for (const [key, value] of Object.entries(data)) {
if (key === 'HandshakeKey' || key === 'FormID' || key === 'UniqueID') continue;
if (value === null || value === undefined || value === '') continue;
<hr>
<p><em>Submitted via Formstack | Form ID: ${FormID} | Submission ID: ${UniqueID} | Received: ${receivedAt}</em></p>`
: `<p><em>No description provided</em></p>
const displayValue = Array.isArray(value) ? value.join(', ') :
typeof value === 'object' ? JSON.stringify(value) : value;
html += `<strong>${key}:</strong><br>${displayValue}<br>`;
}
return html;
};
<hr>
<p><em>Submitted via Formstack | Form ID: ${FormID} | Submission ID: ${UniqueID} | Received: ${receivedAt}</em></p>`;
const body = formatAllFields(formData);
// Get Zammad configuration from environment
const zammadUrl = process.env.ZAMMAD_URL || 'http://zammad-nginx:8080';
@ -79,22 +93,41 @@ const createTicketFromFormTask = async (
const zammad = Zammad({ token: zammadToken }, zammadUrl);
try {
// Get or create user based on contact info
// Priority: signal_number > phone_number > email_address
// Look up the article type ID for cdr_signal
let cdrSignalTypeId: number | undefined;
try {
const articleTypes = await zammad.get('ticket_article_types');
const cdrSignalType = articleTypes.find((t: any) => t.name === 'cdr_signal');
cdrSignalTypeId = cdrSignalType?.id;
if (cdrSignalTypeId) {
logger.info({ cdrSignalTypeId }, 'Found cdr_signal article type');
} else {
logger.warn('cdr_signal article type not found, ticket will use default type');
}
} catch (error: any) {
logger.warn({ error: error.message }, 'Failed to look up cdr_signal article type');
}
// Determine contact method and phone number - matching Python logic
// Priority: Signal > SMS/Phone > Email
const useSignal = preferredContactMethod?.includes('Signal') || preferredContactMethod?.includes('ignal');
const useSMS = preferredContactMethod?.includes('SMS');
const phoneNumber = useSignal ? signalAccount : (useSMS || Phone) ? Phone : '';
// Get or create user - matching Python pattern
let customer;
// Try to find existing user by phone or email
if (signal_number || phone_number) {
const phoneToSearch = signal_number || phone_number;
customer = await getUser(zammad, phoneToSearch);
if (phoneNumber) {
// Try to find by phone (Signal or regular)
customer = await getUser(zammad, phoneNumber);
if (customer) {
logger.info({ customerId: customer.id, method: 'phone' }, 'Found existing user by phone');
}
}
if (!customer && email_address) {
if (!customer && Email) {
// Search by email if phone search didn't work
const emailResults = await zammad.user.search(`email:${email_address}`);
const emailResults = await zammad.user.search(`email:${Email}`);
if (emailResults.length > 0) {
customer = emailResults[0];
logger.info({ customerId: customer.id, method: 'email' }, 'Found existing user by email');
@ -102,14 +135,14 @@ const createTicketFromFormTask = async (
}
if (!customer) {
// Create new user with all available contact information
// Create new user - matching Python user creation pattern
logger.info('Creating new user from form submission');
customer = await zammad.user.create({
firstname: name?.first || '',
lastname: name?.last || '',
email: email_address || `${UniqueID}@formstack.local`,
phone: signal_number || phone_number || '',
note: `User created from Formstack submission ${UniqueID}`,
firstname: firstName,
lastname: lastName,
email: Email || `${UniqueID}@formstack.local`,
phone: phoneNumber || '',
roles: ['Customer'],
});
}
@ -119,45 +152,55 @@ const createTicketFromFormTask = async (
customerPhone: customer.phone,
}, 'Customer identified/created');
// Build address parts
const streetAddress = address?.address || '';
const cityValue = address?.city || '';
const stateValue = address?.state || '';
const zipValue = address?.zip || '';
// Helper function to format field values (handle arrays and null values)
const formatFieldValue = (value: any): string | undefined => {
if (value === null || value === undefined || value === '') return undefined;
if (Array.isArray(value)) return value.join(', ');
if (typeof value === 'object') return JSON.stringify(value);
return String(value);
};
// Create the ticket with custom fields mapped to Zammad attributes
// Following the pattern from ngo-isac-uploader where all form data
// goes into structured fields rather than HTML body
const ticket = await zammad.ticket.create({
// Create the ticket with custom fields - EXACTLY matching Python ngo-isac-uploader field names
const ticketData: any = {
title,
group: "Users", // Default group - you may want to make this configurable
group: "Imports", // Matching Python - uses "Imports" group
customer_id: customer.id,
// Custom fields - these will be populated in Zammad's ticket attributes
// NOTE: 'organization', 'formstack_form_id', 'formstack_submission_id'
// fields could not be created due to naming conflicts, so metadata
// is included in the ticket body instead
signal_number: signal_number || undefined,
type_of_help_requested: type_of_help_requested || undefined,
type_of_organization: type_of_organization || undefined,
urgency_level: urgency_level || undefined,
city: cityValue || undefined,
us_state: stateValue || undefined,
zip_code: zipValue || undefined,
street_address: streetAddress || undefined,
preferred_contact_method: preferred_contact_method || undefined,
available_times: available_times_for_contact || undefined,
where_heard: how_did_you_hear_about_us || undefined,
preferred_language: preferred_language || undefined,
// Custom fields - matching Python field names EXACTLY
us_state: formatFieldValue(State),
zip_code: formatFieldValue(zipCode),
city: formatFieldValue(City),
type_of_support: formatFieldValue(typeOfSupport),
specific_deadline: formatFieldValue(specificDeadline),
deadline: formatFieldValue(deadline),
has_insurance_provider: formatFieldValue(hasInsuranceProvider),
approached_provider: formatFieldValue(approachedProvider),
type_of_user: formatFieldValue(typeOfUser),
org_structure: formatFieldValue(orgStructure),
government_affiliated: formatFieldValue(governmentAffiliated),
where_heard: formatFieldValue(whereHeard),
related_issues: formatFieldValue(relatedIssues),
type_of_work: formatFieldValue(typeOfWork),
// Article with just the description
// Article with all formatted fields
article: {
body,
subject: title,
content_type: "text/html",
type: "note",
type: useSignal ? "cdr_signal" : "note",
from: phoneNumber || Email || 'unknown',
sender: "Customer",
},
});
};
const ticket = await zammad.ticket.create(ticketData);
// Update the ticket with the cdr_signal article type
// This must be done after creation as Zammad doesn't allow setting this field during creation
if (cdrSignalTypeId) {
await zammad.ticket.update(ticket.id, { create_article_type_id: cdrSignalTypeId });
logger.info({ ticketId: ticket.id, cdrSignalTypeId }, 'Updated ticket with cdr_signal article type');
}
logger.info({
ticketId: ticket.id,