Resolve tasks by Users.name to Tasgird user relation
This commit is contained in:
@@ -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 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({
|
return taskPb.collection(PB_TASKS_COLLECTION).getFullList({
|
||||||
|
filter: relationFilter,
|
||||||
sort: '-startDate,-created',
|
sort: '-startDate,-created',
|
||||||
expand: 'user',
|
expand: 'user',
|
||||||
fields: baseFields,
|
fields: baseFields,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const matchTaskByName = (task: any, name: string): boolean => {
|
const resolveUserIdsByName = async (name: string): Promise<string[]> => {
|
||||||
const target = String(name || '').trim().toLowerCase();
|
const target = String(name || '').trim();
|
||||||
if (!target) return false;
|
if (!target) return [];
|
||||||
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 escaped = target.replace(/"/g, '\\"');
|
||||||
const owner = String(task?.owner || '').trim().toLowerCase();
|
const matched = await taskPb.collection(PB_AUTH_COLLECTION).getFullList({
|
||||||
const assignedTo = String(task?.assigned_to || '').trim().toLowerCase();
|
filter: `name = "${escaped}"`,
|
||||||
const userName = String(task?.user_name || task?.Username || '').trim().toLowerCase();
|
fields: 'id,name,email,username',
|
||||||
return [taskUserDisplay, taskUserRaw, owner, assignedTo, userName].some((v) => v && v === target);
|
});
|
||||||
|
|
||||||
|
if (matched.length) {
|
||||||
|
return matched.map((user: any) => String(user.id || '').trim()).filter(Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (meId && target === meName) {
|
||||||
|
return [meId];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
};
|
};
|
||||||
|
|
||||||
let records: any[] = [];
|
let records: any[] = [];
|
||||||
@@ -557,14 +571,26 @@ app.get('/api/tasks/list', async (c) => {
|
|||||||
let effectiveUserId = requestedUserId || meId;
|
let effectiveUserId = requestedUserId || meId;
|
||||||
let effectiveUserName = targetUserName;
|
let effectiveUserName = targetUserName;
|
||||||
try {
|
try {
|
||||||
const all = await fetchAllTasks();
|
|
||||||
if (targetUserName) {
|
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) {
|
} else if (requestedUserId || meId) {
|
||||||
const fallbackId = String(requestedUserId || meId).trim();
|
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 {
|
} else {
|
||||||
records = all;
|
records = [];
|
||||||
}
|
}
|
||||||
} catch (listErr) {
|
} catch (listErr) {
|
||||||
const message = (listErr as Error)?.message || String(listErr);
|
const message = (listErr as Error)?.message || String(listErr);
|
||||||
@@ -577,12 +603,6 @@ app.get('/api/tasks/list', async (c) => {
|
|||||||
}, 500);
|
}, 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({
|
return c.json({
|
||||||
success: true,
|
success: true,
|
||||||
userId: effectiveUserId,
|
userId: effectiveUserId,
|
||||||
@@ -594,7 +614,7 @@ app.get('/api/tasks/list', async (c) => {
|
|||||||
user: task.user || task.expand?.user?.id || null,
|
user: task.user || task.expand?.user?.id || null,
|
||||||
userDisplay: task.expand?.user
|
userDisplay: task.expand?.user
|
||||||
? (task.expand.user.name || task.expand.user.username || task.expand.user.email || task.expand.user.id)
|
? (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 ?? '',
|
title: task.title ?? '',
|
||||||
startDate: task.startDate ?? null,
|
startDate: task.startDate ?? null,
|
||||||
dueDate: task.dueDate ?? null,
|
dueDate: task.dueDate ?? null,
|
||||||
|
|||||||
Reference in New Issue
Block a user