125 lines
3.8 KiB
JavaScript
125 lines
3.8 KiB
JavaScript
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
const path = require('path');
|
|
const fs = require('fs-extra');
|
|
|
|
// Load environment before any other imports
|
|
const args = process.argv.slice(2);
|
|
let envFile = './environment.env';
|
|
let outputPath = path.resolve(process.cwd(), 'test-logs/promo-expired-preview.html');
|
|
const cli = {};
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
switch (args[i]) {
|
|
case '--env':
|
|
if (args[i + 1]) envFile = args[++i];
|
|
break;
|
|
case '--email':
|
|
if (args[i + 1]) cli.to = args[++i];
|
|
break;
|
|
case '--lang':
|
|
if (args[i + 1]) cli.lang = args[++i];
|
|
break;
|
|
case '--name':
|
|
if (args[i + 1]) cli.name = args[++i];
|
|
break;
|
|
case '--promo':
|
|
if (args[i + 1]) cli.promoName = args[++i];
|
|
break;
|
|
case '--subType':
|
|
if (args[i + 1]) cli.subType = args[++i];
|
|
break;
|
|
case '--date':
|
|
if (args[i + 1]) cli.newBillingDate = args[++i];
|
|
break;
|
|
case '--amount':
|
|
if (args[i + 1]) cli.chargeAmount = args[++i];
|
|
break;
|
|
case '--discount':
|
|
if (args[i + 1]) cli.promoDiscount = args[++i];
|
|
break;
|
|
case '--startDate':
|
|
if (args[i + 1]) cli.promoStartDate = args[++i];
|
|
break;
|
|
case '--endDate':
|
|
if (args[i + 1]) cli.promoEndDate = args[++i];
|
|
break;
|
|
case '--out':
|
|
if (args[i + 1]) outputPath = path.resolve(process.cwd(), args[++i]);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
const envPath = path.resolve(process.cwd(), envFile);
|
|
require('dotenv').config({ path: envPath });
|
|
|
|
const Email = require('email-templates');
|
|
const hbs = require('handlebars');
|
|
const env = require('../helpers/env');
|
|
const { DEFAULT_LANG } = require('../helpers/constants');
|
|
require('../helpers/mailer'); // Registers partials/helpers on load
|
|
|
|
async function main() {
|
|
const supportedLocales = ['en', 'es', 'pt'];
|
|
const langRaw = (cli.lang || process.env.LANG || DEFAULT_LANG).split(',')[0];
|
|
const lang = supportedLocales.includes(langRaw.split('.')[0].split('_')[0])
|
|
? langRaw.split('.')[0].split('_')[0]
|
|
: DEFAULT_LANG;
|
|
const baseFromEnv = process.env.BASE_URL || process.env.APP_URL;
|
|
const baseUrl = (baseFromEnv || `https://${env.PRODUCTION ? 'agmission.agnav.com' : 'localhost:4200'}`).replace(/\/$/, '');
|
|
|
|
const locals = {
|
|
locale: lang,
|
|
lang,
|
|
name: cli.name || 'Sample Customer',
|
|
promoName: cli.promoName || 'Seasonal Discount',
|
|
subName: cli.subType || 'AgMission Essentials 2',
|
|
subKind: 'package',
|
|
promoDiscount: cli.promoDiscount || '20% off',
|
|
promoStartDate: cli.promoStartDate || 'January 1, 2024 UTC',
|
|
promoEndDate: cli.promoEndDate || 'March 31, 2024 UTC',
|
|
newBillingDate: cli.newBillingDate || 'July 30, 2024 UTC',
|
|
chargeAmount: cli.chargeAmount || '$49.00',
|
|
isTaxable: true, // set to false to test non-taxable variant
|
|
manageSubUrl: `${baseUrl}/${lang}/#/manage-subscription`,
|
|
baseUrl
|
|
};
|
|
|
|
const email = new Email({
|
|
message: {
|
|
from: 'AgMission <noreply@agnav.com>',
|
|
to: cli.to || 'preview@agnav.com'
|
|
},
|
|
send: false,
|
|
preview: false,
|
|
i18n: {
|
|
locales: ['en', 'es', 'pt'],
|
|
defaultLocale: lang,
|
|
directory: path.join(process.cwd(), 'locales')
|
|
},
|
|
views: {
|
|
root: path.join(process.cwd(), 'emails'),
|
|
options: { extension: 'hbs' },
|
|
engineSource: { requires: { handlebars: hbs } }
|
|
}
|
|
});
|
|
|
|
const subject = (await email.render('promo-expired/subject', locals)).trim();
|
|
const html = await email.render('promo-expired/html', locals);
|
|
|
|
await fs.ensureDir(path.dirname(outputPath));
|
|
await fs.writeFile(outputPath, html, 'utf8');
|
|
|
|
console.log('Promo expired preview generated.');
|
|
console.log('Subject:', subject);
|
|
console.log('HTML saved to:', outputPath);
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error('Failed to render promo expired email:', err);
|
|
process.exit(1);
|
|
});
|