feat: dispatch authenticated project issue requests
This commit is contained in:
@@ -1,8 +1,82 @@
|
||||
import {
|
||||
projectIssueDraftFromSignal,
|
||||
queueProjectIssue,
|
||||
validateProjectIssueDraft,
|
||||
type ProjectIssueDraft,
|
||||
} from "@code/primitives/project-issue";
|
||||
import { ConvexError, v } from "convex/values";
|
||||
import { Effect } from "effect";
|
||||
|
||||
import { mutation, query } from "./_generated/server";
|
||||
import type { Id } from "./_generated/dataModel";
|
||||
import { mutation, type MutationCtx, query } from "./_generated/server";
|
||||
import { requireProjectMember } from "./authz";
|
||||
|
||||
const toDomainError = (error: unknown) => {
|
||||
return new ConvexError(
|
||||
error instanceof Error ? error.message : "Invalid project issue"
|
||||
);
|
||||
};
|
||||
|
||||
const decodeDraft = async (input: unknown): Promise<ProjectIssueDraft> => {
|
||||
try {
|
||||
return await Effect.runPromise(validateProjectIssueDraft(input));
|
||||
} catch (error) {
|
||||
throw toDomainError(error);
|
||||
}
|
||||
};
|
||||
|
||||
const draftFromSignal = async (input: unknown): Promise<ProjectIssueDraft> => {
|
||||
try {
|
||||
return await Effect.runPromise(projectIssueDraftFromSignal(input));
|
||||
} catch (error) {
|
||||
throw toDomainError(error);
|
||||
}
|
||||
};
|
||||
|
||||
const insertIssue = async (
|
||||
ctx: MutationCtx,
|
||||
projectId: Id<"projects">,
|
||||
draft: ProjectIssueDraft
|
||||
): Promise<Id<"projectIssues">> => {
|
||||
const latest = await ctx.db
|
||||
.query("projectIssues")
|
||||
.withIndex("by_project_and_number", (q) => q.eq("projectId", projectId))
|
||||
.order("desc")
|
||||
.first();
|
||||
const timestamp = Date.now();
|
||||
const number = (latest?.number ?? 0) + 1;
|
||||
const issueId = await ctx.db.insert("projectIssues", {
|
||||
body: draft.body,
|
||||
createdAt: timestamp,
|
||||
number,
|
||||
projectId,
|
||||
status: "open",
|
||||
title: draft.title,
|
||||
updatedAt: timestamp,
|
||||
});
|
||||
const workArtifact = await ctx.db
|
||||
.query("projectArtifacts")
|
||||
.withIndex("by_project_and_path", (q) =>
|
||||
q.eq("projectId", projectId).eq("path", "work.md")
|
||||
)
|
||||
.unique();
|
||||
if (workArtifact) {
|
||||
await ctx.db.patch("projectArtifacts", workArtifact._id, {
|
||||
content: `${workArtifact.content}\n## Issue ${number}: ${draft.title}\n\nStatus: open\n\n${draft.body}\n`,
|
||||
revision: workArtifact.revision + 1,
|
||||
updatedAt: timestamp,
|
||||
});
|
||||
}
|
||||
await ctx.db.insert("projectEvents", {
|
||||
createdAt: timestamp,
|
||||
data: { number, title: draft.title },
|
||||
issueId,
|
||||
kind: "issue.created",
|
||||
projectId,
|
||||
});
|
||||
return issueId;
|
||||
};
|
||||
|
||||
export const create = mutation({
|
||||
args: {
|
||||
body: v.string(),
|
||||
@@ -11,55 +85,36 @@ export const create = mutation({
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
await requireProjectMember(ctx, args.projectId);
|
||||
const title = args.title.trim();
|
||||
const body = args.body.trim();
|
||||
if (title.length < 3 || title.length > 160) {
|
||||
throw new ConvexError("Issue title must be between 3 and 160 characters");
|
||||
const draft = await decodeDraft({ body: args.body, title: args.title });
|
||||
return insertIssue(ctx, args.projectId, draft);
|
||||
},
|
||||
});
|
||||
|
||||
export const createFromSignal = mutation({
|
||||
args: { signalId: v.id("signals") },
|
||||
handler: async (ctx, args) => {
|
||||
const signal = await ctx.db.get("signals", args.signalId);
|
||||
if (!signal) {
|
||||
throw new ConvexError("Signal not found");
|
||||
}
|
||||
if (body.length < 10 || body.length > 10_000) {
|
||||
if (!signal.projectId) {
|
||||
throw new ConvexError("Signal is not project-scoped");
|
||||
}
|
||||
const { organizationId } = await requireProjectMember(
|
||||
ctx,
|
||||
signal.projectId
|
||||
);
|
||||
if (signal.organizationId !== organizationId) {
|
||||
throw new ConvexError(
|
||||
"Issue description must be between 10 and 10000 characters"
|
||||
"Signal does not belong to the project organization"
|
||||
);
|
||||
}
|
||||
const latest = await ctx.db
|
||||
.query("projectIssues")
|
||||
.withIndex("by_project_and_number", (q) =>
|
||||
q.eq("projectId", args.projectId)
|
||||
)
|
||||
.order("desc")
|
||||
.first();
|
||||
const timestamp = Date.now();
|
||||
const number = (latest?.number ?? 0) + 1;
|
||||
const issueId = await ctx.db.insert("projectIssues", {
|
||||
body,
|
||||
createdAt: timestamp,
|
||||
number,
|
||||
projectId: args.projectId,
|
||||
status: "open",
|
||||
title,
|
||||
updatedAt: timestamp,
|
||||
const draft = await draftFromSignal({
|
||||
problemStatement: signal.problemStatement,
|
||||
signalId: String(signal._id),
|
||||
});
|
||||
const workArtifact = await ctx.db
|
||||
.query("projectArtifacts")
|
||||
.withIndex("by_project_and_path", (q) =>
|
||||
q.eq("projectId", args.projectId).eq("path", "work.md")
|
||||
)
|
||||
.unique();
|
||||
if (workArtifact) {
|
||||
await ctx.db.patch("projectArtifacts", workArtifact._id, {
|
||||
content: `${workArtifact.content}\n## Issue ${number}: ${title}\n\nStatus: open\n\n${body}\n`,
|
||||
revision: workArtifact.revision + 1,
|
||||
updatedAt: timestamp,
|
||||
});
|
||||
}
|
||||
await ctx.db.insert("projectEvents", {
|
||||
createdAt: timestamp,
|
||||
data: { number, title },
|
||||
issueId,
|
||||
kind: "issue.created",
|
||||
projectId: args.projectId,
|
||||
});
|
||||
return issueId;
|
||||
const issueId = await insertIssue(ctx, signal.projectId, draft);
|
||||
return { issueId, projectId: signal.projectId };
|
||||
},
|
||||
});
|
||||
|
||||
@@ -71,12 +126,18 @@ export const begin = mutation({
|
||||
throw new ConvexError("Issue not found");
|
||||
}
|
||||
await requireProjectMember(ctx, issue.projectId);
|
||||
if (issue.status === "queued" || issue.status === "working") {
|
||||
return issue.status;
|
||||
let status: "queued" | "working";
|
||||
try {
|
||||
status = await Effect.runPromise(queueProjectIssue(issue.status));
|
||||
} catch (error) {
|
||||
throw toDomainError(error);
|
||||
}
|
||||
if (status === issue.status) {
|
||||
return status;
|
||||
}
|
||||
const timestamp = Date.now();
|
||||
await ctx.db.patch("projectIssues", issue._id, {
|
||||
status: "queued",
|
||||
status,
|
||||
updatedAt: timestamp,
|
||||
});
|
||||
await ctx.db.insert("projectEvents", {
|
||||
|
||||
Reference in New Issue
Block a user