Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Grant activity email trigger #3571 #3647

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/server/__tests__/db/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('db', () => {
'DRY RUN :: Migrating agency criteria for agency 4',
'DRY RUN :: Migrating agency criteria for users 1,2 belonging to agency 0',
'DRY RUN :: No agency criteria to migrate for users 3 belonging to agency 1',
'DRY RUN :: No agency criteria to migrate for users 4 belonging to agency 2',
'DRY RUN :: No agency criteria to migrate for users 4,5 belonging to agency 2',
'DRY RUN :: No users to migrate for agency 4',
'DRY RUN :: Would have inserted approximately 2 saved searches. Note: there may be duplicates.',
'DRY RUN :: Done migrating legacy agency criteria to saved searches',
Expand All @@ -70,7 +70,7 @@ describe('db', () => {
'Migrating agency criteria for agency 4',
'Migrating agency criteria for users 1,2 belonging to agency 0',
'No agency criteria to migrate for users 3 belonging to agency 1',
'No agency criteria to migrate for users 4 belonging to agency 2',
'No agency criteria to migrate for users 4,5 belonging to agency 2',
'No users to migrate for agency 4',
'Inserted 2 saved searches',
'Done migrating legacy agency criteria to saved searches',
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('db', () => {
'Migrating agency criteria for agency 4',
'Migrating agency criteria for users 1,2 belonging to agency 0',
'No agency criteria to migrate for users 3 belonging to agency 1',
'No agency criteria to migrate for users 4 belonging to agency 2',
'No agency criteria to migrate for users 4,5 belonging to agency 2',
'No users to migrate for agency 4',
'Inserted 1 saved searches', // This would have been 2 if not for the duplication mechanism.
'Done migrating legacy agency criteria to saved searches',
Expand Down
8 changes: 8 additions & 0 deletions packages/server/__tests__/db/seeds/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ const users = {
id: 4,
tenant_id: agencies.usdr.tenant_id,
},
usdrAdmin: {
email: '[email protected]',
name: 'USDR admin',
agency_id: agencies.usdr.id,
role_id: roles.adminRole.id,
id: 5,
tenant_id: agencies.usdr.tenant_id,
},
};

const keywords = {
Expand Down
98 changes: 95 additions & 3 deletions packages/server/__tests__/email/email.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const emailService = require('../../src/lib/email/service-email');
const email = require('../../src/lib/email');
const fixtures = require('../db/seeds/fixtures');
const db = require('../../src/db');
const { tags, notificationType, emailSubscriptionStatus } = require('../../src/lib/email/constants');

const { knex } = db;
const awsTransport = require('../../src/lib/gost-aws');

const {
Expand Down Expand Up @@ -216,19 +219,20 @@ describe('Email module', () => {
describe('Email sender', () => {
const sandbox = sinon.createSandbox();
before(async () => {
await fixtures.seed(db.knex);
process.env.DD_SERVICE = 'test-dd-service';
process.env.DD_ENV = 'test-dd-env';
process.env.DD_VERSION = 'test-dd-version';
});

after(async () => {
await db.knex.destroy();
await knex.destroy();
process.env.DD_SERVICE = DD_SERVICE;
process.env.DD_ENV = DD_ENV;
process.env.DD_VERSION = DD_VERSION;
});

beforeEach(() => {
beforeEach(async () => {
await fixtures.seed(knex);
sandbox.spy(emailService);
});

Expand Down Expand Up @@ -571,4 +575,92 @@ describe('Email sender', () => {
expect(sendFake.calledOnce).to.equal(true);
});
});

context('buildAndSendGrantActivityDigestEmails', () => {
const { adminUser, staffUser } = fixtures.users;
const grant1 = fixtures.grants.earFellowship;

let periodStart;
let periodEnd;

let sendFake;

beforeEach(async () => {
sendFake = sinon.fake.returns('foo');
sinon.replace(emailService, 'getTransport', sinon.fake.returns({ sendEmail: sendFake }));

await knex('email_subscriptions').insert([
{
user_id: adminUser.id,
agency_id: adminUser.agency_id,
notification_type: notificationType.grantActivity,
status: emailSubscriptionStatus.subscribed,
},
{
user_id: staffUser.id,
agency_id: staffUser.agency_id,
notification_type: notificationType.grantActivity,
status: emailSubscriptionStatus.subscribed,
},
]);

periodStart = new Date();

// Grant 1 Follows
await knex('grant_followers')
.insert([
{ grant_id: grant1.grant_id, user_id: adminUser.id },
{ grant_id: grant1.grant_id, user_id: staffUser.id },
], 'id');

// Grant 1 Notes
const [adminNote, staffNote] = await knex('grant_notes')
.insert([
{ grant_id: grant1.grant_id, user_id: adminUser.id },
{ grant_id: grant1.grant_id, user_id: staffUser.id },
], 'id');

await knex('grant_notes_revisions')
.insert([
{ grant_note_id: adminNote.id, text: 'Admin note' },
{ grant_note_id: staffNote.id, text: 'Staff note' },
], 'id');

periodEnd = new Date();
});

it('Sends an email for all users with activity', async () => {
// Send digest email for users following grants
await email.buildAndSendGrantActivityDigestEmails(null, periodStart, periodEnd);
expect(sendFake.callCount).to.equal(2);

const [firstEmail, secondEmail] = sendFake.getCalls();

expect([firstEmail.args[0].toAddress, secondEmail.args[0].toAddress]).to.be.members([adminUser.email, staffUser.email]);
});

it('Sends an email for single user\'s digest', async () => {
// Build digest for user following grants
await email.buildAndSendGrantActivityDigestEmails(adminUser.id, periodStart, periodEnd);
expect(sendFake.calledOnce).to.equal(true);

const { body, text, ...rest } = sendFake.firstCall.args[0];

expect(rest).to.deep.equal({
fromName: 'USDR Federal Grant Finder',
ccAddress: undefined,
toAddress: adminUser.email,
subject: 'New activity in your grants',
tags: [
`notification_type=${tags.emailTypes.grantActivityDigest}`,
'user_role=admin',
`organization_id=${adminUser.tenant_id}`,
`team_id=${adminUser.agency_id}`,
'service=test-dd-service',
'env=test-dd-env',
'version=test-dd-version',
],
});
});
});
});
Loading
Loading