Resolve tasks by Users.name to Tasgird user relation

This commit is contained in:
2026-03-29 03:12:26 +00:00
parent b3b2a2e2f0
commit e6b727b345
+41 -21
View File
@@ -533,23 +533,37 @@ app.get('/api/tasks/list', async (c) => {
}
const baseFields = 'id,user,title,startDate,dueDate,priority,completed,content,size,status,owner,assigned_to,user_name,Username,expand.user.id,expand.user.email,expand.user.name,expand.user.username';
const fetchAllTasks = async () => {
const fetchTasksByRelationIds = async (userIds: string[]) => {
const ids = Array.from(new Set(userIds.map((id) => String(id || '').trim()).filter(Boolean)));
if (!ids.length) return [];
const relationFilter = ids.map((id) => `user = "${id.replace(/"/g, '\\"')}"`).join(' || ');
return taskPb.collection(PB_TASKS_COLLECTION).getFullList({
filter: relationFilter,
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);
const resolveUserIdsByName = async (name: string): Promise<string[]> => {
const target = String(name || '').trim();
if (!target) return [];
const escaped = target.replace(/"/g, '\\"');
const matched = await taskPb.collection(PB_AUTH_COLLECTION).getFullList({
filter: `name = "${escaped}"`,
fields: 'id,name,email,username',
});
if (matched.length) {
return matched.map((user: any) => String(user.id || '').trim()).filter(Boolean);
}
if (meId && target === meName) {
return [meId];
}
return [];
};
let records: any[] = [];
@@ -557,14 +571,26 @@ app.get('/api/tasks/list', async (c) => {
let effectiveUserId = requestedUserId || meId;
let effectiveUserName = targetUserName;
try {
const all = await fetchAllTasks();
if (targetUserName) {
records = all.filter((task: any) => matchTaskByName(task, targetUserName));
const targetUserIds = await resolveUserIdsByName(targetUserName);
if (!targetUserIds.length) {
warning = `No Users record matched name "${targetUserName}".`;
records = [];
} else {
records = await fetchTasksByRelationIds(targetUserIds);
if (!records.length) {
warning = `No accessible Tasgird tasks matched Users.name "${targetUserName}".`;
}
effectiveUserId = targetUserIds[0] || effectiveUserId;
}
} else if (requestedUserId || meId) {
const fallbackId = String(requestedUserId || meId).trim();
records = all.filter((task: any) => String(task?.user || '').trim() === fallbackId);
records = await fetchTasksByRelationIds([fallbackId]);
if (!records.length) {
warning = 'No accessible Tasgird tasks matched the current user relation.';
}
} else {
records = all;
records = [];
}
} catch (listErr) {
const message = (listErr as Error)?.message || String(listErr);
@@ -577,12 +603,6 @@ app.get('/api/tasks/list', async (c) => {
}, 500);
}
if (!records.length) {
warning = targetUserName
? `No accessible Tasgird tasks matched user name "${targetUserName}".`
: 'No accessible Tasgird tasks matched the current user.';
}
return c.json({
success: true,
userId: effectiveUserId,
@@ -594,7 +614,7 @@ app.get('/api/tasks/list', async (c) => {
user: task.user || task.expand?.user?.id || null,
userDisplay: task.expand?.user
? (task.expand.user.name || task.expand.user.username || task.expand.user.email || task.expand.user.id)
: null,
: (task.user_name || task.Username || task.owner || task.assigned_to || task.user || null),
title: task.title ?? '',
startDate: task.startDate ?? null,
dueDate: task.dueDate ?? null,