diff --git a/packages/server/__tests__/email/email.test.js b/packages/server/__tests__/email/email.test.js index 360d4a360..ee31fab54 100644 --- a/packages/server/__tests__/email/email.test.js +++ b/packages/server/__tests__/email/email.test.js @@ -417,6 +417,21 @@ describe('Email sender', () => { expect(body).to.include(name); expect(body).to.include(moment(openDate).format('MMMM Do YYYY')); }); + it('links to Grants.gov when Grant Details page is not live', async () => { + const agencies = await db.getAgency(fixtures.agencies.accountancy.id); + const agency = agencies[0]; + agency.matched_grants = [fixtures.grants.healthAide]; + const body = await email.buildDigestBody({ name: 'Saved search test', openDate: '2021-08-05', matchedGrants: agency.matched_grants }); + expect(body).to.include(`https://www.grants.gov/search-results-detail/${fixtures.grants.healthAide.grant_id}`); + }); + it('links to Grant Finder when Grant Details page is live', async () => { + process.env.NEW_GRANT_DETAILS_PAGE_ENABLED = 'true'; + const agencies = await db.getAgency(fixtures.agencies.accountancy.id); + const agency = agencies[0]; + agency.matched_grants = [fixtures.grants.healthAide]; + const body = await email.buildDigestBody({ name: 'Saved search test', openDate: '2021-08-05', matchedGrants: agency.matched_grants }); + expect(body).to.include(`${process.env.WEBSITE_DOMAIN}/grants/${fixtures.grants.healthAide.grant_id}`); + }); }); context('getAndSendGrantForSavedSearch', () => { it('Sends an email for a saved search', async () => { diff --git a/packages/server/src/lib/email.js b/packages/server/src/lib/email.js index 27d6c5009..9570a8d3a 100644 --- a/packages/server/src/lib/email.js +++ b/packages/server/src/lib/email.js @@ -152,10 +152,17 @@ function sendWelcomeEmail(email, httpOrigin) { }); } -function getGrantDetail(grant, emailNotificationType) { - const grantDetailTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_grant_detail.html')); +function buildGrantDetailUrlSafe(grantId, emailNotificationType) { + const grantDetailUrl = new URL(process.env.WEBSITE_DOMAIN); + grantDetailUrl.pathname = `grants/${mustache.escape(grantId)}`; + grantDetailUrl.searchParams.set('utm_source', 'usdr-grants'); + grantDetailUrl.searchParams.set('utm_medium', 'email'); + grantDetailUrl.searchParams.set('utm_campaign', mustache.escape(emailNotificationType)); + grantDetailUrl.searchParams.set('utm_content', 'grant-details'); + return grantDetailUrl.toString(); +} - const description = grant.description.substring(0, 380).replace(/(<([^>]+)>)/ig, ''); +function buildGrantsUrlSafe(emailNotificationType) { const grantsUrl = new URL(process.env.WEBSITE_DOMAIN); if (emailNotificationType === notificationType.grantDigest) { grantsUrl.pathname = 'grants'; @@ -164,7 +171,13 @@ function getGrantDetail(grant, emailNotificationType) { } grantsUrl.searchParams.set('utm_source', 'subscription'); grantsUrl.searchParams.set('utm_medium', 'email'); - grantsUrl.searchParams.set('utm_campaign', emailNotificationType); + grantsUrl.searchParams.set('utm_campaign', mustache.escape(emailNotificationType)); + return grantsUrl.toString(); +} + +function getGrantDetail(grant, emailNotificationType) { + const grantDetailTemplate = fileSystem.readFileSync(path.join(__dirname, '../static/email_templates/_grant_detail.html')); + const description = grant.description.substring(0, 380).replace(/(<([^>]+)>)/ig, ''); const grantDetail = mustache.render( grantDetailTemplate.toString(), { title: grant.title, @@ -177,8 +190,13 @@ function getGrantDetail(grant, emailNotificationType) { award_ceiling: grant.award_ceiling || 'Not available', // estimated_funding: grant.estimated_funding, TODO: add once field is available in the database. cost_sharing: grant.cost_sharing, - link_url: `https://www.grants.gov/search-results-detail/${grant.grant_id}`, - grants_url: grantsUrl.toString(), + link_url_safe: process.env.NEW_GRANT_DETAILS_PAGE_ENABLED === 'true' + ? buildGrantDetailUrlSafe(grant.grant_id, emailNotificationType) + : `https://www.grants.gov/search-results-detail/${mustache.escape(grant.grant_id)}`, + link_description: process.env.NEW_GRANT_DETAILS_PAGE_ENABLED === 'true' + ? 'Grant Finder' + : 'Grants.gov', + grants_url_safe: buildGrantsUrlSafe(emailNotificationType), view_grant_label: emailNotificationType === notificationType.grantDigest ? undefined : 'View My Grants', }, ); @@ -218,10 +236,10 @@ async function sendGrantAssignedNotficationForAgency(assignee_agency, grantDetai // TODO: add plain text version of the email const emailPlain = emailHTML.replace(/<[^>]+>/g, ''); const emailSubject = `Grant Assigned to ${assignee_agency.name}`; - const assginees = await db.getSubscribersForNotification(assignee_agency.id, notificationType.grantAssignment); + const assignees = await db.getSubscribersForNotification(assignee_agency.id, notificationType.grantAssignment); const inputs = []; - assginees.forEach((assignee) => inputs.push( + assignees.forEach((assignee) => inputs.push( { toAddress: assignee.email, emailHTML, diff --git a/packages/server/src/static/email_templates/_grant_detail.html b/packages/server/src/static/email_templates/_grant_detail.html index 7bc698c1d..82506e575 100644 --- a/packages/server/src/static/email_templates/_grant_detail.html +++ b/packages/server/src/static/email_templates/_grant_detail.html @@ -19,7 +19,7 @@ - +

{{title}}

@@ -31,7 +31,7 @@ style="Margin:0;padding-bottom:16px;">

- {{{description}}}... View on Grants.gov + {{{description}}}... View on {{ link_description }}

@@ -60,8 +60,8 @@ - {{view_grant_label}} diff --git a/terraform/prod.tfvars b/terraform/prod.tfvars index ce5832bbb..31c2f40ab 100644 --- a/terraform/prod.tfvars +++ b/terraform/prod.tfvars @@ -72,6 +72,9 @@ api_log_retention_in_days = 30 api_datadog_environment_variables = { DD_PROFILING_ENABLED = true, } +api_container_environment = { + NEW_GRANT_DETAILS_PAGE_ENABLED = false, +} // Postgres postgres_enabled = true diff --git a/terraform/staging.tfvars b/terraform/staging.tfvars index 79095a43d..5498752f9 100644 --- a/terraform/staging.tfvars +++ b/terraform/staging.tfvars @@ -69,6 +69,9 @@ api_log_retention_in_days = 14 api_datadog_environment_variables = { DD_PROFILING_ENABLED = true, } +api_container_environment = { + NEW_GRANT_DETAILS_PAGE_ENABLED = true, +} // Postgres postgres_enabled = true