Skip to content

Commit 3628677

Browse files
committed
use naive stringify to search displayable_data
1 parent 785ad67 commit 3628677

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

packages/db/src/impl/couch/courseDB.ts

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,13 @@ above:\n${above.rows.map((r) => `\t${r.id}-${r.key}\n`)}`;
342342
tagId: string,
343343
updateELO?: boolean
344344
): Promise<PouchDB.Core.Response> {
345-
return await addTagToCard(this.id, cardId, tagId, (await this._getCurrentUser()).getUsername(), updateELO);
345+
return await addTagToCard(
346+
this.id,
347+
cardId,
348+
tagId,
349+
(await this._getCurrentUser()).getUsername(),
350+
updateELO
351+
);
346352
}
347353

348354
async removeTagFromCard(cardId: string, tagId: string): Promise<PouchDB.Core.Response> {
@@ -602,30 +608,98 @@ above:\n${above.rows.map((r) => `\t${r.id}-${r.key}\n`)}`;
602608

603609
// Admin search methods
604610
public async searchCards(query: string): Promise<any[]> {
605-
const displayableData = await this.db.find({
606-
selector: {
607-
docType: 'DISPLAYABLE_DATA',
608-
'data.data': { $regex: query },
609-
},
610-
});
611+
console.log(`[CourseDB ${this.id}] Searching for: "${query}"`);
612+
613+
// Try multiple search approaches
614+
let displayableData;
615+
616+
try {
617+
// Try regex search on the correct data structure: data[0].data
618+
displayableData = await this.db.find({
619+
selector: {
620+
docType: 'DISPLAYABLE_DATA',
621+
'data.0.data': { $regex: `.*${query}.*` },
622+
},
623+
});
624+
console.log(`[CourseDB ${this.id}] Regex search on data[0].data successful`);
625+
} catch (regexError) {
626+
console.log(
627+
`[CourseDB ${this.id}] Regex search failed, falling back to manual search:`,
628+
regexError
629+
);
630+
631+
// Fallback: get all displayable data and filter manually
632+
const allDisplayable = await this.db.find({
633+
selector: {
634+
docType: 'DISPLAYABLE_DATA',
635+
},
636+
});
637+
638+
console.log(
639+
`[CourseDB ${this.id}] Retrieved ${allDisplayable.docs.length} documents for manual filtering`
640+
);
641+
642+
displayableData = {
643+
docs: allDisplayable.docs.filter((doc) => {
644+
// Search entire document as JSON string - inclusive approach for admin tool
645+
const docString = JSON.stringify(doc).toLowerCase();
646+
const match = docString.includes(query.toLowerCase());
647+
if (match) {
648+
console.log(`[CourseDB ${this.id}] Manual match found in document: ${doc._id}`);
649+
}
650+
return match;
651+
}),
652+
};
653+
}
654+
655+
console.log(
656+
`[CourseDB ${this.id}] Found ${displayableData.docs.length} displayable data documents`
657+
);
658+
659+
if (displayableData.docs.length === 0) {
660+
// Debug: Let's see what displayable data exists
661+
const allDisplayableData = await this.db.find({
662+
selector: {
663+
docType: 'DISPLAYABLE_DATA',
664+
},
665+
limit: 5, // Just sample a few
666+
});
667+
668+
console.log(
669+
`[CourseDB ${this.id}] Sample displayable data:`,
670+
allDisplayableData.docs.map((d) => ({
671+
id: d._id,
672+
docType: d.docType,
673+
dataStructure: d.data ? Object.keys(d.data) : 'no data field',
674+
dataContent: d.data,
675+
fullDoc: d,
676+
}))
677+
);
678+
}
611679

612680
const allResults: any[] = [];
613-
681+
614682
for (const dd of displayableData.docs) {
615683
const cards = await this.db.find({
616684
selector: {
617685
docType: 'CARD',
618686
id_displayable_data: { $in: [dd._id] },
619687
},
620688
});
621-
689+
690+
console.log(
691+
`[CourseDB ${this.id}] Displayable data ${dd._id} linked to ${cards.docs.length} cards`
692+
);
622693
allResults.push(...cards.docs);
623694
}
624695

696+
console.log(`[CourseDB ${this.id}] Total cards found: ${allResults.length}`);
625697
return allResults;
626698
}
627699

628-
public async find(request: PouchDB.Find.FindRequest<any>): Promise<PouchDB.Find.FindResponse<any>> {
700+
public async find(
701+
request: PouchDB.Find.FindRequest<any>
702+
): Promise<PouchDB.Find.FindResponse<any>> {
629703
return this.db.find(request);
630704
}
631705
}

0 commit comments

Comments
 (0)