core: ensure problem config correct for getList

This commit is contained in:
undefined committed 2025-11-20 13:23:58 +08:00
1 parent a2d2741682
commit 6c5db5e997
1 file changed
+18 -8
+18 -8
View File
@@ -202,7 +202,7 @@ export class ProblemModel {
.project(buildProjection(projection)).limit(1).toArray())[0]; .project(buildProjection(projection)).limit(1).toArray())[0];
if (!res) return null; if (!res) return null;
try { try {
if (!rawConfig) res.config = await parseConfig(res.config, res.data?.map((i) => i.name) || []); if (!rawConfig && projection.includes('config')) res.config = await parseConfig(res.config, res.data?.map((i) => i.name) || []);
} catch (e) { } catch (e) {
res.config = `Cannot parse: ${e.message}`; res.config = `Cannot parse: ${e.message}`;
} }
@@ -387,20 +387,30 @@ export class ProblemModel {
const r: Record<number, ProblemDoc> = {}; const r: Record<number, ProblemDoc> = {};
const l: Record<string, ProblemDoc> = {}; const l: Record<string, ProblemDoc> = {};
const q: any = { docId: { $in: pids } }; const q: any = { docId: { $in: pids } };
const projectionExpr = buildProjection(projection.includes('config') ? [...projection, 'data', 'reference'] : projection);
let pdocs = await document.getMulti(domainId, document.TYPE_PROBLEM, q) let pdocs = await document.getMulti(domainId, document.TYPE_PROBLEM, q)
.project<ProblemDoc>(buildProjection(projection)).toArray(); .project<ProblemDoc>(projectionExpr).toArray();
if (canViewHidden !== true) { if (canViewHidden !== true) {
pdocs = pdocs.filter((i) => i.owner === canViewHidden || i.maintainer?.includes(canViewHidden as any) || !i.hidden); pdocs = pdocs.filter((i) => i.owner === canViewHidden || i.maintainer?.includes(canViewHidden as any) || !i.hidden);
} }
for (const pdoc of pdocs) { await Promise.all(pdocs.map(async (pdoc) => {
try { if (projection.includes('config')) {
pdoc.config = await parseConfig(pdoc.config as string, pdoc.data?.map((i) => i.name) || []); if (pdoc.reference) {
} catch (e) { const src = await ProblemModel.get(pdoc.reference.domainId, pdoc.reference.pid);
pdoc.config = `Cannot parse: ${e.message}`; pdoc.config = src ? src.config : 'Cannot find source problem';
} else {
try {
pdoc.config = await parseConfig(pdoc.config as string, pdoc.data?.map((i) => i.name) || []);
} catch (e) {
pdoc.config = `Cannot parse: ${e.message}`;
}
}
} }
if (!projection.includes('data')) delete pdoc.data;
if (!projection.includes('reference')) delete pdoc.reference;
r[pdoc.docId] = pdoc; r[pdoc.docId] = pdoc;
if (pdoc.pid) l[pdoc.pid] = pdoc; if (pdoc.pid) l[pdoc.pid] = pdoc;
} }));
// TODO enhance // TODO enhance
if (pdocs.length !== pids.length) { if (pdocs.length !== pids.length) {
for (const pid of pids) { for (const pid of pids) {