Use user name for tasks lookup with robust fallbacks
This commit is contained in:
@@ -495,39 +495,98 @@ app.get('/api/tasks/list', async (c) => {
|
||||
try {
|
||||
const me = pb.authStore.model;
|
||||
const meId = String(me?.id || '').trim();
|
||||
const meName = String(me?.name || me?.username || me?.email || '').trim();
|
||||
const requestedUserName = String(c.req.query('userName') || '').trim();
|
||||
const requestedUserId = String(c.req.query('userId') || '').trim();
|
||||
const targetUserId = requestedUserId || meId;
|
||||
if (!targetUserId) return c.json({ success: false, message: 'No target user available' }, 400);
|
||||
const targetUserName = requestedUserName || meName;
|
||||
if (!targetUserName && !requestedUserId && !meId) {
|
||||
return c.json({ success: false, message: 'No target user available' }, 400);
|
||||
}
|
||||
|
||||
const fetchTasks = async (userId: string) => {
|
||||
const filter = `user = "${userId.replace(/"/g, '\\"')}"`;
|
||||
const baseFields = 'id,user,title,startDate,dueDate,priority,completed,content,size,status,expand.user.id,expand.user.email,expand.user.name,expand.user.username';
|
||||
const fetchTasksWithFilter = async (filter: string) => {
|
||||
return pb.collection(PB_TASKS_COLLECTION).getFullList({
|
||||
filter,
|
||||
sort: '-startDate,-created',
|
||||
expand: 'user',
|
||||
fields: 'id,user,title,startDate,dueDate,priority,completed,content,size,status,expand.user.id,expand.user.email,expand.user.name,expand.user.username',
|
||||
fields: baseFields,
|
||||
});
|
||||
};
|
||||
|
||||
const fetchAllTasks = async () => {
|
||||
return pb.collection(PB_TASKS_COLLECTION).getFullList({
|
||||
sort: '-startDate,-created',
|
||||
expand: 'user',
|
||||
fields: baseFields,
|
||||
});
|
||||
};
|
||||
|
||||
const matchTaskByName = (task: any, name: string): boolean => {
|
||||
const target = String(name || '').trim().toLowerCase();
|
||||
if (!target) return false;
|
||||
const taskUserDisplay = String(task?.expand?.user?.name || task?.expand?.user?.username || task?.expand?.user?.email || '').trim().toLowerCase();
|
||||
const taskUserRaw = String(task?.user || '').trim().toLowerCase();
|
||||
const owner = String(task?.owner || '').trim().toLowerCase();
|
||||
const assignedTo = String(task?.assigned_to || '').trim().toLowerCase();
|
||||
const userName = String(task?.user_name || task?.Username || '').trim().toLowerCase();
|
||||
return [taskUserDisplay, taskUserRaw, owner, assignedTo, userName].some((v) => v && v === target);
|
||||
};
|
||||
|
||||
let records: any[] = [];
|
||||
let warning = '';
|
||||
let effectiveUserId = targetUserId;
|
||||
let effectiveUserId = requestedUserId || meId;
|
||||
let effectiveUserName = targetUserName;
|
||||
try {
|
||||
records = await fetchTasks(targetUserId);
|
||||
} catch (listErr) {
|
||||
if (targetUserId !== meId && meId) {
|
||||
const escapedName = targetUserName.replace(/"/g, '\\"');
|
||||
const escapedUserId = (requestedUserId || meId).replace(/"/g, '\\"');
|
||||
const candidateFilters = [
|
||||
targetUserName ? `user = "${escapedName}"` : '',
|
||||
targetUserName ? `user_name = "${escapedName}"` : '',
|
||||
targetUserName ? `Username = "${escapedName}"` : '',
|
||||
targetUserName ? `assigned_to = "${escapedName}"` : '',
|
||||
targetUserName ? `owner = "${escapedName}"` : '',
|
||||
escapedUserId ? `user = "${escapedUserId}"` : '',
|
||||
].filter(Boolean);
|
||||
|
||||
for (const filter of candidateFilters) {
|
||||
try {
|
||||
records = await fetchTasks(meId);
|
||||
const found = await fetchTasksWithFilter(filter);
|
||||
if (Array.isArray(found) && found.length) {
|
||||
records = found;
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
|
||||
if (!records.length) {
|
||||
const all = await fetchAllTasks();
|
||||
if (targetUserName) {
|
||||
records = all.filter((task: any) => matchTaskByName(task, targetUserName));
|
||||
} else if (escapedUserId) {
|
||||
records = all.filter((task: any) => String(task?.user || '') === escapedUserId);
|
||||
} else {
|
||||
records = all;
|
||||
}
|
||||
}
|
||||
} catch (listErr) {
|
||||
if (targetUserName && meName && targetUserName !== meName) {
|
||||
try {
|
||||
const all = await fetchAllTasks();
|
||||
records = all.filter((task: any) => matchTaskByName(task, meName));
|
||||
effectiveUserId = meId;
|
||||
effectiveUserName = meName;
|
||||
warning = 'Requested user tasks are restricted by PocketBase rules; showing your tasks instead.';
|
||||
} catch {
|
||||
records = [];
|
||||
effectiveUserId = meId;
|
||||
effectiveUserName = meName;
|
||||
warning = 'Task list is restricted by PocketBase rules; no accessible tasks for current user.';
|
||||
}
|
||||
} else {
|
||||
records = [];
|
||||
effectiveUserId = meId || targetUserId;
|
||||
effectiveUserId = meId || requestedUserId;
|
||||
effectiveUserName = targetUserName || meName;
|
||||
warning = 'Task list is restricted by PocketBase rules; no accessible tasks for current user.';
|
||||
}
|
||||
}
|
||||
@@ -535,6 +594,7 @@ app.get('/api/tasks/list', async (c) => {
|
||||
return c.json({
|
||||
success: true,
|
||||
userId: effectiveUserId,
|
||||
userName: effectiveUserName,
|
||||
count: records.length,
|
||||
warning: warning || undefined,
|
||||
tasks: records.map((task: any) => ({
|
||||
|
||||
Reference in New Issue
Block a user