This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from HHS/simpler-grants-gov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchOpportunityAPI.ts
135 lines (112 loc) · 3.93 KB
/
SearchOpportunityAPI.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import "server-only";
import {
PaginationOrderBy,
PaginationRequestBody,
PaginationSortDirection,
SearchFetcherActionType,
SearchFilterRequestBody,
SearchRequestBody,
} from "../../types/search/searchRequestTypes";
import BaseApi from "./BaseApi";
import { QueryParamData } from "../../services/search/searchfetcher/SearchFetcher";
export default class SearchOpportunityAPI extends BaseApi {
get basePath(): string {
return process.env.API_URL || "";
}
get namespace(): string {
return "opportunities";
}
get headers() {
const baseHeaders = super.headers;
const searchHeaders = {};
return { ...baseHeaders, ...searchHeaders };
}
async searchOpportunities(searchInputs: QueryParamData) {
const { query } = searchInputs;
const filters = this.buildFilters(searchInputs);
const pagination = this.buildPagination(searchInputs);
const requestBody: SearchRequestBody = { pagination };
// Only add filters if there are some
if (Object.keys(filters).length > 0) {
requestBody.filters = filters;
}
if (query) {
requestBody.query = query;
}
const subPath = "search";
const response = await this.request(
"POST",
this.basePath,
this.namespace,
subPath,
searchInputs,
requestBody,
);
return response;
}
// Build with one_of syntax
private buildFilters(searchInputs: QueryParamData): SearchFilterRequestBody {
const { status, fundingInstrument, eligibility, agency, category } =
searchInputs;
const filters: SearchFilterRequestBody = {};
if (status && status.size > 0) {
filters.opportunity_status = { one_of: Array.from(status) };
}
if (fundingInstrument && fundingInstrument.size > 0) {
filters.funding_instrument = { one_of: Array.from(fundingInstrument) };
}
if (eligibility && eligibility.size > 0) {
// Note that eligibility gets remapped to the API name of "applicant_type"
filters.applicant_type = { one_of: Array.from(eligibility) };
}
if (agency && agency.size > 0) {
filters.agency = { one_of: Array.from(agency) };
}
if (category && category.size > 0) {
// Note that category gets remapped to the API name of "funding_category"
filters.funding_category = { one_of: Array.from(category) };
}
return filters;
}
private buildPagination(searchInputs: QueryParamData): PaginationRequestBody {
const { sortby, page, fieldChanged } = searchInputs;
// When performing an update (query, filter, sortby change) - we want to
// start back at the 1st page (we never want to retain the current page).
// In addition to this statement - on the client (handleSubmit in useSearchFormState), we
// clear the page query param and set the page back to 1.
// On initial load (SearchFetcherActionType.InitialLoad) we honor the page the user sent. There is validation guards
// in convertSearchParamstoProperTypes keep 1<= page <= max_possible_page
const page_offset =
searchInputs.actionType === SearchFetcherActionType.Update &&
fieldChanged !== "pagination"
? 1
: page;
const orderByFieldLookup = {
opportunityNumber: "opportunity_number",
opportunityTitle: "opportunity_title",
agency: "agency_code",
postedDate: "post_date",
closeDate: "close_date",
};
let order_by: PaginationOrderBy = "post_date";
if (sortby) {
for (const [key, value] of Object.entries(orderByFieldLookup)) {
if (sortby.startsWith(key)) {
order_by = value as PaginationOrderBy;
break; // Stop searching after the first match is found
}
}
}
// default to descending
let sort_direction: PaginationSortDirection = "descending";
if (sortby) {
sort_direction = sortby?.endsWith("Desc") ? "descending" : "ascending";
}
return {
order_by,
page_offset,
page_size: 25,
sort_direction,
};
}
}