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: add API to retrieve grant notes for specific user via Finder API #3471

4 changes: 2 additions & 2 deletions packages/server/src/lib/grantsCollaboration/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { saveNoteRevision, getOrganizationNotesForGrant } = require('./notes');
const { saveNoteRevision, getOrganizationNotesForGrant, getOrganizationNotesForGrantByUser } = require('./notes');
const {
followGrant, unfollowGrant, getFollowerForGrant, getFollowersForGrant,
} = require('./followers');

module.exports = {
saveNoteRevision, getOrganizationNotesForGrant, followGrant, unfollowGrant, getFollowerForGrant, getFollowersForGrant,
saveNoteRevision, getOrganizationNotesForGrant, getOrganizationNotesForGrantByUser, followGrant, unfollowGrant, getFollowerForGrant, getFollowersForGrant,
};
46 changes: 41 additions & 5 deletions packages/server/src/lib/grantsCollaboration/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@
return grantNotesRevisionId;
}

async function getOrganizationNotesForGrant(knex, grantId, organizationId, { afterRevision, limit = 50 } = {}) {
async function getOrganizationNotesForGrantByUser(
knex,
organizationId,
userId,
grantId,
{ afterRevision, limit = 50 } = {}

Check failure on line 40 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Missing trailing comma
) {
return getCurrentNoteRevisions(knex, { grantId, userId }, { afterRevision, limit });

Check failure on line 42 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

'getCurrentNoteRevisions' was used before it was defined
}

async function getCurrentNoteRevisions(
knex,
{ grantId, organizationId, userId } = {},

Check failure on line 47 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Trailing spaces not allowed
{ afterRevision, limit = 50 } = {}

Check failure on line 48 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Missing trailing comma
) {
const subquery = knex.select([
'r.id',
'r.grant_note_id',
Expand Down Expand Up @@ -64,9 +78,22 @@
.joinRaw(`LEFT JOIN LATERAL (${subquery.toQuery()}) AS rev ON rev.grant_note_id = grant_notes.id`)
.join('users', 'users.id', 'grant_notes.user_id')
.join('agencies', 'agencies.id', 'users.agency_id')
.join('tenants', 'tenants.id', 'users.tenant_id')
.where('grant_notes.grant_id', grantId)
.andWhere('tenants.id', organizationId);
.join('tenants', 'tenants.id', 'users.tenant_id');

// Conditionally applying filters based on grantID if it is null or undefined or not

Check failure on line 83 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Expected indentation of 4 spaces but found 8
if (grantId != null && grantId != undefined) {

Check failure on line 84 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Expected indentation of 4 spaces but found 8

Check failure on line 84 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Expected '!==' and instead saw '!='
Fixed Show fixed Hide fixed
query = query.where('grant_notes.grant_id', grantId);

Check failure on line 85 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Expected indentation of 8 spaces but found 12
}

Check failure on line 86 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Expected indentation of 4 spaces but found 8

Check failure on line 87 in packages/server/src/lib/grantsCollaboration/notes.js

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Trailing spaces not allowed
// Conditionally applying filters based on organizationID if it is null or undefined or not
if (organizationId != null && organizationId != undefined) {
Fixed Show fixed Hide fixed
query = query.andWhere('tenants.id', organizationId);
}

// Conditionally applying filters based on userID if it is null or undefined or not
if (userId != null && userId != undefined) {
Fixed Show fixed Hide fixed
query = query.andWhere('grant_notes.user_id', userId)
}

if (afterRevision) {
query = query.andWhere('rev.id', '>', afterRevision);
Expand Down Expand Up @@ -100,4 +127,13 @@
};
}

module.exports = { saveNoteRevision, getOrganizationNotesForGrant };
async function getOrganizationNotesForGrant(
knex,
grantId,
organizationId,
{ afterRevision, limit = 50 } = {}
) {
return getCurrentNoteRevisions(knex, { grantId, organizationId }, { afterRevision, limit });
}

module.exports = { saveNoteRevision, getCurrentNoteRevisions, getOrganizationNotesForGrant, getOrganizationNotesForGrantByUser };
33 changes: 33 additions & 0 deletions packages/server/src/routes/grants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { requireUser, isUserAuthorized } = require('../lib/access-helpers');
const knex = require('../db/connection');
const {
saveNoteRevision, followGrant, unfollowGrant, getFollowerForGrant, getFollowersForGrant, getOrganizationNotesForGrant,
getOrganizationNotesForGrantByUser,
} = require('../lib/grantsCollaboration');

const router = express.Router({ mergeParams: true });
Expand Down Expand Up @@ -45,6 +46,38 @@ router.get('/', requireUser, async (req, res) => {
res.json(grants);
});

// getting notes for a specific user and grant
router.get('/:grantId/notes/user/:userId', requireUser, async (req, res) => {
const { grantId, userId } = req.params;
const { tenant_id: organizationId } = req.session.user;
const { paginateFrom, limit } = req.query;

// Converting limit to an integer
const limitInt = limit ? parseInt(limit, 10) : undefined;

// Validating the limit query parameter
if (limit && (!Number.isInteger(limitInt) || limitInt < 1 || limitInt > 100)) {
res.status(400).send('Invalid limit parameter');
return;
}

try {
// Fetching the notes using getOrganizationNotesForGrantByUser function
const notes = await getOrganizationNotesForGrantByUser(
knex,
organizationId,
userId,
grantId,
{ afterRevision: paginateFrom, limit: limitInt },
);

// sending the notes as JSON response
res.json(notes);
} catch (error) {
res.status(500).json({ error: 'Failed to retrieve notes' });
}
});

function criteriaToFiltersObj(criteria, agencyId) {
const filters = criteria || {};
const postedWithinOptions = {
Expand Down
Loading