Use exact-case TasGrid task collection

This commit is contained in:
2026-03-29 03:32:26 +00:00
parent d70a8b64c6
commit 602c275644
+41 -24
View File
@@ -38,7 +38,12 @@ const PB_BASE_URL = process.env.PB_URL || process.env.PB_DB || '';
const PB_AUTH_COLLECTION = process.env.PB_AUTH_COLLECTION || 'Users'; const PB_AUTH_COLLECTION = process.env.PB_AUTH_COLLECTION || 'Users';
const PB_OAUTH_PROVIDER = 'microsoft'; const PB_OAUTH_PROVIDER = 'microsoft';
const PB_NOTES_COLLECTION = process.env.PB_NOTES_COLLECTION || 'Notes'; const PB_NOTES_COLLECTION = process.env.PB_NOTES_COLLECTION || 'Notes';
const PB_TASKS_COLLECTION = 'Tasgird'; const PB_TASKS_COLLECTION_CANDIDATES = Array.from(new Set([
String(process.env.PB_TASKS_COLLECTION || 'TasGrid').trim(),
'TasGrid',
'Tasgird',
'Tasgrid',
].filter(Boolean)));
const pb = new PocketBase(PB_BASE_URL); const pb = new PocketBase(PB_BASE_URL);
@@ -673,37 +678,44 @@ app.get('/api/tasks/list', async (c) => {
]; ];
let lastError: unknown = null; let lastError: unknown = null;
for (const attempt of filterAttempts) { for (const candidate of PB_TASKS_COLLECTION_CANDIDATES) {
for (const attempt of filterAttempts) {
try {
const records = await taskPb.collection(candidate).getFullList({
filter: attempt.filter,
sort: '-startDate,-created',
expand: 'user',
fields: baseFields,
});
if (records.length) {
return { records: uniqueById(records), lookupMethod: `${attempt.lookupMethod}:${candidate}`, collectionUsed: candidate };
}
} catch (error) {
lastError = error;
}
}
}
for (const candidate of PB_TASKS_COLLECTION_CANDIDATES) {
try { try {
const records = await taskPb.collection(PB_TASKS_COLLECTION).getFullList({ const accessibleTasks = await taskPb.collection(candidate).getFullList({
filter: attempt.filter,
sort: '-startDate,-created', sort: '-startDate,-created',
expand: 'user', expand: 'user',
fields: baseFields, fields: baseFields,
}); });
if (records.length) { const filtered = accessibleTasks.filter((task: any) => taskMatchesTarget(task, ids, nameHints));
return { records: uniqueById(records), lookupMethod: attempt.lookupMethod }; return {
} records: uniqueById(filtered),
lookupMethod: `accessible_fallback:${candidate}`,
collectionUsed: candidate,
};
} catch (error) { } catch (error) {
lastError = error; lastError = error;
} }
} }
try { if (lastError) throw lastError;
const accessibleTasks = await taskPb.collection(PB_TASKS_COLLECTION).getFullList({ return { records: [], lookupMethod: 'no_collection_match', collectionUsed: '' };
sort: '-startDate,-created',
expand: 'user',
fields: baseFields,
});
const filtered = accessibleTasks.filter((task: any) => taskMatchesTarget(task, ids, nameHints));
return {
records: uniqueById(filtered),
lookupMethod: 'accessible_fallback',
};
} catch (error) {
if (lastError) throw lastError;
throw error;
}
}; };
const resolveUserIdsByName = async (name: string): Promise<string[]> => { const resolveUserIdsByName = async (name: string): Promise<string[]> => {
@@ -749,6 +761,7 @@ app.get('/api/tasks/list', async (c) => {
let effectiveUserId = requestedUserId || meId; let effectiveUserId = requestedUserId || meId;
let effectiveUserName = targetUserName; let effectiveUserName = targetUserName;
let lookupMethod = 'unresolved'; let lookupMethod = 'unresolved';
let tasksCollection = PB_TASKS_COLLECTION_CANDIDATES[0] || '';
try { try {
if (targetUserName) { if (targetUserName) {
const targetUserIds = Array.from(new Set([ const targetUserIds = Array.from(new Set([
@@ -763,8 +776,9 @@ app.get('/api/tasks/list', async (c) => {
const result = await fetchTasksByRelationIds(targetUserIds, [targetUserName, meName]); const result = await fetchTasksByRelationIds(targetUserIds, [targetUserName, meName]);
records = result.records; records = result.records;
lookupMethod = result.lookupMethod; lookupMethod = result.lookupMethod;
tasksCollection = result.collectionUsed || tasksCollection;
if (!records.length) { if (!records.length) {
warning = `No accessible Tasgird tasks matched Users.name "${targetUserName}".`; warning = `No accessible tasks matched Users.name "${targetUserName}".`;
} }
effectiveUserId = targetUserIds[0] || effectiveUserId; effectiveUserId = targetUserIds[0] || effectiveUserId;
} }
@@ -773,8 +787,9 @@ app.get('/api/tasks/list', async (c) => {
const result = await fetchTasksByRelationIds([fallbackId], [meName]); const result = await fetchTasksByRelationIds([fallbackId], [meName]);
records = result.records; records = result.records;
lookupMethod = result.lookupMethod; lookupMethod = result.lookupMethod;
tasksCollection = result.collectionUsed || tasksCollection;
if (!records.length) { if (!records.length) {
warning = 'No accessible Tasgird tasks matched the current user relation.'; warning = 'No accessible tasks matched the current user relation.';
} }
} else { } else {
records = []; records = [];
@@ -785,6 +800,7 @@ app.get('/api/tasks/list', async (c) => {
success: false, success: false,
message: 'Failed to load accessible tasks from PocketBase', message: 'Failed to load accessible tasks from PocketBase',
details: message, details: message,
tasksCollectionsTried: PB_TASKS_COLLECTION_CANDIDATES,
userId: meId || requestedUserId, userId: meId || requestedUserId,
userName: targetUserName || meName, userName: targetUserName || meName,
}, 500); }, 500);
@@ -804,6 +820,7 @@ app.get('/api/tasks/list', async (c) => {
collectionId: String(tokenPayload?.collectionId || ''), collectionId: String(tokenPayload?.collectionId || ''),
collectionName: String(tokenPayload?.collectionName || ''), collectionName: String(tokenPayload?.collectionName || ''),
}, },
tasksCollection,
count: records.length, count: records.length,
lookupMethod, lookupMethod,
warning: warning || undefined, warning: warning || undefined,