PSA/server/migrations/20250226090000_add_credit_expiration_notification.cjs
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

133 lines
4.7 KiB
JavaScript

/**
* Migration to add Credit Expiring notification subtype and email template
*
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
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: `
<h2>Credits Expiring Soon</h2>
<p>The following credits for {{company.name}} will expire soon:</p>
<div class="details">
<p><strong>Company:</strong> {{company.name}}</p>
<p><strong>Total Expiring Amount:</strong> {{credits.totalAmount}}</p>
<p><strong>Expiration Date:</strong> {{credits.expirationDate}}</p>
<p><strong>Days Until Expiration:</strong> {{credits.daysRemaining}}</p>
</div>
<table style="width: 100%; border-collapse: collapse; margin-top: 20px;">
<thead>
<tr style="background-color: #f2f2f2;">
<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">Credit ID</th>
<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">Amount</th>
<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">Expiration Date</th>
<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">Original Transaction</th>
</tr>
</thead>
<tbody>
{{#each credits.items}}
<tr>
<td style="padding: 8px; text-align: left; border: 1px solid #ddd;">{{this.creditId}}</td>
<td style="padding: 8px; text-align: left; border: 1px solid #ddd;">{{this.amount}}</td>
<td style="padding: 8px; text-align: left; border: 1px solid #ddd;">{{this.expirationDate}}</td>
<td style="padding: 8px; text-align: left; border: 1px solid #ddd;">{{this.transactionId}}</td>
</tr>
{{/each}}
</tbody>
</table>
<p style="margin-top: 20px;">Please use these credits before they expire to avoid losing them.</p>
<a href="{{credits.url}}" class="button">View Credits</a>
`,
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<void> }
*/
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();
}
}
};