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

1
.gitignore vendored
View file

@ -28,3 +28,4 @@ baileys-state
signald-state signald-state
project.org project.org
**/.openapi-generator/ **/.openapi-generator/
apps/bridge-worker/scripts/*

View file

@ -19,11 +19,13 @@ export interface Ticket {
export interface ZammadClient { export interface ZammadClient {
ticket: { ticket: {
create: (data: any) => Promise<Ticket>; create: (data: any) => Promise<Ticket>;
update: (id: number, data: any) => Promise<Ticket>;
}; };
user: { user: {
search: (data: any) => Promise<User[]>; search: (data: any) => Promise<User[]>;
create: (data: any) => Promise<User>; create: (data: any) => Promise<User>;
}; };
get: (path: string) => Promise<any>;
} }
export type ZammadCredentials = export type ZammadCredentials =
@ -73,6 +75,10 @@ export const Zammad = (
const { payload: result } = await wreck.post("tickets", { payload }); const { payload: result } = await wreck.post("tickets", { payload });
return result as Ticket; return result as Ticket;
}, },
update: async (id, payload) => {
const { payload: result } = await wreck.put(`tickets/${id}`, { payload });
return result as Ticket;
},
}, },
user: { user: {
search: async (query) => { search: async (query) => {
@ -85,6 +91,10 @@ export const Zammad = (
return result as User; 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), formDataKeys: Object.keys(formData),
}, 'Processing Formstack form submission'); }, 'Processing Formstack form submission');
// Extract data from Formstack payload // Extract data from Formstack payload - matching Python ngo-isac-uploader field names
const { const {
FormID, FormID,
UniqueID, UniqueID,
name, Name,
signal_number, Email,
type_of_help_requested, Phone,
type_of_organization, 'Signal Account': signalAccount,
urgency_level, City,
email_address, State,
phone_number, 'Zip Code': zipCode,
address, 'What organization are you affiliated with and/or employed by (if applicable)?': organization,
description_of_issue, 'What type of support do you wish to receive (to the extent you know)?': typeOfSupport,
preferred_contact_method, 'Is there a specific deadline associated with this request (e.g., a legal or legislative deadline)?': specificDeadline,
available_times_for_contact, 'Please provide the deadline': deadline,
how_did_you_hear_about_us, '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,
preferred_language, '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; } = formData;
// Build full name // Build full name - matching Python pattern
const fullName = name const firstName = Name?.first || '';
? `${name.first || ''} ${name.last || ''}`.trim() const lastName = Name?.last || '';
: 'Unknown'; const fullName = (firstName && lastName)
? `${firstName} ${lastName}`.trim()
: firstName || lastName || 'Unknown';
// Get organization name from form data // Build ticket title - exactly matching Python ngo-isac-uploader pattern
const organizationName = type_of_organization || ''; // Pattern: [Name] - [Organization] - [Type of support]
// Build ticket title - matching ngo-isac-uploader pattern
let title = fullName; let title = fullName;
if (organizationName) { if (organization) {
title += ` - ${organizationName}`; title += ` - ${organization}`;
} }
if (type_of_help_requested) { if (typeOfSupport) {
title += ` - ${type_of_help_requested}`; // 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 // Build article body - format all fields as HTML like Python does
// All other fields go into custom Zammad ticket fields const formatAllFields = (data: any): string => {
const body = description_of_issue let html = '';
? `<p>${description_of_issue}</p> for (const [key, value] of Object.entries(data)) {
if (key === 'HandshakeKey' || key === 'FormID' || key === 'UniqueID') continue;
if (value === null || value === undefined || value === '') continue;
<hr> const displayValue = Array.isArray(value) ? value.join(', ') :
<p><em>Submitted via Formstack | Form ID: ${FormID} | Submission ID: ${UniqueID} | Received: ${receivedAt}</em></p>` typeof value === 'object' ? JSON.stringify(value) : value;
: `<p><em>No description provided</em></p> html += `<strong>${key}:</strong><br>${displayValue}<br>`;
}
return html;
};
<hr> const body = formatAllFields(formData);
<p><em>Submitted via Formstack | Form ID: ${FormID} | Submission ID: ${UniqueID} | Received: ${receivedAt}</em></p>`;
// Get Zammad configuration from environment // Get Zammad configuration from environment
const zammadUrl = process.env.ZAMMAD_URL || 'http://zammad-nginx:8080'; const zammadUrl = process.env.ZAMMAD_URL || 'http://zammad-nginx:8080';
@ -79,22 +93,41 @@ const createTicketFromFormTask = async (
const zammad = Zammad({ token: zammadToken }, zammadUrl); const zammad = Zammad({ token: zammadToken }, zammadUrl);
try { try {
// Get or create user based on contact info // Look up the article type ID for cdr_signal
// Priority: signal_number > phone_number > email_address 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; let customer;
// Try to find existing user by phone or email if (phoneNumber) {
if (signal_number || phone_number) { // Try to find by phone (Signal or regular)
const phoneToSearch = signal_number || phone_number; customer = await getUser(zammad, phoneNumber);
customer = await getUser(zammad, phoneToSearch);
if (customer) { if (customer) {
logger.info({ customerId: customer.id, method: 'phone' }, 'Found existing user by phone'); 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 // 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) { if (emailResults.length > 0) {
customer = emailResults[0]; customer = emailResults[0];
logger.info({ customerId: customer.id, method: 'email' }, 'Found existing user by email'); logger.info({ customerId: customer.id, method: 'email' }, 'Found existing user by email');
@ -102,14 +135,14 @@ const createTicketFromFormTask = async (
} }
if (!customer) { 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'); logger.info('Creating new user from form submission');
customer = await zammad.user.create({ customer = await zammad.user.create({
firstname: name?.first || '', firstname: firstName,
lastname: name?.last || '', lastname: lastName,
email: email_address || `${UniqueID}@formstack.local`, email: Email || `${UniqueID}@formstack.local`,
phone: signal_number || phone_number || '', phone: phoneNumber || '',
note: `User created from Formstack submission ${UniqueID}`, roles: ['Customer'],
}); });
} }
@ -119,45 +152,55 @@ const createTicketFromFormTask = async (
customerPhone: customer.phone, customerPhone: customer.phone,
}, 'Customer identified/created'); }, 'Customer identified/created');
// Build address parts // Helper function to format field values (handle arrays and null values)
const streetAddress = address?.address || ''; const formatFieldValue = (value: any): string | undefined => {
const cityValue = address?.city || ''; if (value === null || value === undefined || value === '') return undefined;
const stateValue = address?.state || ''; if (Array.isArray(value)) return value.join(', ');
const zipValue = address?.zip || ''; if (typeof value === 'object') return JSON.stringify(value);
return String(value);
};
// Create the ticket with custom fields mapped to Zammad attributes // Create the ticket with custom fields - EXACTLY matching Python ngo-isac-uploader field names
// Following the pattern from ngo-isac-uploader where all form data const ticketData: any = {
// goes into structured fields rather than HTML body
const ticket = await zammad.ticket.create({
title, title,
group: "Users", // Default group - you may want to make this configurable group: "Imports", // Matching Python - uses "Imports" group
customer_id: customer.id, customer_id: customer.id,
// Custom fields - these will be populated in Zammad's ticket attributes // Custom fields - matching Python field names EXACTLY
// NOTE: 'organization', 'formstack_form_id', 'formstack_submission_id' us_state: formatFieldValue(State),
// fields could not be created due to naming conflicts, so metadata zip_code: formatFieldValue(zipCode),
// is included in the ticket body instead city: formatFieldValue(City),
signal_number: signal_number || undefined, type_of_support: formatFieldValue(typeOfSupport),
type_of_help_requested: type_of_help_requested || undefined, specific_deadline: formatFieldValue(specificDeadline),
type_of_organization: type_of_organization || undefined, deadline: formatFieldValue(deadline),
urgency_level: urgency_level || undefined, has_insurance_provider: formatFieldValue(hasInsuranceProvider),
city: cityValue || undefined, approached_provider: formatFieldValue(approachedProvider),
us_state: stateValue || undefined, type_of_user: formatFieldValue(typeOfUser),
zip_code: zipValue || undefined, org_structure: formatFieldValue(orgStructure),
street_address: streetAddress || undefined, government_affiliated: formatFieldValue(governmentAffiliated),
preferred_contact_method: preferred_contact_method || undefined, where_heard: formatFieldValue(whereHeard),
available_times: available_times_for_contact || undefined, related_issues: formatFieldValue(relatedIssues),
where_heard: how_did_you_hear_about_us || undefined, type_of_work: formatFieldValue(typeOfWork),
preferred_language: preferred_language || undefined,
// Article with just the description // Article with all formatted fields
article: { article: {
body, body,
subject: title, subject: title,
content_type: "text/html", 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({ logger.info({
ticketId: ticket.id, ticketId: ticket.id,

View file

@ -22,6 +22,8 @@ x-bridge-vars: &common-bridge-variables
BRIDGE_SIGNAL_URL: ${BRIDGE_SIGNAL_URL} BRIDGE_SIGNAL_URL: ${BRIDGE_SIGNAL_URL}
BRIDGE_SIGNAL_AUTO_GROUPS: ${BRIDGE_SIGNAL_AUTO_GROUPS} BRIDGE_SIGNAL_AUTO_GROUPS: ${BRIDGE_SIGNAL_AUTO_GROUPS}
LOG_LEVEL: "debug" LOG_LEVEL: "debug"
ZAMMAD_API_TOKEN: ${ZAMMAD_API_TOKEN}
ZAMMAD_URL: ${ZAMMAD_URL}
services: services:
bridge-frontend: bridge-frontend:

View file

@ -38,15 +38,14 @@ RUN bundle check || bundle install --jobs 8
# Install Node packages # Install Node packages
RUN pnpm install --frozen-lockfile RUN pnpm install --frozen-lockfile
# CRITICAL: Install addons BEFORE asset compilation # CRITICAL: Install addons
# This extracts addon files including Vue components, TypeScript, and CSS # This extracts addon files including CoffeeScript, Vue components, TypeScript, and CSS
RUN ruby contrib/link/install.rb RUN ruby contrib/link/install.rb
# Recompile assets with our addon components # Precompile assets with addon CoffeeScript files included
# The base image has assets precompiled, but we need to recompile with our additions # Use ZAMMAD_SAFE_MODE=1 and dummy DATABASE_URL to avoid needing real database
# SKIP asset compilation during build - it will happen at runtime via entrypoint RUN touch db/schema.rb && \
# This is because asset compilation requires Redis which isn't available during build ZAMMAD_SAFE_MODE=1 DATABASE_URL=postgresql://zammad:/zammad bundle exec rake assets:precompile
# RUN bundle exec rake assets:precompile RAILS_SKIP_ASSET_COMPILATION=false || echo "Skipped"
# Run additional setup for addons # Run additional setup for addons
RUN bundle exec rails runner /opt/zammad/contrib/link/setup.rb || true RUN bundle exec rails runner /opt/zammad/contrib/link/setup.rb || true
@ -69,14 +68,6 @@ RUN if [ "$EMBEDDED" = "true" ] ; then \
echo " }" >> /opt/zammad/contrib/nginx/zammad.conf && \ echo " }" >> /opt/zammad/contrib/nginx/zammad.conf && \
echo "}" >> /opt/zammad/contrib/nginx/zammad.conf; \ echo "}" >> /opt/zammad/contrib/nginx/zammad.conf; \
fi fi
RUN sed -i '/^[[:space:]]*# es config/a\
echo "about to reinstall..."\n\
bundle exec rails runner /opt/zammad/contrib/link/setup.rb\n\
bundle exec rake zammad:package:migrate\n\
echo "Recompiling assets with addon CoffeeScript files..."\n\
bundle exec rake assets:precompile RAILS_SKIP_ASSET_COMPILATION=false\n\
echo "Asset recompilation complete"\n\
' /docker-entrypoint.sh
FROM zammad/zammad-docker-compose:${ZAMMAD_VERSION} AS runner FROM zammad/zammad-docker-compose:${ZAMMAD_VERSION} AS runner
USER root USER root

View file

@ -47,8 +47,14 @@ class CdrSignalReply
# Check CDR Link allowed channels setting # Check CDR Link allowed channels setting
allowedChannels = ui.Config.get('cdr_link_allowed_channels') allowedChannels = ui.Config.get('cdr_link_allowed_channels')
if allowedChannels && allowedChannels.trim() hasWhitelist = allowedChannels && allowedChannels.trim()
if hasWhitelist
whitelist = (channel.trim() for channel in allowedChannels.split(',')) whitelist = (channel.trim() for channel in allowedChannels.split(','))
# Filter articleTypes to only those in the whitelist (keep 'note' for internal notes)
articleTypes = articleTypes.filter (type) ->
type.name is 'note' or type.name in whitelist
# Return early if 'cdr_signal' or 'signal message' not in whitelist # Return early if 'cdr_signal' or 'signal message' not in whitelist
return articleTypes if 'cdr_signal' not in whitelist && 'signal message' not in whitelist return articleTypes if 'cdr_signal' not in whitelist && 'signal message' not in whitelist

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
class CdrTicketArticleTypesController < ApplicationController
prepend_before_action -> { authentication_check && authorize! }
def index
types = Ticket::Article::Type.all.map do |type|
{
id: type.id,
name: type.name,
communication: type.communication
}
end
render json: types
end
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
module Controllers
class CdrTicketArticleTypesControllerPolicy < Controllers::ApplicationControllerPolicy
def index?
true
end
end
end

View file

@ -0,0 +1,5 @@
Zammad::Application.routes.draw do
api_path = Rails.configuration.api_path
match api_path + '/ticket_article_types', to: 'cdr_ticket_article_types#index', via: :get
end

View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
class AddCdrLinkConfig < ActiveRecord::Migration[5.2]
def self.up
Setting.create_if_not_exists(
title: 'CDR Link',
name: 'cdr_link_config',
area: 'Integration::CDRLink',
description: 'Defines the CDR Link integration config.',
options: {},
state: { items: [] },
frontend: false,
preferences: {
prio: 2,
permission: ['admin.integration'],
}
)
# Update the existing allowed_channels setting to use admin.integration permission
setting = Setting.find_by(name: 'cdr_link_allowed_channels')
if setting
setting.preferences = {
permission: ['admin.integration'],
}
setting.save!
end
end
def self.down
Setting.find_by(name: 'cdr_link_config')&.destroy
# Restore original permission if rolling back
setting = Setting.find_by(name: 'cdr_link_allowed_channels')
if setting
setting.preferences = {
permission: ['admin'],
}
setting.save!
end
end
end