Last active
February 4, 2023 10:27
-
-
Save radiovisual/e4de090ca40617bcfe5e173b69f3c8a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ts-node | |
import parseArguments from 'yargs-parser'; | |
import { getSendGridTemplateIdsForSendGridSubUser, renderMJMLHandlebarsMarkupFromTemplateName } from '../../helpers/templates'; | |
import { fetchTemplateById } from './sendgrid-client'; | |
import { getActiveVersionHTMLContent, getChecksum } from './sendgrid-utils'; | |
import path from 'path'; | |
const argv = parseArguments(process.argv.slice(2)); | |
const { sendgriduser } = argv; | |
if (!sendgriduser) { | |
console.log(`You need to pass in a --sendgriduser option to run the sync script`); | |
process.exit(1); | |
} | |
const templateData: Array<TemplateData> = getSendGridTemplateIdsForSendGridSubUser(sendgriduser); | |
if (!templateData || templateData.length === 0) { | |
console.log(`No template ids can be found for sendgrid subuser ${sendgriduser}. Aborting`); | |
process.exit(0); | |
} | |
type TemplateData = { | |
templateId: string; | |
templateName: string; | |
htmlChecksum?: string; | |
html?: string; | |
newHtml?: string; | |
} | |
class SendGridSync { | |
private readonly apiKey: string; | |
public readonly downloadedTemplates: Array<TemplateData>; | |
public readonly updatesRequired: Array<TemplateData>; | |
constructor(apiKey: string) { | |
this.apiKey = apiKey; | |
this.downloadedTemplates = []; | |
this.updatesRequired = []; | |
} | |
async downloadTemplates(templateData: Array<TemplateData>, index = 0): Promise<TemplateData> { | |
if (!Array.isArray(templateData)) { | |
throw TypeError('You must supply an array of templateData objects'); | |
} | |
console.log('Downloading templates from SendGrid...'); | |
const template: TemplateData = templateData[index] as TemplateData; | |
// @ts-expect-error | |
return fetchTemplateById(template.templateId).then(([response, _]) => { | |
if (response.statusCode === 200) { | |
// @ts-expect-error | |
const htmlContent = getActiveVersionHTMLContent(response.body); | |
const download = { | |
...template, | |
// @ts-expect-error | |
htmlChecksum: getChecksum(htmlContent), | |
// @ts-expect-error | |
html: getActiveVersionHTMLContent(response.body), | |
}; | |
console.log({ download }); | |
// @ts-expect-error | |
this.downloadedTemplates.push(download); | |
if (index + 1 < templateData.length) { | |
// There is more to fetch... | |
return this.downloadTemplates(templateData, index + 1); | |
} | |
return this.downloadedTemplates; | |
} | |
}); | |
} | |
checkForUpdateRequired(downloadedTemplates: Array<Partial<TemplateData>>, index = 0): Array<TemplateData> { | |
if (downloadedTemplates.length > 0) { | |
const template: TemplateData = templateData[index] as TemplateData; | |
const hbsTemplate = renderMJMLHandlebarsMarkupFromTemplateName(template.templateName, path.join(__dirname, "..", "..")); | |
const checksum = getChecksum(hbsTemplate.html); | |
if (checksum !== template.htmlChecksum) { | |
console.log(`Found a template to update (checksums don't match): ${template.templateName}`); | |
this.updatesRequired.push({ | |
...template, | |
newHtml: hbsTemplate.html | |
} as TemplateData); | |
} | |
if (index + 1 < downloadedTemplates.length) { | |
// There is more to check... | |
return this.checkForUpdateRequired(downloadedTemplates, index + 1); | |
} | |
return this.updatesRequired; | |
} | |
return []; | |
} | |
} | |
(async () => { | |
const sendGridSync = new SendGridSync('api-key'); | |
const downloadedTemplates = await sendGridSync.downloadTemplates(templateData); | |
let updateRequired = false; | |
let templatesNeedingUpdates:Array<TemplateData> = []; | |
if (Array.isArray(downloadedTemplates)) { | |
templatesNeedingUpdates = sendGridSync.checkForUpdateRequired(downloadedTemplates); | |
updateRequired = Array.isArray(templatesNeedingUpdates) && templatesNeedingUpdates.length > 0; | |
} | |
console.log({ updateRequired, templatesNeedingUpdates }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment