/** * Migration: Add Invoice Email subtype and template * This adds a new notification subtype and email template for sending invoices to clients */ /** * @param { import("knex").Knex } knex * @returns { Promise } */ exports.up = async function(knex) { // Get the Invoices category const invoicesCategory = await knex('notification_categories') .where({ name: 'Invoices' }) .first(); if (!invoicesCategory) { console.warn('Invoices category not found, skipping invoice-email template creation'); return; } // Check if the subtype already exists const existingSubtype = await knex('notification_subtypes') .where({ category_id: invoicesCategory.id, name: 'Invoice Email' }) .first(); let subtypeId; if (existingSubtype) { subtypeId = existingSubtype.id; } else { // Insert the new subtype const [newSubtype] = await knex('notification_subtypes') .insert({ category_id: invoicesCategory.id, name: 'Invoice Email', description: 'Email sent to client with invoice attached', is_enabled: true, is_default_enabled: true }) .returning('*'); subtypeId = newSubtype.id; } // Define the HTML template (matching the existing ticket-created style) const htmlTemplate = `
Invoice
{{invoice.number}}
From {{company.name}}

Dear {{recipient.name}},

Please find attached your invoice from {{company.name}}.

Invoice Number {{invoice.number}}
Amount Due {{invoice.amount}}
Invoice Date {{invoice.invoiceDate}}
Due Date {{invoice.dueDate}}
{{#if customMessage}}
Note from {{company.name}}
{{customMessage}}
{{/if}}

The invoice is attached to this email as a PDF. If you have any questions, please don't hesitate to contact us.

Thank you for your business!

Best regards,
{{company.name}}

Powered by Alga PSA
`.trim(); const textTemplate = ` Invoice {{invoice.number}} from {{company.name}} Dear {{recipient.name}}, Please find attached your invoice from {{company.name}}. Invoice Details: - Invoice Number: {{invoice.number}} - Amount Due: {{invoice.amount}} - Invoice Date: {{invoice.invoiceDate}} - Due Date: {{invoice.dueDate}} {{#if customMessage}} Note: {{customMessage}} {{/if}} The invoice is attached to this email as a PDF. If you have any questions, please don't hesitate to contact us. Thank you for your business! Best regards, {{company.name}} `.trim(); // Check if template already exists const existingTemplate = await knex('system_email_templates') .where({ name: 'invoice-email', language_code: 'en' }) .first(); if (!existingTemplate) { // Insert the English template await knex('system_email_templates').insert({ name: 'invoice-email', language_code: 'en', subject: 'Invoice {{invoice.number}} from {{company.name}}', html_content: htmlTemplate, text_content: textTemplate, notification_subtype_id: subtypeId }); } console.log('✓ Invoice Email subtype and template added'); }; /** * @param { import("knex").Knex } knex * @returns { Promise } */ exports.down = async function(knex) { // Delete the template first (due to foreign key) await knex('system_email_templates') .where({ name: 'invoice-email' }) .del(); // Delete the subtype await knex('notification_subtypes') .where({ name: 'Invoice Email' }) .del(); console.log('✓ Invoice Email subtype and template removed'); };