-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from StefanPenchev05/controllers
Controllers
- Loading branch information
Showing
11 changed files
with
170 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
module.exports = { | ||
content: ['./src/emailTemplates/**/*.html'], | ||
theme: { | ||
extend: {}, | ||
}, | ||
plugins: [], | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import fs from "fs" | ||
import ejs from "ejs" | ||
import path from "path" | ||
import juice from "juice" | ||
import tailwindcss from "tailwindcss" | ||
|
||
/** | ||
* Load and convert an HTML template. | ||
* @param {string} templateName - The name of the template to load. This should be the name of an HTML file in the emailTemplates directory, without the .html extension. | ||
* @param {Object} data - The data to insert into the template. This should be an object where the keys are the names of placeholders in the template and the values are the values to replace the placeholders with. | ||
* @returns {Promise<string>} The converted HTML template. | ||
*/ | ||
export function convertTemplate(templateName, data){ | ||
return new Promise((resolve, reject) => { | ||
// Define the paths for the template and its settings | ||
const templatePath = path.resolve(__dirname, `../emailTemplates/${templateName}/${templateName}.html`); | ||
const settingsPath = path.resolve(__dirname, `../emailTemplates/${templateName}/settings.json`); | ||
|
||
// Read settings.json | ||
fs.readFile(settingsPath, 'utf-8', (err, settings) => { | ||
if(err){ | ||
// If an error occurred while reading the file, reject the promise | ||
reject(err); | ||
return; | ||
} | ||
|
||
// Parse the settings from JSON to a JavaScript object | ||
const settings = JSON.parse(settings); | ||
// Get the style compilator from the settings | ||
const styleCompilator = settings.styleCompilator; | ||
|
||
// Render the template with the provided data | ||
ejs.renderFile(templatePath, data, {}, (err, str) => { | ||
if(err){ | ||
// If an error occurred while rendering the template, reject the promise | ||
reject(err); | ||
}else{ | ||
// If the style compilator is Tailwind CSS, apply Tailwind CSS styles | ||
if(styleCompilator === 'tailwind'){ | ||
const css = tailwindcss(path.resolve(__dirname, "../emailTemplates/style/tailwind.config.js")); | ||
const inlineHtml = juice.inlineContent(str,css); | ||
resolve(inlineHtml); | ||
}else{ | ||
// Otherwise, apply the styles from the style.css file in the template's directory | ||
const css = path.resolve(__dirname, `../emailTemplates/${templateName}/style.css`); | ||
const inlineHtml = juice.inlineContent(str,css); | ||
resolve(inlineHtml); | ||
} | ||
} | ||
}); | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import CryptoJS from "crypto-js"; | ||
|
||
/** | ||
* Encrypt a string using AES encryption. | ||
* @param {string} text - The text to encrypt. This could be any string that you want to encrypt, such as a user ID. | ||
* @param {string} secretKey - The secret key for encryption. This should be a string that you use as the key for AES encryption. If no key is provided, the function will use the ENCRYPTION_SECRET environment variable as the key. | ||
* @returns {string} The encrypted text. This is the original text after it has been encrypted using AES encryption. | ||
*/ | ||
export function encrypt(text, secretKey = process.env.ENCRYPTION_SECRET) { | ||
// Use the AES encryption method from the CryptoJS library to encrypt the text | ||
return CryptoJS.AES.encrypt(text, secretKey).toString(); | ||
} | ||
|
||
/** | ||
* Decrypt a string using AES decryption. | ||
* @param {string} text - A string to decrypt. This is expected to be a string that was previously encrypted using AES encryption. | ||
* @param {string} secretKey - The secret key for decryption. This is expected to be a string that was used as the key during AES encryption. If no key is provided, the function will use the ENCRYPTION_SECRET environment variable as the key. | ||
* @returns {string} - The decrypted userId. This is the original userId before it was encrypted. | ||
*/ | ||
export function decrypt(text, secretKey = process.env.ENCRYPTION_SECRET){ | ||
// Use the AES decryption method from the CryptoJS library to decrypt the userId | ||
const bytes = CryptoJS.AES.decrypt(text, secretKey); | ||
// Convert the decrypted bytes back into a string and return it | ||
return bytes.toString(CryptoJS.enc.Utf8); | ||
} |