0.3.5 共享功能修复
This commit is contained in:
@@ -2,6 +2,7 @@ import { mutation, query } from "./_generated/server";
|
||||
import { v } from "convex/values";
|
||||
import { getAuthUserId } from "@convex-dev/auth/server";
|
||||
import { nowIso } from "./_utils/time";
|
||||
import type { Id } from "./_generated/dataModel";
|
||||
|
||||
async function requireUserId(ctx: any): Promise<string> {
|
||||
const userId = await getAuthUserId(ctx);
|
||||
@@ -48,6 +49,49 @@ export const listByWorkspace = query({
|
||||
},
|
||||
});
|
||||
|
||||
export const listMineByWorkspace = query({
|
||||
args: { workspaceId: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
await requireWorkspaceMember(ctx, args.workspaceId, userId);
|
||||
|
||||
const memberships = await ctx.db
|
||||
.query("group_members")
|
||||
.withIndex("by_workspace_user", (q: any) => q.eq("workspace_id", args.workspaceId).eq("user_id", userId))
|
||||
.collect();
|
||||
|
||||
const groupsById = new Map<string, any>();
|
||||
for (const m of memberships) {
|
||||
const group = await ctx.db
|
||||
.query("groups")
|
||||
.withIndex("by_group_id", (q: any) => q.eq("id", m.group_id))
|
||||
.first();
|
||||
if (group) {
|
||||
groupsById.set(String(group.id), {
|
||||
id: group.id,
|
||||
workspace_id: group.workspace_id,
|
||||
name: group.name,
|
||||
created_by: group.created_by,
|
||||
created_at: group.created_at,
|
||||
updated_at: group.updated_at,
|
||||
my_role: m.role,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const rows = Array.from(groupsById.values());
|
||||
rows.sort((a: any, b: any) => String(a.created_at ?? "").localeCompare(String(b.created_at ?? "")));
|
||||
|
||||
// 额外:带上当前用户用户名,前端可用
|
||||
const me = await ctx.db.get(userId as Id<"users">);
|
||||
|
||||
return {
|
||||
me: { userId, username: me?.name ?? null },
|
||||
groups: rows,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export const create = mutation({
|
||||
args: {
|
||||
id: v.string(),
|
||||
@@ -159,4 +203,3 @@ export const remove = mutation({
|
||||
return { ok: true };
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user