import { appName, smtpenable } from '../../Utils.js'; export const errHtml = err => { return `Reset Password

${err}

`; }; const sendDeleteUserMail = async req => { const app = req.params.app || appName; if (!req.user) { throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'User is not authenticated.'); } try { const { userId } = req.params; if (!userId) { throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Missing userId parameter.'); } const userPointer = { __type: 'Pointer', className: '_User', objectId: userId }; const createdByPointer = { __type: 'Pointer', className: '_User', objectId: req.user.id }; const userCondition = new Parse.Query('contracts_Users'); userCondition.equalTo('UserId', userPointer); const userAndCreatorCondition = new Parse.Query('contracts_Users'); userAndCreatorCondition.equalTo('UserId', userPointer); userAndCreatorCondition.equalTo('CreatedBy', createdByPointer); const mainQuery = Parse.Query.or(userCondition, userAndCreatorCondition); const result = await mainQuery.first({ useMasterKey: true }); const username = result.get('Email')?.toLowerCase()?.replace(/\s/g, ''); const name = result?.get('Name') ? `${result?.get('Name')}` : ''; const isAdmin = result?.get('UserRole') === 'contracts_Admin'; if (!isAdmin) { throw new Parse.Error( Parse.Error.SCRIPT_FAILED, 'This action is not permitted. Kindly contact your administrator to request account deletion.' ); } const serverUrl = process.env?.SERVER_URL?.replace(/\/app\/?$/, '/'); const deleteUrl = `${serverUrl}delete-account/${userId}`; const mailsender = smtpenable ? process.env.SMTP_USER_EMAIL : process.env.MAILGUN_SENDER; // Render a simple HTML form. In production, consider using a templating engine. await Parse.Cloud.sendEmail({ sender: app + ' <' + mailsender + '>', recipient: username, subject: `Account Deletion Request for ${username} – ${app}`, text: `Account Deletion Request for ${username} – ${app}`, html: ` Account Deletion Request - ${app}

Request to Delete Your Account

Hello ${name},

We have received a request to permanently delete your ${app} account associated with ${username}.

If you did not make this request, please ignore this email. Otherwise, click the button below to proceed with the deletion.

Confirm Account Deletion

If the button above doesn't work, please copy and open the following link with your browser.

${deleteUrl}

Note: This action is irreversible and all your data will be permanently removed from our systems.


If you have any questions or need assistance, please contact our support team.

© ${new Date().getFullYear()} ${app}. All rights reserved.

`, }); return 'mail sent.'; } catch (err) { console.log('Err in sending delete user email ', err); throw new Parse.Error(Parse.Error.SCRIPT_FAILED, err.message); } }; export default sendDeleteUserMail;