Skip to content

Commit

Permalink
Merge pull request #464 from USEPA/feature/update-nces-data
Browse files Browse the repository at this point in the history
Feature/update nces data
  • Loading branch information
courtneymyers authored Aug 22, 2024
2 parents aea6fdb + 163f8c1 commit 4070b45
Show file tree
Hide file tree
Showing 8 changed files with 526,760 additions and 31 deletions.
File renamed without changes.
526,664 changes: 526,664 additions & 0 deletions app/server/app/content/nces-2024.json

Large diffs are not rendered by default.

72 changes: 43 additions & 29 deletions app/server/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,56 @@ requiredEnvironmentVariables.forEach((variable) => {
* Fetch NCES JSON data from S3 bucket or read from local file system.
*/
function fetchNcesData() {
const localFilePath = resolve(__dirname, "./content", "nces.json");
const s3FileUrl = `${s3BucketUrl}/content/nces.json`;

const logMessage =
NODE_ENV === "development"
? "Reading NCES.json file from disk."
: `Fetching NCES.json from S3 bucket.`;

log({ level: "info", message: logMessage });

return Promise.resolve(
/**
* local development: read file directly from disk
* Cloud.gov: fetch file from the public s3 bucket
*/
NODE_ENV === "development"
? readFile(localFilePath, "utf8").then((string) => JSON.parse(string))
: axios.get(s3FileUrl).then((res) => res.data),
).catch((error) => {
const errorStatus = error.response?.status || 500;
const errorMethod = error.response?.config?.method?.toUpperCase();
const errorUrl = error.response?.config?.url;

const logMessage = `S3 Error: ${errorStatus} ${errorMethod} ${errorUrl}`;
log({ level: "error", message: logMessage });

process.exitCode = 1;
});
/** NOTE: static content files found in `app/server/app/content/` directory. */
const filenames = ["nces-2023.json", "nces-2024.json"];

return Promise.all(
filenames.map((filename) => {
const localFilePath = resolve(__dirname, "./content", filename);
const s3FileUrl = `${s3BucketUrl}/content/${filename}`;

const logMessage =
NODE_ENV === "development"
? `Reading ${filename} file from disk.`
: `Fetching ${filename} from S3 bucket.`;

log({ level: "info", message: logMessage });

/**
* local development: read files directly from disk
* Cloud.gov: fetch files from the public s3 bucket
*/
return NODE_ENV === "development"
? readFile(localFilePath, "utf8").then((string) => JSON.parse(string))
: axios.get(s3FileUrl).then((res) => res.data);
}),
)
.then((data) => {
return {
2023: data[0],
2024: data[1],
};
})
.catch((error) => {
console.log(error);

const errorStatus = error.response?.status || 500;
const errorMethod = error.response?.config?.method?.toUpperCase();
const errorUrl = error.response?.config?.url;

const logMessage = `S3 Error: ${errorStatus} ${errorMethod} ${errorUrl}`;
log({ level: "error", message: logMessage });

process.exitCode = 1;
});
}

fetchNcesData().then((ncesData) => {
const app = express();
const port = PORT || 3001;

/** Store NCES JSON data in the Express app's locals object. */
app.locals.ncesData = ncesData;
app.locals.nces = ncesData;

app.use(helmet({ contentSecurityPolicy: false }));
app.use(helmet.hsts({ maxAge: 31536000 }));
Expand Down
7 changes: 7 additions & 0 deletions app/server/app/routes/formio2023.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {
verifyMongoObjectId,
} = require("../middleware");
const {
searchNcesData,
//
uploadS3FileMetadata,
downloadS3FileMetadata,
//
Expand Down Expand Up @@ -36,6 +38,11 @@ const router = express.Router();

router.use(ensureAuthenticated);

// --- search 2023 NCES data with the provided NCES ID and return a match
router.get("nces/:searchText", (req, res) => {
searchNcesData({ rebateYear, req, res });
});

// --- download Formio S3 file metadata
router.get(
"/s3/:formType/:mongoId/:comboKey/storage/s3",
Expand Down
7 changes: 7 additions & 0 deletions app/server/app/routes/formio2024.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {
verifyMongoObjectId,
} = require("../middleware");
const {
searchNcesData,
//
uploadS3FileMetadata,
downloadS3FileMetadata,
//
Expand Down Expand Up @@ -36,6 +38,11 @@ const router = express.Router();

router.use(ensureAuthenticated);

// --- search 2024 NCES data with the provided NCES ID and return a match
router.get("nces/:searchText", (req, res) => {
searchNcesData({ rebateYear, req, res });
});

// --- download Formio S3 file metadata
router.get(
"/s3/:formType/:mongoId/:comboKey/storage/s3",
Expand Down
2 changes: 1 addition & 1 deletion app/server/app/routes/formioNCES.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ router.get("/:searchText?", (req, res) => {
return res.json({});
}

const result = req.app.locals.ncesData.find((item) => {
const result = req.app.locals.nces[2023].find((item) => {
return item["NCES ID"] === searchText;
});

Expand Down
2 changes: 1 addition & 1 deletion app/server/app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ router.use("/api/content", require("./content"));
router.use("/api/user", require("./user"));
router.use("/api/config", require("./config"));
router.use("/api/bap", require("./bap"));
router.use("/api/formio/nces", require("./formioNCES"));
router.use("/api/formio/nces", require("./formioNCES")); // TODO: remove this route and file after NCES URL has been updated in 2022 FRF
router.use("/api/formio/2022", require("./formio2022"));
router.use("/api/formio/2023", require("./formio2023"));
router.use("/api/formio/2024", require("./formio2024"));
Expand Down
37 changes: 37 additions & 0 deletions app/server/app/utilities/formio.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@ const { NODE_ENV } = process.env;
* @typedef {'2022' | '2023' | '2024'} RebateYear
*/

/**
* @param {Object} param
* @param {RebateYear} param.rebateYear
* @param {express.Request} param.req
* @param {express.Response} param.res
*/
function searchNcesData({ rebateYear, req, res }) {
const { searchText } = req.params;

if (!searchText) {
const logMessage = `No NCES ID passed to NCES data lookup.`;
log({ level: "info", message: logMessage, req });

return res.json({});
}

if (searchText.length !== 7) {
const logMessage = `Invalid NCES ID '${searchText}' passed to NCES data lookup.`;
log({ level: "info", message: logMessage, req });

return res.json({});
}

const result = req.app.locals.nces[rebateYear].find((item) => {
return item["NCES ID"] === searchText;
});

const logMessage =
`${rebateYear} NCES data searched with NCES ID '${searchText}' resulting in ` +
`${result ? "a match" : "no matches"}.`;
log({ level: "info", message: logMessage, req });

return res.json({ ...result });
}

/**
* @param {Object} param
* @param {RebateYear} param.rebateYear
Expand Down Expand Up @@ -1847,6 +1882,8 @@ function fetchChangeRequest({ rebateYear, req, res }) {
}

module.exports = {
searchNcesData,
//
uploadS3FileMetadata,
downloadS3FileMetadata,
//
Expand Down

0 comments on commit 4070b45

Please sign in to comment.