Skip to content

Commit

Permalink
feat: add/fix fields in csv export (#2180)
Browse files Browse the repository at this point in the history
* feat: add/fix fields in csv export

* fix: column header flipped in test

---------
  • Loading branch information
replicantSocks authored Nov 21, 2023
1 parent 0459b8a commit 30826a4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
19 changes: 16 additions & 3 deletions packages/server/__tests__/api/grants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ describe('`/api/grants` endpoint', () => {
'Title',
'Viewed By',
'Interested Teams',
'Status',
'Opportunity Status',
'Opportunity Category',
'Cost Sharing',
'Award Ceiling',
Expand All @@ -487,11 +487,24 @@ describe('`/api/grants` endpoint', () => {
'Agency Code',
'Grant Id',
'URL',
'Funding Type',
'Appropriations Bill',
'Agency Code',
'Eligibility',
];

const txt = await response.text();
const rows = txt.split('\n');
expect(rows[0]).to.equal(expectedCsvHeaders.join(','));

const cells = rows[1].split(',');
const valMap = new Map([...Array(cells.length).keys()].map((i) => [expectedCsvHeaders[i], cells[i]]));

expect(txt.split('\n')[0]).to.equal(expectedCsvHeaders.join(','));
expect(txt.split('\n')[1]).to.contain('HHS-2021-IHS-TPI-0001,Community Health Aide Program: Tribal Planning &');
expect(valMap.get('Opportunity Number')).to.equal('HHS-2021-IHS-TPI-0001');
expect(valMap.get('Title')).to.equal('Community Health Aide Program: Tribal Planning & Implementation');
expect(valMap.get('Funding Type')).to.equal('Other');
expect(valMap.get('Agency Code')).to.equal('HHS-IHS');
expect(valMap.get('Eligibility')).to.equal('"Native American tribal organizations (other than Federally recognized tribal governments)|Others(see text field entitled ""Additional Information on Eligibility"" for clarification)|Native American tribal governments(Federally recognized)"');
});

it('produces same number of rows as grid', async () => {
Expand Down
27 changes: 24 additions & 3 deletions packages/server/src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,23 @@ function validateSearchFilters(filters) {
return errors;
}

function addCsvData(qb) {
qb
.select(knex.raw(`
CASE
WHEN grants.funding_instrument_codes = 'G' THEN 'Grant'
WHEN grants.funding_instrument_codes = 'CA' THEN 'Cooperative Agreement'
WHEN grants.funding_instrument_codes = 'PC' THEN 'Procurement Contract'
ELSE 'Other'
END as funding_type
`))
.select(knex.raw(`array_to_string(array_agg(${TABLES.eligibility_codes}.label), '|') AS eligibility`))
.leftJoin(
`${TABLES.eligibility_codes}`,
`${TABLES.eligibility_codes}.code`, '=', knex.raw(`ANY(string_to_array(${TABLES.grants}.eligibility_codes, ' '))`),
);
}

/*
filters: {
reviewStatuses: List[Enum['Applied', 'Not Applying', 'Interested']],
Expand All @@ -689,15 +706,15 @@ function validateSearchFilters(filters) {
tenantId: number
agencyId: number
*/
async function getGrantsNew(filters, paginationParams, orderingParams, tenantId, agencyId) {
console.log(JSON.stringify([filters, paginationParams, orderingParams, tenantId, agencyId]));
async function getGrantsNew(filters, paginationParams, orderingParams, tenantId, agencyId, toCsv) {
console.log(JSON.stringify([filters, paginationParams, orderingParams, tenantId, agencyId, toCsv]));

const errors = validateSearchFilters(filters);
if (errors.length > 0) {
throw new Error(`Invalid filters: ${errors.join(', ')}`);
}

const data = await knex(TABLES.grants)
const query = knex(TABLES.grants)
.select([
'grants.grant_id',
'grants.grant_number',
Expand Down Expand Up @@ -767,6 +784,10 @@ async function getGrantsNew(filters, paginationParams, orderingParams, tenantId,
'grants.funding_instrument_codes',
'grants.bill',
);
if (toCsv) {
query.modify(addCsvData);
}
const data = await query;

const fullCount = data.length > 0 ? data[0].full_count : 0;

Expand Down
1 change: 1 addition & 0 deletions packages/server/src/lib/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ async function getAndSendGrantForSavedSearch({
await db.buildPaginationParams({ currentPage: 1, perPage: 31 }),
{},
userSavedSearch.tenantId,
false,
);

return sendGrantDigest({
Expand Down
8 changes: 7 additions & 1 deletion packages/server/src/routes/grants.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ router.get('/next', requireUser, async (req, res) => {
orderingParams,
user.tenant_id,
user.agency_id,
false,
);

return res.json(grants);
Expand Down Expand Up @@ -121,6 +122,7 @@ router.get('/exportCSVNew', requireUser, async (req, res) => {
orderingParams,
user.tenant_id,
user.agency_id,
true,
);

// Generate CSV
Expand Down Expand Up @@ -155,7 +157,7 @@ router.get('/exportCSVNew', requireUser, async (req, res) => {
{ key: 'title', header: 'Title' },
{ key: 'viewed_by', header: 'Viewed By' },
{ key: 'interested_agencies', header: 'Interested Teams' },
{ key: 'opportunity_status', header: 'Status' },
{ key: 'opportunity_status', header: 'Opportunity Status' },
{ key: 'opportunity_category', header: 'Opportunity Category' },
{ key: 'cost_sharing', header: 'Cost Sharing' },
{ key: 'award_ceiling', header: 'Award Ceiling' },
Expand All @@ -164,6 +166,10 @@ router.get('/exportCSVNew', requireUser, async (req, res) => {
{ key: 'agency_code', header: 'Agency Code' },
{ key: 'grant_id', header: 'Grant Id' },
{ key: 'url', header: 'URL' },
{ key: 'funding_type', header: 'Funding Type' },
{ key: 'bill', header: 'Appropriations Bill' },
{ key: 'agency_code', header: 'Agency Code' },
{ key: 'eligibility', header: 'Eligibility' },
],
});

Expand Down

0 comments on commit 30826a4

Please sign in to comment.