♻️ refactor: update [number].ts
swadhinbiswas committed 3 months ago
962fa93
1 parent 36377f7
commit 962fa93
1 file changed
+18 -4
Showing 1 changed file+18-4
▼src/pages/api/repos/[owner]/[repo]/issues/[number].ts
+18-4
| @@ -4,7 +4,7 @@ import { getDatabase, schema } from "@/db"; | ||
4 | 4 | import { eq, and } from "drizzle-orm"; |
5 | 5 | import { getUserFromRequest } from "@/lib/auth"; |
6 | 6 | import { unauthorized, badRequest, success, notFound, serverError } from "@/lib/api"; |
7 | -import { issues, issueLabels, issueAssignees } from "@/db/schema"; | |
7 | +import { issues, issueLabels } from "@/db/schema"; | |
8 | 8 | import { getRepoAndUser } from "@/lib/auth"; |
9 | 9 | import { logger } from "@/lib/logger"; |
10 | 10 | import type { NodePgDatabase } from "drizzle-orm/node-postgres"; |
| @@ -39,7 +39,7 @@ export const PATCH: APIRoute = async ({ request, params }) => { | ||
39 | 39 | |
40 | 40 | // Parse body |
41 | 41 | const body = await request.json(); |
42 | - const { title, description, state, type, parentId } = body; | |
42 | + const { title, description, state, type, parentId, labels } = body; | |
43 | 43 | |
44 | 44 | // Prepare updates |
45 | 45 | const updates: any = {}; |
| @@ -81,6 +81,22 @@ export const PATCH: APIRoute = async ({ request, params }) => { | ||
81 | 81 | .where(eq(issues.id, issue.id)); |
82 | 82 | } |
83 | 83 | |
84 | + // Handle label updates | |
85 | + if (Array.isArray(labels)) { | |
86 | + // Remove existing labels | |
87 | + await db.delete(issueLabels).where(eq(issueLabels.issueId, issue.id)); | |
88 | + // Add new labels | |
89 | + if (labels.length > 0) { | |
90 | + const { generateId } = await import("@/lib/utils"); | |
91 | + const labelInserts = labels.map((labelId: string) => ({ | |
92 | + id: generateId(), | |
93 | + issueId: issue.id, | |
94 | + labelId: labelId, | |
95 | + })); | |
96 | + await db.insert(issueLabels).values(labelInserts); | |
97 | + } | |
98 | + } | |
99 | + | |
84 | 100 | if (title !== undefined || description !== undefined) { |
85 | 101 | const linkText = `${title ?? issue.title} ${description ?? issue.body ?? ""}`; |
86 | 102 | try { |
| @@ -125,8 +141,6 @@ export const GET: APIRoute = async ({ request, params }) => { | ||
125 | 141 | const { owner, repo, number } = params; |
126 | 142 | if (!owner || !repo || !number) return badRequest("Missing parameters"); |
127 | 143 | |
128 | - const user = await getUserFromRequest(request); // Optional for public repos, but good to check context | |
129 | - | |
130 | 144 | const db = getDatabase(); |
131 | 145 | |
132 | 146 | // Check repo access |
133 | 147 | |