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

fix: ensures opportunity_status is up to date #1920

Merged
merged 9 commits into from
Sep 18, 2023
117 changes: 108 additions & 9 deletions packages/server/src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,6 @@ function buildFiltersQuery(queryBuilder, filters, agencyId) {
if (parseInt(filters.assignedToAgencyId, 10) >= 0) {
qb.where(`${TABLES.assigned_grants_agency}.agency_id`, '=', filters.assignedToAgencyId);
}
if (filters.opportunityStatuses?.length) {
qb.whereIn(`${TABLES.grants}.opportunity_status`, filters.opportunityStatuses);
}
if (filters.opportunityCategories?.length) {
qb.whereIn(`${TABLES.grants}.opportunity_category`, filters.opportunityCategories);
}
Expand Down Expand Up @@ -575,6 +572,14 @@ function grantsQuery(queryBuilder, filters, agencyId, orderingParams, pagination
}
}

if (filters.opportunityStatuses?.length) {
queryBuilder.havingRaw(`
CASE
WHEN grants.archive_date <= now() THEN 'archived'
WHEN grants.close_date <= now() THEN 'closed'
ELSE 'posted'
END IN (${Array(filters.opportunityStatuses.length).fill('?').join(',')})`, filters.opportunityStatuses);
}
if (paginationParams) {
queryBuilder.limit(paginationParams.perPage);
queryBuilder.offset((paginationParams.currentPage - 1) * paginationParams.perPage);
Expand Down Expand Up @@ -605,13 +610,107 @@ function grantsQuery(queryBuilder, filters, agencyId, orderingParams, pagination
async function getGrantsNew(filters, paginationParams, orderingParams, tenantId, agencyId) {
console.log(filters, paginationParams, orderingParams, tenantId, agencyId);
const data = await knex(TABLES.grants)
.select(`${TABLES.grants}.*`)
.select([
'grants.grant_id',
'grants.grant_number',
'grants.title',
'grants.status',
'grants.agency_code',
'grants.award_ceiling',
'grants.cost_sharing',
'grants.cfda_list',
'grants.open_date',
'grants.close_date',
'grants.archive_date',
'grants.reviewer_name',
'grants.opportunity_category',
'grants.search_terms',
'grants.notes',
'grants.created_at',
'grants.updated_at',
'grants.description',
'grants.eligibility_codes',
'grants.raw_body',
'grants.award_floor',
'grants.revision_id',
'grants.title_ts',
'grants.description_ts',
'grants.funding_instrument_codes',
'grants.bill',
])
.select(knex.raw(`
CASE
WHEN grants.archive_date <= now() THEN 'archived'
WHEN grants.close_date <= now() THEN 'closed'
ELSE 'posted'
END as opportunity_status
`))
.distinct()
.modify((qb) => grantsQuery(qb, filters, agencyId, orderingParams, paginationParams));

const counts = await knex(TABLES.grants)
.modify((qb) => grantsQuery(qb, filters, agencyId, { orderBy: undefined }, null))
.countDistinct('grants.grant_id as total_grants');
.modify((qb) => grantsQuery(qb, filters, agencyId, orderingParams, paginationParams))
.groupBy(
'grants.grant_id',
'grants.grant_number',
'grants.title',
'grants.status',
'grants.agency_code',
'grants.award_ceiling',
'grants.cost_sharing',
'grants.cfda_list',
'grants.open_date',
'grants.close_date',
'grants.archive_date',
'grants.reviewer_name',
'grants.opportunity_category',
'grants.search_terms',
'grants.notes',
'grants.created_at',
'grants.updated_at',
'grants.description',
'grants.eligibility_codes',
'grants.raw_body',
'grants.award_floor',
'grants.revision_id',
'grants.title_ts',
'grants.description_ts',
'grants.funding_instrument_codes',
'grants.bill',
'grants.grant_number',
'grants.title',
'grants.status',
'grants.agency_code',
'grants.award_ceiling',
'grants.cost_sharing',
'grants.cfda_list',
'grants.open_date',
'grants.close_date',
'grants.reviewer_name',
'grants.opportunity_category',
'grants.search_terms',
'grants.notes',
'grants.created_at',
'grants.updated_at',
'grants.description',
'grants.eligibility_codes',
'grants.raw_body',
'grants.award_floor',
'grants.revision_id',
'grants.title_ts',
'grants.description_ts',
'grants.funding_instrument_codes',
'grants.bill',
);

const counts = await knex.with('filtered_grants', (qb) => {
qb.modify((q) => grantsQuery(q, filters, agencyId, { orderBy: undefined }, null))
.select([
'grants.grant_id',
'grants.open_date',
'grants.close_date',
'grants.archive_date',
])
.from('grants')
.groupBy('grants.grant_id', 'grants.open_date', 'grants.close_date', 'grants.archive_date');
}).countDistinct('filtered_grants.grant_id as total_grants').from('filtered_grants');

const pagination = {
total: parseInt(counts[0].total_grants, 10),
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/lib/grants-ingest.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function mapSourceDataToGrant(source) {
? milestones.close.date : '2100-01-01';
const today = moment().startOf('day');
if (milestones.archive_date && today.isSameOrAfter(moment(milestones.archive_date), 'date')) {
grant.archive_date = milestones.archive_date;
grant.opportunity_status = 'archived';
} else if (today.isSameOrAfter(moment(grant.close_date), 'date')) {
grant.opportunity_status = 'closed';
Expand Down
Loading