diff --git a/server.ts b/server.ts index d96fe70..bfe9f5f 100644 --- a/server.ts +++ b/server.ts @@ -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_OAUTH_PROVIDER = 'microsoft'; 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); @@ -673,37 +678,44 @@ app.get('/api/tasks/list', async (c) => { ]; 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 { - const records = await taskPb.collection(PB_TASKS_COLLECTION).getFullList({ - filter: attempt.filter, + const accessibleTasks = await taskPb.collection(candidate).getFullList({ sort: '-startDate,-created', expand: 'user', fields: baseFields, }); - if (records.length) { - return { records: uniqueById(records), lookupMethod: attempt.lookupMethod }; - } + const filtered = accessibleTasks.filter((task: any) => taskMatchesTarget(task, ids, nameHints)); + return { + records: uniqueById(filtered), + lookupMethod: `accessible_fallback:${candidate}`, + collectionUsed: candidate, + }; } catch (error) { lastError = error; } } - try { - const accessibleTasks = await taskPb.collection(PB_TASKS_COLLECTION).getFullList({ - 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; - } + if (lastError) throw lastError; + return { records: [], lookupMethod: 'no_collection_match', collectionUsed: '' }; }; const resolveUserIdsByName = async (name: string): Promise => { @@ -749,6 +761,7 @@ app.get('/api/tasks/list', async (c) => { let effectiveUserId = requestedUserId || meId; let effectiveUserName = targetUserName; let lookupMethod = 'unresolved'; + let tasksCollection = PB_TASKS_COLLECTION_CANDIDATES[0] || ''; try { if (targetUserName) { const targetUserIds = Array.from(new Set([ @@ -763,8 +776,9 @@ app.get('/api/tasks/list', async (c) => { const result = await fetchTasksByRelationIds(targetUserIds, [targetUserName, meName]); records = result.records; lookupMethod = result.lookupMethod; + tasksCollection = result.collectionUsed || tasksCollection; if (!records.length) { - warning = `No accessible Tasgird tasks matched Users.name "${targetUserName}".`; + warning = `No accessible tasks matched Users.name "${targetUserName}".`; } effectiveUserId = targetUserIds[0] || effectiveUserId; } @@ -773,8 +787,9 @@ app.get('/api/tasks/list', async (c) => { const result = await fetchTasksByRelationIds([fallbackId], [meName]); records = result.records; lookupMethod = result.lookupMethod; + tasksCollection = result.collectionUsed || tasksCollection; if (!records.length) { - warning = 'No accessible Tasgird tasks matched the current user relation.'; + warning = 'No accessible tasks matched the current user relation.'; } } else { records = []; @@ -785,6 +800,7 @@ app.get('/api/tasks/list', async (c) => { success: false, message: 'Failed to load accessible tasks from PocketBase', details: message, + tasksCollectionsTried: PB_TASKS_COLLECTION_CANDIDATES, userId: meId || requestedUserId, userName: targetUserName || meName, }, 500); @@ -804,6 +820,7 @@ app.get('/api/tasks/list', async (c) => { collectionId: String(tokenPayload?.collectionId || ''), collectionName: String(tokenPayload?.collectionName || ''), }, + tasksCollection, count: records.length, lookupMethod, warning: warning || undefined,