43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { v } from "convex/values";
|
|
|
|
import type { Doc } from "./_generated/dataModel";
|
|
import { query } from "./_generated/server";
|
|
import { requireCurrentOrganization, requireWorkMember } from "./authz";
|
|
|
|
export const listRecent = query({
|
|
args: { limit: v.optional(v.number()) },
|
|
handler: async (ctx, args): Promise<readonly Doc<"works">[]> => {
|
|
const { organizationId } = await requireCurrentOrganization(ctx);
|
|
const limit = Math.min(Math.max(Math.floor(args.limit ?? 50), 1), 100);
|
|
return await ctx.db
|
|
.query("works")
|
|
.withIndex("by_organizationId_and_createdAt", (q) =>
|
|
q.eq("organizationId", organizationId)
|
|
)
|
|
.order("desc")
|
|
.take(limit);
|
|
},
|
|
});
|
|
|
|
export const get = query({
|
|
args: { workId: v.id("works") },
|
|
handler: async (ctx, args) => {
|
|
await requireWorkMember(ctx, args.workId);
|
|
const work = await ctx.db.get(args.workId);
|
|
if (!work) {
|
|
return null;
|
|
}
|
|
const threads = await ctx.db
|
|
.query("threads")
|
|
.withIndex("by_workId_and_createdAt", (q) => q.eq("workId", args.workId))
|
|
.order("asc")
|
|
.collect();
|
|
const artifacts = await ctx.db
|
|
.query("artifacts")
|
|
.withIndex("by_workId_and_createdAt", (q) => q.eq("workId", args.workId))
|
|
.order("desc")
|
|
.take(50);
|
|
return { artifacts, threads, work };
|
|
},
|
|
});
|