/** * Migration to add Credit Expiring notification subtype and email template * * @param { import("knex").Knex } knex * @returns { Promise } */ exports.up = async function(knex) { // Get or create the Invoices category let invoicesCategory = await knex('notification_categories') .where({ name: 'Invoices' }) .first(); if (!invoicesCategory) { // Create the Invoices category if it doesn't exist console.log('Creating Invoices notification category...'); [invoicesCategory] = await knex('notification_categories') .insert({ name: 'Invoices', description: 'Notifications related to billing and invoices', is_enabled: true, is_default_enabled: true }) .returning('*'); } // Add the Credit Expiring notification subtype const [creditExpiringSubtype] = await knex('notification_subtypes') .insert({ category_id: invoicesCategory.id, name: 'Credit Expiring', description: 'When credits are about to expire', is_enabled: true, is_default_enabled: true }) .returning('*'); // Add the email template await knex('system_email_templates').insert({ name: 'credit-expiring', subject: 'Credits Expiring Soon: {{company.name}}', notification_subtype_id: creditExpiringSubtype.id, html_content: `

Credits Expiring Soon

The following credits for {{company.name}} will expire soon:

Company: {{company.name}}

Total Expiring Amount: {{credits.totalAmount}}

Expiration Date: {{credits.expirationDate}}

Days Until Expiration: {{credits.daysRemaining}}

{{#each credits.items}} {{/each}}
Credit ID Amount Expiration Date Original Transaction
{{this.creditId}} {{this.amount}} {{this.expirationDate}} {{this.transactionId}}

Please use these credits before they expire to avoid losing them.

View Credits `, text_content: ` Credits Expiring Soon The following credits for {{company.name}} will expire soon: Company: {{company.name}} Total Expiring Amount: {{credits.totalAmount}} Expiration Date: {{credits.expirationDate}} Days Until Expiration: {{credits.daysRemaining}} Credit Details: {{#each credits.items}} - Credit ID: {{this.creditId}} Amount: {{this.amount}} Expiration Date: {{this.expirationDate}} Original Transaction: {{this.transactionId}} {{/each}} Please use these credits before they expire to avoid losing them. View credits at: {{credits.url}} ` }); }; /** * @param { import("knex").Knex } knex * @returns { Promise } */ exports.down = async function(knex) { // Delete the email template await knex('system_email_templates') .where({ name: 'credit-expiring' }) .del(); // Delete the notification subtype const deletedSubtype = await knex('notification_subtypes') .where({ name: 'Credit Expiring' }) .del(); // Check if we need to clean up the Invoices category // Only delete it if there are no other subtypes using it const invoicesCategory = await knex('notification_categories') .where({ name: 'Invoices' }) .first(); if (invoicesCategory) { const remainingSubtypes = await knex('notification_subtypes') .where({ category_id: invoicesCategory.id }) .count('* as count') .first(); if (remainingSubtypes && remainingSubtypes.count === '0') { console.log('No remaining subtypes for Invoices category, deleting it...'); await knex('notification_categories') .where({ id: invoicesCategory.id }) .del(); } } };