Skip to content

Commit

Permalink
feat(grants-collaboration): create follow/unfollow funcs (#3337)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amy Wang authored Aug 5, 2024
1 parent 2d5ce66 commit 0f61dab
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
24 changes: 23 additions & 1 deletion packages/server/__tests__/lib/grants-collaboration.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const { expect } = require('chai');
const { expect, use } = require('chai');
const chaiAsPromised = require('chai-as-promised');
const knex = require('../../src/db/connection');
const fixtures = require('../db/seeds/fixtures');
const { saveNoteRevision, getOrganizationNotesForGrant } = require('../../src/lib/grantsCollaboration/notes');
const { followGrant, unfollowGrant } = require('../../src/lib/grantsCollaboration/followers');

use(chaiAsPromised);

describe('Grants Collaboration', () => {
context('saveNoteRevision', () => {
Expand Down Expand Up @@ -113,4 +117,22 @@ describe('Grants Collaboration', () => {
expect(result).to.deep.equal(expectedNoteStructure);
});
});
context('followGrant', () => {
it('follows a grant', async () => {
await followGrant(knex, fixtures.grants.earFellowship.grant_id, fixtures.users.adminUser.id);
});
it('suppresses unique constraint violations when trying to follow a grant twice', async () => {
await expect(followGrant(knex, fixtures.grants.earFellowship.grant_id, fixtures.users.adminUser.id)).to.not.be.rejected;
});
it('does not suppress non-unique constraint violation errors', async () => {
// First ensure the test user id is invalid
await expect(knex('users').where({ id: 999999999 })).to.eventually.be.an('array').that.is.empty;
await expect(followGrant(knex, fixtures.grants.earFellowship.grant_id, 999999999)).to.be.rejected;
});
});
context('unfollowGrant', () => {
it('unfollows a grant', async () => {
await unfollowGrant(knex, fixtures.grants.earFellowship.grant_id, fixtures.users.adminUser.id);
});
});
});
21 changes: 21 additions & 0 deletions packages/server/src/lib/grantsCollaboration/followers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
async function followGrant(knex, grantId, userId) {
return knex.transaction(async (trx) => {
try {
await trx('grant_followers').insert({ grant_id: grantId, user_id: userId });
} catch (err) {
if (err.code === '23505') { // unique constraint violation
await trx.rollback();
} else {
throw err;
}
}
});
}

async function unfollowGrant(knex, grantId, userId) {
await knex('grant_followers')
.where({ grant_id: grantId, user_id: userId })
.del();
}

module.exports = { followGrant, unfollowGrant };
3 changes: 2 additions & 1 deletion packages/server/src/lib/grantsCollaboration/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { saveNoteRevision, getOrganizationNotesForGrant } = require('./notes');
const { followGrant, unfollowGrant } = require('./followers');

module.exports = {
saveNoteRevision, getOrganizationNotesForGrant,
saveNoteRevision, getOrganizationNotesForGrant, followGrant, unfollowGrant,
};

0 comments on commit 0f61dab

Please sign in to comment.