/** * Add Portal Access notification template to system_email_templates */ exports.up = async function(knex) { // Check if Portal Access category exists let portalAccessCategory = await knex('notification_categories') .where({ name: 'Portal Access' }) .first(); if (!portalAccessCategory) { // Create new category if it doesn't exist [portalAccessCategory] = await knex('notification_categories') .insert({ name: 'Portal Access', description: 'Customer portal access and invitation notifications', is_enabled: true, is_default_enabled: true, created_at: new Date(), updated_at: new Date() }) .returning('*'); } else { // Update existing category [portalAccessCategory] = await knex('notification_categories') .where({ id: portalAccessCategory.id }) .update({ description: 'Customer portal access and invitation notifications', updated_at: new Date() }) .returning('*'); } // Check if portal invitation subtype exists let portalInvitationSubtype = await knex('notification_subtypes') .where({ name: 'portal-invitation' }) .first(); if (!portalInvitationSubtype) { // Create new subtype if it doesn't exist [portalInvitationSubtype] = await knex('notification_subtypes') .insert({ category_id: portalAccessCategory.id, name: 'portal-invitation', description: 'Portal access invitation emails for contacts', is_enabled: true, is_default_enabled: true, created_at: new Date(), updated_at: new Date() }) .returning('*'); } else { // Update existing subtype [portalInvitationSubtype] = await knex('notification_subtypes') .where({ id: portalInvitationSubtype.id }) .update({ category_id: portalAccessCategory.id, description: 'Portal access invitation emails for contacts', updated_at: new Date() }) .returning('*'); } // Check if email template exists let emailTemplate = await knex('system_email_templates') .where({ name: 'portal-invitation' }) .first(); const templateData = { name: 'portal-invitation', notification_subtype_id: portalInvitationSubtype.id, subject: 'Portal Access Invitation - {{companyName}}', created_at: new Date(), updated_at: new Date(), html_content: `

Portal Access Invitation

Hello {{contactName}},

You have been invited to access the customer portal for {{companyName}}. This portal will give you access to view your tickets, invoices, and other important information.

Getting Started

Click the button below to set up your portal account. You'll be able to create a secure password and access your information immediately.

Set Up Portal Access

If the button doesn't work, you can also copy and paste this link into your browser:
{{portalLink}}

Important: This invitation link will expire in {{expirationTime}}. Please complete your account setup before then.

If you didn't expect this invitation or have questions, please contact us.

© {{currentYear}} {{companyName}}. All rights reserved.

`, text_content: ` Portal Access Invitation - {{companyName}} Hello {{contactName}}, You have been invited to access the customer portal for {{companyName}}. This portal will give you access to view your tickets, invoices, and other important information. Getting Started: Click the link below to set up your portal account. You'll be able to create a secure password and access your information immediately. Portal Setup Link: {{portalLink}} IMPORTANT: This invitation link will expire in {{expirationTime}}. Please complete your account setup before then. If you didn't expect this invitation or have questions, please contact us. © {{currentYear}} {{companyName}}. All rights reserved. ` }; if (!emailTemplate) { // Create new template if it doesn't exist await knex('system_email_templates').insert(templateData); } else { // Update existing template await knex('system_email_templates') .where({ id: emailTemplate.id }) .update({ ...templateData, updated_at: new Date() }); } }; exports.down = async function(knex) { // Delete the template await knex('system_email_templates') .where({ name: 'portal-invitation' }) .del(); // Delete the subtype await knex('notification_subtypes') .where({ name: 'portal-invitation' }) .del(); // Check if Portal Access category has other subtypes const portalAccessCategory = await knex('notification_categories') .where({ name: 'Portal Access' }) .first(); if (portalAccessCategory) { const subtypeCount = await knex('notification_subtypes') .where({ category_id: portalAccessCategory.id }) .count('id as count') .first(); // If no other subtypes, delete the category if (subtypeCount && Number(subtypeCount.count) === 0) { await knex('notification_categories') .where({ name: 'Portal Access' }) .del(); } } };