generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: BCGOV fax implementation (#1437)
- Loading branch information
Showing
26 changed files
with
433 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,49 +62,49 @@ export class AppService { | |
await addToCache( | ||
this.cacheManager, | ||
CacheKey.PAYMENT_AND_REFUND_DETAILED_REPORT, | ||
this.convertFiletoString( | ||
this.convertFileToString( | ||
assetsPath + 'templates/payment-refund-detailed.report.hbs', | ||
), | ||
); | ||
|
||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.PAYMENT_AND_REFUND_SUMMARY_REPORT, | ||
this.convertFiletoString( | ||
this.convertFileToString( | ||
assetsPath + 'templates/payment-refund-summary.report.hbs', | ||
), | ||
); | ||
|
||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.EMAIL_TEMPLATE_PROFILE_REGISTRATION, | ||
this.convertFiletoString( | ||
this.convertFileToString( | ||
assetsPath + 'templates/profile-registration.email.hbs', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.EMAIL_TEMPLATE_ISSUE_PERMIT, | ||
this.convertFiletoString(assetsPath + 'templates/issue-permit.email.hbs'), | ||
this.convertFileToString(assetsPath + 'templates/issue-permit.email.hbs'), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.EMAIL_TEMPLATE_PAYMENT_RECEIPT, | ||
this.convertFiletoString( | ||
this.convertFileToString( | ||
assetsPath + 'templates/payment-receipt.email.hbs', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.EMAIL_TEMPLATE_COMPANY_SUSPEND, | ||
this.convertFiletoString( | ||
this.convertFileToString( | ||
assetsPath + 'templates/suspend-company.email.hbs', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.EMAIL_TEMPLATE_COMPANY_UNSUSPEND, | ||
this.convertFiletoString( | ||
this.convertFileToString( | ||
assetsPath + 'templates/unsuspend-company.email.hbs', | ||
), | ||
); | ||
|
@@ -116,6 +116,97 @@ export class AppService { | |
createCacheMap(featureFlags, 'featureKey', 'featureValue'), | ||
); | ||
|
||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_HEADER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/BC_Logo_MOTI.png', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_FOOTER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/onRouteBC_Logo.png', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_DARK_MODE_HEADER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/BC_Logo_Rev_MOTI.png', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_DARK_MODE_MED_HEADER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/[email protected]', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_DARK_MODE_FOOTER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/onRouteBC_Rev_Logo.png', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_DARK_MODE_MED_FOOTER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/[email protected]', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_WHITE_HEADER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/BC_Logo_MOTI_White.jpg', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_WHITE_MED_HEADER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/[email protected]', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_WHITE_FOOTER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/onRouteBC_Logo_White.jpg', | ||
true, | ||
'base64', | ||
), | ||
); | ||
await addToCache( | ||
this.cacheManager, | ||
CacheKey.IMG_WHITE_MED_FOOTER_LOGO, | ||
this.convertFileToString( | ||
assetsPath + 'images/[email protected]', | ||
true, | ||
'base64', | ||
), | ||
); | ||
|
||
const endDateTime = new Date(); | ||
const processingTime = endDateTime.getTime() - startDateTime.getTime(); | ||
this.logger.log( | ||
|
@@ -125,12 +216,20 @@ export class AppService { | |
); | ||
} | ||
|
||
private convertFiletoString(filePath: string, encode?: string) { | ||
const file = fs.readFileSync(filePath, 'utf-8'); | ||
if (encode) { | ||
return Buffer.from(file).toString('base64'); | ||
} else { | ||
return Buffer.from(file).toString(); | ||
} | ||
/** | ||
* Converts a file to a string representation. | ||
* | ||
* @param {string} filePath - The path to the file to convert. | ||
* @param {boolean} [treatAsBinary=false] - Flag to indicate if the file should be read as binary. | ||
* @param {BufferEncoding} [encode] - The optional encoding to use for the buffer conversion. | ||
* @returns {string} - The string representation of the file. | ||
*/ | ||
private convertFileToString( | ||
filePath: string, | ||
treatAsBinary = false, | ||
encode?: BufferEncoding, | ||
) { | ||
const file = fs.readFileSync(filePath, treatAsBinary ? undefined : 'utf-8'); | ||
return Buffer.from(file).toString(encode); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,4 @@ | ||
export enum ImageType { | ||
JPG = 'image/jpg', | ||
PNG = 'image/png', | ||
} |
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,148 @@ | ||
import { InternalServerErrorException } from '@nestjs/common'; | ||
import { CacheKey } from '../enum/cache-key.enum'; | ||
import { ImageType } from '../enum/image-type.enum'; | ||
import { NotificationTemplate } from '../enum/notification-template.enum'; | ||
import { getFromCache } from './cache.helper'; | ||
import { Cache } from 'cache-manager'; | ||
import * as Handlebars from 'handlebars'; | ||
|
||
/** | ||
* Retrieves a base64 encoded image string from the cache. | ||
* | ||
* @param imageType - The type of the image to retrieve. | ||
* @param cacheManager - The cache manager instance to use. | ||
* @param cacheKey - The cache key under which the image is stored. | ||
* @returns A promise that resolves to a base64 encoded image string. | ||
*/ | ||
export const getBase64Img = async ( | ||
imageType: ImageType, | ||
cacheManager: Cache, | ||
cacheKey: CacheKey, | ||
): Promise<string> => { | ||
return `data:${imageType};base64,${await getFromCache( | ||
cacheManager, | ||
cacheKey, | ||
)}`; | ||
}; | ||
|
||
export const getCacheKeyforEmailTemplate = ( | ||
templateName: NotificationTemplate, | ||
): CacheKey => { | ||
switch (templateName) { | ||
case NotificationTemplate.ISSUE_PERMIT: | ||
return CacheKey.EMAIL_TEMPLATE_ISSUE_PERMIT; | ||
case NotificationTemplate.PAYMENT_RECEIPT: | ||
return CacheKey.EMAIL_TEMPLATE_PAYMENT_RECEIPT; | ||
case NotificationTemplate.PROFILE_REGISTRATION: | ||
return CacheKey.EMAIL_TEMPLATE_PROFILE_REGISTRATION; | ||
case NotificationTemplate.COMPANY_SUSPEND: | ||
return CacheKey.EMAIL_TEMPLATE_COMPANY_SUSPEND; | ||
case NotificationTemplate.COMPANY_UNSUSPEND: | ||
return CacheKey.EMAIL_TEMPLATE_COMPANY_UNSUSPEND; | ||
default: | ||
throw new Error('Invalid template name'); | ||
} | ||
}; | ||
|
||
/** | ||
* Compiles an HTML email body from a specified template and data. | ||
* | ||
* This method retrieves an email template by name from the cache, then uses Handlebars to compile the template | ||
* with the provided data object. It automatically adds URLs for various logos based on environment variables | ||
* and returns the compiled HTML as a string. | ||
* | ||
* @param templateName The name of the email template to render. | ||
* @param data The data object to pass to the Handlebars template. | ||
* @param isEmbedBase64Image Whether to embed images as Base64 images. | ||
* @returns A promise that resolves with the compiled HTML string of the email body. | ||
* @throws InternalServerErrorException If the template is not found in the cache. | ||
*/ | ||
export const renderTemplate = async ( | ||
templateName: NotificationTemplate, | ||
data: object, | ||
cacheManager: Cache, | ||
isEmbedBase64Image = false, | ||
): Promise<string> => { | ||
const template = await getFromCache( | ||
cacheManager, | ||
getCacheKeyforEmailTemplate(templateName), | ||
); | ||
if (!template?.length) { | ||
throw new InternalServerErrorException('Template not found'); | ||
} | ||
const compiledTemplate = Handlebars.compile(template); | ||
const htmlBody = compiledTemplate({ | ||
...data, | ||
headerLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.PNG, | ||
cacheManager, | ||
CacheKey.IMG_HEADER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/BC_Logo_MOTI.png', | ||
footerLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.PNG, | ||
cacheManager, | ||
CacheKey.IMG_FOOTER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/onRouteBC_Logo.png', | ||
darkModeHeaderLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.PNG, | ||
cacheManager, | ||
CacheKey.IMG_DARK_MODE_HEADER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/BC_Logo_Rev_MOTI.png', | ||
darkModeMedHeaderLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.PNG, | ||
cacheManager, | ||
CacheKey.IMG_DARK_MODE_MED_HEADER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/[email protected]', | ||
darkModeFooterLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.PNG, | ||
cacheManager, | ||
CacheKey.IMG_DARK_MODE_FOOTER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/onRouteBC_Rev_Logo.png', | ||
darkModeMedFooterLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.PNG, | ||
cacheManager, | ||
CacheKey.IMG_DARK_MODE_MED_FOOTER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/[email protected]', | ||
whiteHeaderLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.JPG, | ||
cacheManager, | ||
CacheKey.IMG_WHITE_HEADER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/BC_Logo_MOTI_White.jpg', | ||
whiteMedHeaderLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.JPG, | ||
cacheManager, | ||
CacheKey.IMG_WHITE_MED_HEADER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/[email protected]', | ||
whiteFooterLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.JPG, | ||
cacheManager, | ||
CacheKey.IMG_WHITE_FOOTER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/onRouteBC_Logo_White.jpg', | ||
whiteMedFooterLogo: isEmbedBase64Image | ||
? await getBase64Img( | ||
ImageType.JPG, | ||
cacheManager, | ||
CacheKey.IMG_WHITE_MED_FOOTER_LOGO, | ||
) | ||
: process.env.FRONTEND_URL + '/[email protected]', | ||
}); | ||
return htmlBody; | ||
}; |
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
Oops, something went wrong.