Skip to content

Commit

Permalink
debug test
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-adams committed Oct 16, 2024
1 parent 752c5b2 commit 11900d8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
49 changes: 38 additions & 11 deletions packages/server/__tests__/lib/grants-collaboration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const emailConstants = require('../../src/lib/email/constants');
use(chaiAsPromised);

beforeEach(async () => {
console.log('SEED......................');
await seed(knex);
});

Expand Down Expand Up @@ -254,7 +255,7 @@ describe('Grants Collaboration', () => {
});
});

context('Grant Activity', () => {
context.only('Grant Activity', () => {
// Helper functions
const getUserIdsForActivities = (grants) => {
const ids = grants.reduce((userIds, grant) => {
Expand Down Expand Up @@ -286,8 +287,8 @@ describe('Grants Collaboration', () => {
let grant1NoteStaff;

beforeEach(async () => {
subscribeUser(adminUser);
subscribeUser(staffUser);
await subscribeUser(adminUser);
await subscribeUser(staffUser);

periodStart = DateTime.now().minus({ days: 1 }).toJSDate();

Expand Down Expand Up @@ -323,7 +324,7 @@ describe('Grants Collaboration', () => {
.insert({ grant_id: grant2.grant_id, user_id: staffUser.id }, 'id');

await knex('grant_notes_revisions')
.insert({ grant_note_id: grant2NoteStaff.id, text: 'Staff note' }, 'id');
.insert({ grant_note_id: grant2NoteStaff.id, text: 'Another Staff note' }, 'id');

periodEnd = new Date();
});
Expand Down Expand Up @@ -361,21 +362,47 @@ describe('Grants Collaboration', () => {
expect(grantActivities[0].follows[0].userId).to.equal(staffUser.id);
});

it('retrieves email recipients only if OTHER users took action', async () => {
it.only('retrieves email recipients only if OTHER users took action', async () => {
const logState = async () => {

Check failure on line 366 in packages/server/__tests__/lib/grants-collaboration.test.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

'logState' is assigned a value but never used

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable logState.
// test
const followers = await knex.select('*').from('grant_followers');
const notes = await knex.select('*').from('grant_notes');
const revisions = await knex.select('*').from('grant_notes_revisions');
const subs = await knex.select('*').from('email_subscriptions');

console.log('---followers-------------------------');
console.log(followers);
console.log('----notes------------------------');
console.log(notes);
console.log('----revisions------------------------');
console.log(revisions);
console.log('----subs------------------------');
console.log(subs);

console.log('NOW', new Date());
};
periodStart = new Date();

// Admin edits note
await knex('grant_notes_revisions')
.insert({ grant_note_id: grant1NoteAdmin.id, text: 'Edit for Admin note' }, 'id');
const [adminRevised] = await knex('grant_notes_revisions')
.insert({ grant_note_id: grant1NoteAdmin.id, text: 'Edit for Admin note' }, 'created_at');

// Account for db/node delay
periodEnd.setTime(adminRevised.created_at.getTime() + 1);

const recipients1 = await getGrantActivityEmailRecipients(knex, periodStart, new Date());
console.log('revised note:', adminRevised.created_at.toISOString(), periodEnd.toISOString());

const recipients1 = await getGrantActivityEmailRecipients(knex, periodStart, periodEnd);
expect(_.map(recipients1, 'userId')).to.have.members([staffUser.id]);

// Staff edits note
await knex('grant_notes_revisions')
.insert({ grant_note_id: grant1NoteStaff.id, text: 'Edit for Staff note' }, 'id');
const [staffRevised] = await knex('grant_notes_revisions')
.insert({ grant_note_id: grant1NoteStaff.id, text: 'Edit for Staff note' }, 'created_at');

// Account for db/node delay
periodEnd.setTime(staffRevised.created_at.getTime() + 1);

const recipients2 = await getGrantActivityEmailRecipients(knex, periodStart, new Date());
const recipients2 = await getGrantActivityEmailRecipients(knex, periodStart, periodEnd);
expect(_.map(recipients2, 'userId')).to.have.members([staffUser.id, adminUser.id]);
});

Expand Down
7 changes: 5 additions & 2 deletions packages/server/src/lib/grantsCollaboration/grantActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const getActivitiesQuery = (knex) => {
};

async function getGrantActivityEmailRecipients(knex, periodStart, periodEnd) {
await new Promise((resolve) => setTimeout(resolve, 100));

console.log('Performing query...', periodStart, ' to ', periodEnd);
const query = knex
.select([
'recipient_users.id AS recipient_user_id',
Expand All @@ -54,8 +57,8 @@ async function getGrantActivityEmailRecipients(knex, periodStart, periodEnd) {
.on(`recipient_followers.user_id`, '=', `recipient_subscriptions.user_id`)
.on(`recipient_subscriptions.notification_type`, '=', knex.raw('?', [emailConstants.notificationType.grantActivity]));
})
.where('activity.activity_at', '>', periodStart)
.andWhere('activity.activity_at', '<', periodEnd)
.where('activity.activity_at', '>=', periodStart)
.andWhere('activity.activity_at', '<=', periodEnd)
.andWhere('recipient_subscriptions.status', emailConstants.emailSubscriptionStatus.subscribed)
// only consider actions taken by users in the same organization as the recipient:
.andWhereRaw('recipient_users.tenant_id = activity_users.tenant_id')
Expand Down

0 comments on commit 11900d8

Please sign in to comment.