import fs from 'fs/promises';
import path from 'path';
import { CONSTANTS } from '../../../lib/helpers.js';
import { translate } from '../../../lib/locale/translate/translate.js';
import { debug } from '../../../lib/log/logger.js';
import {
buildEmailBodyFromTemplate,
sendEmail
} from '../../../lib/mail/emailHelper.js';
import { buildAbsoluteUrl } from '../../../lib/router/buildAbsoluteUrl.js';
import { getConfig } from '../../../lib/util/getConfig.js';
import { getValue } from '../../../lib/util/registry.js';
const TEMPLATE = `
{{#if storeInfo.logo}}
{{/if}}
{{storeName}} reset your password
{{#if storeInfo.logo}}
{{/if}}
|
Hi,
Someone recently requested a password change for your
{{storeInfo.storeName}} account. If this was you, you can set a new
password here:
Reset password
If you don't want to change your password or
didn't request this, just ignore and delete this
message.
To keep your account secure, please don't forward
this email to anyone.
Happy Shopping!
|
|
`;
export async function sendResetPasswordEmail(email, existingCustomer, token) {
const subject = translate('Reset your password');
const url = buildAbsoluteUrl('resetPasswordPage');
const resetPasswordUrl = `${url}?token=${token}`;
let template = '';
const config = getConfig('system.notification_emails.reset_password');
// Check if templatePath is set in config and the file is exists. It should be relative to the project root
if (config?.templatePath) {
const filePath = path.join(CONSTANTS.ROOTPATH, config.templatePath);
try {
await fs.access(filePath);
template = await fs.readFile(filePath, 'utf8');
} catch (error) {
debug(
`Reset password email template file not found at path: ${filePath}. Using default template.`
);
template = TEMPLATE;
}
} else {
template = TEMPLATE;
}
const dynamicData = await getValue('resetPasswordEmailData', {
token,
resetPasswordUrl,
customer: existingCustomer
});
const args = await getValue(
'resetPasswordEmailArguments',
{
to: email,
subject,
template,
data: dynamicData
},
{ customer: existingCustomer, token }
);
await sendEmail('reset_password', args);
}