♻️ refactor: update states.ts
swadhinbiswas committed 3 months ago
84bfa47
1 parent 56a9aff
commit 84bfa47
1 file changed
+26 -12
Showing 1 changed file+26-12
▼src/pages/api/repos/[owner]/[repo]/settings/states.ts
+26-12
| @@ -6,11 +6,11 @@ import { nanoid } from "nanoid"; | ||
6 | 6 | import { z } from "zod"; |
7 | 7 | |
8 | 8 | const reviewerSchema = z.object({ |
9 | - userId: z.string().optional(), | |
10 | - teamId: z.string().optional(), | |
9 | + username: z.string().optional(), | |
10 | + teamName: z.string().optional(), | |
11 | 11 | requiredCount: z.number().int().positive().optional(), |
12 | -}).refine((value) => Boolean(value.userId) !== Boolean(value.teamId), { | |
13 | - message: "Each reviewer rule requires exactly one of userId or teamId", | |
12 | +}).refine((value) => Boolean(value.username) !== Boolean(value.teamName), { | |
13 | + message: "Each reviewer rule requires exactly one of username or teamName", | |
14 | 14 | }); |
15 | 15 | |
16 | 16 | const createStateSchema = z.object({ |
| @@ -25,7 +25,7 @@ const createStateSchema = z.object({ | ||
25 | 25 | reviewers: z.array(reviewerSchema).optional(), |
26 | 26 | }); |
27 | 27 | |
28 | -export const GET: APIRoute = withErrorHandler(async ({ params, locals }: APIContext) => { | |
28 | +export const GET: APIRoute = withErrorHandler(async ({ locals }: APIContext) => { | |
29 | 29 | const { repo } = locals as any; |
30 | 30 | const db = getDatabase(); |
31 | 31 | |
| @@ -40,7 +40,7 @@ export const GET: APIRoute = withErrorHandler(async ({ params, locals }: APICont | ||
40 | 40 | return success(states); |
41 | 41 | }); |
42 | 42 | |
43 | -export const POST: APIRoute = withErrorHandler(async ({ params, request, locals }: APIContext) => { | |
43 | +export const POST: APIRoute = withErrorHandler(async ({ request, locals }: APIContext) => { | |
44 | 44 | const { repo, user } = locals as any; |
45 | 45 | const db = getDatabase(); |
46 | 46 | |
| @@ -93,15 +93,29 @@ export const POST: APIRoute = withErrorHandler(async ({ params, request, locals | ||
93 | 93 | }).returning(); |
94 | 94 | |
95 | 95 | if (reviewers && reviewers.length > 0) { |
96 | - await (db as any).insert(schema.prStateReviewers).values( | |
97 | - reviewers.map((reviewer) => ({ | |
96 | + const resolvedReviewers = []; | |
97 | + for (const reviewer of reviewers) { | |
98 | + let userId = null; | |
99 | + let teamId = null; | |
100 | + if (reviewer.username) { | |
101 | + const foundUser = await db.query.users.findFirst({ | |
102 | + where: eq(schema.users.username, reviewer.username), | |
103 | + columns: { id: true } | |
104 | + }); | |
105 | + if (!foundUser) { | |
106 | + return badRequest(`User '${reviewer.username}' not found`); | |
107 | + } | |
108 | + userId = foundUser.id; | |
109 | + } | |
110 | + resolvedReviewers.push({ | |
98 | 111 | id: nanoid(), |
99 | 112 | stateDefinitionId: id, |
100 | - userId: reviewer.userId || null, | |
101 | - teamId: reviewer.teamId || null, | |
113 | + userId, | |
114 | + teamId, | |
102 | 115 | requiredCount: reviewer.requiredCount || 1, |
103 | - })) | |
104 | - ); | |
116 | + }); | |
117 | + } | |
118 | + await (db as any).insert(schema.prStateReviewers).values(resolvedReviewers); | |
105 | 119 | } |
106 | 120 | |
107 | 121 | const created = await db.query.prStateDefinitions.findFirst({ |
108 | 122 | |