Skip to content

Commit e97b5f0

Browse files
committed
cascade requests and filters working as intended (finally)
1 parent 8787e49 commit e97b5f0

File tree

6 files changed

+278
-81
lines changed

6 files changed

+278
-81
lines changed

server/[email protected]

48 KB
Binary file not shown.

server/src/generic-controller.ts

Lines changed: 61 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -356,24 +356,13 @@ export class GenericController {
356356
}
357357

358358
/** Filter records dynamically based on provided query parameters */
359-
static async cascade_filter<T extends ModelKey>(modelName: T, filters: any) {
360-
console.log(`Filtering ${modelName} with filters:`, filters);
361-
362-
const formattedName = this.formatModelName(modelName);
363-
const model = models[formattedName];
364-
365-
if (!model) {
366-
return { error: `Invalid model name: ${modelName}`, status: 400 };
367-
}
368-
369-
// Prepare conditions for each model
370-
const whereConditions: Record<string, any> = {};
371-
const includeRelations: any[] = [];
372-
359+
static async cascade_filter<T extends ModelKey>(filters: any) {
360+
console.log(`Filtering papers with filters:`, filters);
373361
// Extract filters for different models
374362
const modelFilters: Record<string, any> = {
375363
paper: {},
376364
parts: {},
365+
authors: {},
377366
tids: {},
378367
sees: {},
379368
dds: {},
@@ -385,34 +374,34 @@ export class GenericController {
385374
if (key.includes(".")) {
386375
const [relation, field] = key.split(".");
387376
if (modelFilters[relation] !== undefined) {
388-
modelFilters[relation][field] = filters[key];
377+
modelFilters[relation][field] =
378+
relation === "authors"
379+
? { [Op.like]: `%${filters[key]}%` }
380+
: { [Op.like]: `${filters[key]}%` };
389381
} else {
390382
console.warn(`Invalid related model in filter: ${relation}`);
391383
return { error: `Invalid relation '${relation}'`, status: 400 };
392384
}
393385
} else {
394-
modelFilters.paper[key] = filters[key]; // Direct paper filters
386+
modelFilters.paper[key] = { [Op.like]: `%${filters[key]}%` };
395387
}
396388
}
397389

398390
console.log("Parsed Filters:", modelFilters);
399391

392+
const noTestFilters = () =>
393+
Object.keys(modelFilters.tids).length === 0 &&
394+
Object.keys(modelFilters.sees).length === 0 &&
395+
Object.keys(modelFilters.dds).length === 0;
396+
400397
// Build Include Array Dynamically
401398
const buildInclude = (modelKey: string, modelRef: any, alias: string) => {
402-
if (Object.keys(modelFilters[modelKey]).length > 0) {
403-
return {
404-
model: modelRef,
405-
as: alias,
406-
required: true, // Enforce filtering at SQL level
407-
where: modelFilters[modelKey],
408-
attributes: {
409-
exclude: ["createdAt", "updatedAt", "paperId", "partId"],
410-
}, // Exclude unwanted fields
411-
};
412-
}
399+
const filter = modelFilters[modelKey];
413400
return {
414401
model: modelRef,
415402
as: alias,
403+
required: Object.keys(filter).length > 0, // Only INNER JOIN if we have filters
404+
where: filter,
416405
attributes: {
417406
exclude: ["createdAt", "updatedAt", "paperId", "partId"],
418407
}, // Exclude unwanted fields
@@ -430,6 +419,7 @@ export class GenericController {
430419
through: {
431420
attributes: [], // This will exclude the 'paper_author' relationship table fields
432421
},
422+
where: modelFilters.authors, // Apply author filters here
433423
}, // Exclude fields for authors
434424
buildInclude("parts", models.Part, "parts"),
435425
{
@@ -438,8 +428,8 @@ export class GenericController {
438428
required: Object.keys(modelFilters.parts).length > 0,
439429
where: modelFilters.parts,
440430
include: [
441-
buildInclude("tids", models.Tid, "tids"),
442431
buildInclude("sees", models.See, "sees"),
432+
buildInclude("tids", models.Tid, "tids"),
443433
buildInclude("dds", models.Dd, "dds"),
444434
],
445435
attributes: { exclude: ["createdAt", "updatedAt", "paper_part"] }, // Exclude fields for parts
@@ -454,56 +444,49 @@ export class GenericController {
454444
console.log("Found records:", records.length);
455445

456446
// Flatten Results & Remove Unfiltered Tests
457-
return records.flatMap((paper) => {
458-
const paperData = paper.get({ plain: true });
459-
const { parts, ...paperWithoutParts } = paperData;
460-
461-
return parts.flatMap((part: any) => {
462-
const { tids, sees, dds, ...partWithoutTests } = part;
463-
464-
return [
465-
...tids
466-
.filter(
467-
(t: { [x: string]: any }) =>
468-
Object.keys(modelFilters.tids).length === 0 ||
469-
modelFilters.tids.some(
470-
(f: string | number) => t[f] === filters[`tids.${f}`],
471-
),
472-
)
473-
.map((tid: any) => ({
474-
paper: paperWithoutParts,
475-
part: partWithoutTests,
476-
tid,
477-
})),
478-
...sees
479-
.filter(
480-
(s: { [x: string]: any }) =>
481-
Object.keys(modelFilters.sees).length === 0 ||
482-
modelFilters.sees.some(
483-
(f: string | number) => s[f] === filters[`sees.${f}`],
484-
),
485-
)
486-
.map((see: any) => ({
487-
paper: paperWithoutParts,
488-
part: partWithoutTests,
489-
see,
490-
})),
491-
...dds
492-
.filter(
493-
(d: { [x: string]: any }) =>
494-
Object.keys(modelFilters.dds).length === 0 ||
495-
modelFilters.dds.some(
496-
(f: string | number) => d[f] === filters[`dds.${f}`],
497-
),
498-
)
499-
.map((dd: any) => ({
500-
paper: paperWithoutParts,
501-
part: partWithoutTests,
502-
dd,
503-
})),
504-
];
505-
});
506-
});
447+
return Promise.all(
448+
records.flatMap((paper) => {
449+
const paperData = paper.get({ plain: true });
450+
const { parts, ...paperWithoutParts } = paperData;
451+
452+
return parts.flatMap((part: any) => {
453+
const { tids, sees, dds, ...partWithoutTests } = part;
454+
const results = [];
455+
456+
if (Object.keys(modelFilters.tids).length > 0 || noTestFilters()) {
457+
results.push(
458+
...tids.map((tid: any) => ({
459+
paper: paperWithoutParts,
460+
part: partWithoutTests,
461+
tid,
462+
})),
463+
);
464+
}
465+
466+
if (Object.keys(modelFilters.sees).length > 0 || noTestFilters()) {
467+
results.push(
468+
...sees.map((see: any) => ({
469+
paper: paperWithoutParts,
470+
part: partWithoutTests,
471+
see,
472+
})),
473+
);
474+
}
475+
476+
if (Object.keys(modelFilters.dds).length > 0 || noTestFilters()) {
477+
results.push(
478+
...dds.map((dd: any) => ({
479+
paper: paperWithoutParts,
480+
part: partWithoutTests,
481+
dd,
482+
})),
483+
);
484+
}
485+
486+
return results;
487+
});
488+
}),
489+
);
507490
}
508491

509492
/** Create full paper along with related entities */

server/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async function initializeSystem(): Promise<{
119119
const results = await gptController.processRadiationPapers(pdfFile);
120120

121121
fs.writeFileSync(
122-
"./test/1-paper-output.json",
122+
"./test/1-paper-output-hope.json",
123123
JSON.stringify(results, null, 4),
124124
);
125125

server/src/refined-prompts.data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const questions = {
9595
prompt: `Describe the type of radiation testing that was conducted.:
9696
9797
- This testing will be either TID SEE(General) OR DD You may ONLY return types TID, SEE or DD for this step
98-
- Facility name
98+
- Find the facility name and remember it for future questions
9999
100100
There might be more than one test for the given part, return a list of different test types, if two tests of the same type are found create two entries, for one each
101101

server/src/routes/cascade-router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function cascadeRouter(dbController: GenericController): Router {
1616

1717
console.log(`Filtering ${modelName} with`, filters);
1818

19-
const records = await GenericController.cascade_filter("papers", filters);
19+
const records = await GenericController.cascade_filter(filters);
2020

2121
if (!Array.isArray(records) && "error" in records) {
2222
res.status(records.status || 500).json({ error: records.error });

0 commit comments

Comments
 (0)