Skip to content

Commit

Permalink
Merge branch 'credential-engine' into ce-deliverytype-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ChelseaKR authored Jan 17, 2025
2 parents 49838a5 + 7a3f6dc commit 54d1199
Showing 1 changed file with 50 additions and 27 deletions.
77 changes: 50 additions & 27 deletions backend/src/domain/search/searchTrainings.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import NodeCache from "node-cache";
// import * as Sentry from "@sentry/node";
import {SearchTrainings} from "../types";
import {credentialEngineAPI} from "../../credentialengine/CredentialEngineAPI";
import {credentialEngineUtils} from "../../credentialengine/CredentialEngineUtils";
import {CTDLResource} from "../credentialengine/CredentialEngine";
import {getLocalExceptionCounties} from "../utils/getLocalExceptionCounties";
import {DataClient} from "../DataClient";
import {getHighlight} from "../utils/getHighlight";
import {TrainingData, TrainingResult} from "../training/TrainingResult";
import { SearchTrainings } from "../types";
import { credentialEngineAPI } from "../../credentialengine/CredentialEngineAPI";
import { credentialEngineUtils } from "../../credentialengine/CredentialEngineUtils";
import { CTDLResource } from "../credentialengine/CredentialEngine";
import { getLocalExceptionCounties } from "../utils/getLocalExceptionCounties";
import { DataClient } from "../DataClient";
import { getHighlight } from "../utils/getHighlight";
import { TrainingData, TrainingResult } from "../training/TrainingResult";
import zipcodeJson from "../utils/zip-county.json";
import zipcodes, {ZipCode} from "zipcodes";
import { convertZipCodeToCounty } from "../utils/convertZipCodeToCounty";
import {DeliveryType} from "../DeliveryType";
import {normalizeCipCode} from "../utils/normalizeCipCode";
import {normalizeSocCode} from "../utils/normalizeSocCode";
import { DeliveryType } from "../DeliveryType";
import { normalizeCipCode } from "../utils/normalizeCipCode";
import { normalizeSocCode } from "../utils/normalizeSocCode";

// Initializing a simple in-memory cache
const cache = new NodeCache({ stdTTL: 300, checkperiod: 120 });

const fetchAllCerts = async (query: object, offset = 0, limit = 10): Promise<{ allCerts: CTDLResource[]; totalResults: number }> => {
const searchTrainingPrograms = async (query: object, offset = 0, limit = 10): Promise<{ allCerts: CTDLResource[]; totalResults: number }> => {
try {
// console.log(`FETCHING RECORD with offset ${offset} and limit ${limit}`);
const response = await credentialEngineAPI.getResults(query, offset, limit);
Expand All @@ -34,19 +34,19 @@ const fetchAllCerts = async (query: object, offset = 0, limit = 10): Promise<{ a
};


const fetchAllCertsInBatches = async (query: object, batchSize = 100) => {
const searchTrainingProgramsInBatches = async (query: object, batchSize = 100) => {
const allCerts: CTDLResource[] = [];
const initialResponse = await fetchAllCerts(query, 0, batchSize);
const initialResponse = await searchTrainingPrograms(query, 0, batchSize);
const totalResults = initialResponse.totalResults;
allCerts.push(...initialResponse.allCerts);

const fetchBatch = async (offset: number) => {
try {
const response = await fetchAllCerts(query, offset, batchSize);
const response = await searchTrainingPrograms(query, offset, batchSize);
return response.allCerts;
} catch (error) {
console.error(`Error fetching batch at offset ${offset}:`, error);
return []; // Skip this batch
return [];
}
};

Expand Down Expand Up @@ -261,7 +261,7 @@ export const searchTrainingsFactory = (dataClient: DataClient): SearchTrainings
// If unfiltered results are not in cache, fetch the results
if (!unFilteredResults) {
console.log(`Fetching results for query: ${params.searchQuery}`);
const { allCerts } = await fetchAllCertsInBatches(query);
const { allCerts } = await searchTrainingProgramsInBatches(query);
unFilteredResults = await Promise.all(
allCerts.map((certificate) =>
transformCertificateToTraining(dataClient, certificate, params.searchQuery)
Expand Down Expand Up @@ -332,23 +332,48 @@ function buildQuery(params: {
let termGroup: TermGroup = {
"search:operator": "search:orTerms",
...(isSOC || isCIP || !!isZipCode || isCounty ? undefined : {
"ceterms:name": { "search:value": params.searchQuery, "search:matchType": "search:contains" },
"ceterms:description": { "search:value": params.searchQuery, "search:matchType": "search:contains" },
"ceterms:ownedBy": { "ceterms:name": { "search:value": params.searchQuery, "search:matchType": "search:contains" } }
"ceterms:name": [
{ "search:value": params.searchQuery, "search:matchType": "search:exact" },
{ "search:value": params.searchQuery, "search:matchType": "search:contains" },
],
"ceterms:description": [
{ "search:value": params.searchQuery, "search:matchType": "search:exact" },
{ "search:value": params.searchQuery, "search:matchType": "search:contains" },
],
"ceterms:ownedBy": {
"ceterms:name": {
"search:value": params.searchQuery,
"search:matchType": "search:contains"
}
}
}),
"ceterms:occupationType": isSOC ? {
"ceterms:codedNotation": { "search:value": params.searchQuery, "search:matchType": "search:startsWith" }
"ceterms:codedNotation": {
"search:value": params.searchQuery,
"search:matchType": "search:startsWith"
}
} : undefined,
"ceterms:instructionalProgramType": isCIP ? {
"ceterms:codedNotation": { "search:value": params.searchQuery, "search:matchType": "search:startsWith" }
"ceterms:instructionalProgramType": isCIP ?
{"ceterms:codedNotation": {
"search:value": params.searchQuery,
"search:matchType": "search:startsWith"
}
} : undefined
};

if (hasMultipleParts) {
termGroup = {
"search:operator": "search:andTerms",
"ceterms:ownedBy": { "ceterms:name": { "search:value": ownedByPart, "search:matchType": "search:contains" } },
"ceterms:name": { "search:value": trainingPart, "search:matchType": "search:contains" }
"ceterms:ownedBy": {
"ceterms:name": {
"search:value": ownedByPart,
"search:matchType": "search:contains"
}
},
"ceterms:name": {
"search:value": trainingPart,
"search:matchType": "search:contains"
}
};
}

Expand Down Expand Up @@ -430,8 +455,6 @@ async function transformCertificateToTraining(
}
}



function packageResults(page: number, limit: number, results: TrainingResult[], totalResults: number): TrainingData {
const totalPages = Math.ceil(totalResults / limit);
const hasPreviousPage = page > 1;
Expand Down

0 comments on commit 54d1199

Please sign in to comment.