@@ -2,7 +2,13 @@ import { and, eq, exists } from "drizzle-orm";
2
2
import { Hono } from "hono" ;
3
3
import { HTTPException } from "hono/http-exception" ;
4
4
import { db } from "service/db/client.ts" ;
5
- import { matches , participants , projects , ratings , roles } from "service/db/schema.ts" ;
5
+ import {
6
+ matches ,
7
+ participants ,
8
+ projects ,
9
+ ratings ,
10
+ roles ,
11
+ } from "service/db/schema.ts" ;
6
12
import { getBrowserID } from "service/features/auth/index.ts" ;
7
13
import type { HonoOptions } from "service/types.ts" ;
8
14
import { json , param } from "service/validator/hono.ts" ;
@@ -42,7 +48,11 @@ const route = new Hono<HonoOptions>()
42
48
async ( c ) => {
43
49
const browser_id = await getBrowserID ( c ) ;
44
50
const projectId = c . req . valid ( "param" ) . projectId ;
45
- const project_resp = db ( c ) . select ( ) . from ( projects ) . where ( eq ( projects . id , projectId ) ) . execute ( ) ;
51
+ const project_resp = db ( c )
52
+ . select ( )
53
+ . from ( projects )
54
+ . where ( eq ( projects . id , projectId ) )
55
+ . execute ( ) ;
46
56
const project = project_resp . then ( ( it ) => {
47
57
const project = it [ 0 ] ;
48
58
if ( ! project ) {
@@ -108,7 +118,8 @@ const route = new Hono<HonoOptions>()
108
118
] )
109
119
. returning ( )
110
120
) [ 0 ] ;
111
- if ( ! project_resp ) throw new HTTPException ( 500 , { message : "failed to create project" } ) ;
121
+ if ( ! project_resp )
122
+ throw new HTTPException ( 500 , { message : "failed to create project" } ) ;
112
123
await db ( c )
113
124
. insert ( participants )
114
125
. values ( [
@@ -138,23 +149,32 @@ const route = new Hono<HonoOptions>()
138
149
roles : roles_resp ,
139
150
} ) ;
140
151
} )
141
- . delete ( "/:projectId" , param ( { projectId : v . pipe ( v . string ( ) , v . uuid ( ) ) } ) , async ( c ) => {
142
- const browser_id = await getBrowserID ( c ) ;
143
- const { projectId : project_id } = c . req . valid ( "param" ) ;
144
- const d = db ( c ) ;
145
- await d . delete ( projects ) . where (
146
- and (
147
- eq ( projects . id , project_id ) ,
148
- exists (
149
- d
150
- . select ( )
151
- . from ( participants )
152
- . where ( and ( eq ( participants . browser_id , browser_id ) , eq ( participants . is_admin , 1 ) ) ) ,
152
+ . delete (
153
+ "/:projectId" ,
154
+ param ( { projectId : v . pipe ( v . string ( ) , v . uuid ( ) ) } ) ,
155
+ async ( c ) => {
156
+ const browser_id = await getBrowserID ( c ) ;
157
+ const { projectId : project_id } = c . req . valid ( "param" ) ;
158
+ const d = db ( c ) ;
159
+ await d . delete ( projects ) . where (
160
+ and (
161
+ eq ( projects . id , project_id ) ,
162
+ exists (
163
+ d
164
+ . select ( )
165
+ . from ( participants )
166
+ . where (
167
+ and (
168
+ eq ( participants . browser_id , browser_id ) ,
169
+ eq ( participants . is_admin , 1 ) ,
170
+ ) ,
171
+ ) ,
172
+ ) ,
153
173
) ,
154
- ) ,
155
- ) ;
156
- return c . json ( { ok : true } , 200 ) ;
157
- } )
174
+ ) ;
175
+ return c . json ( { ok : true } , 200 ) ;
176
+ } ,
177
+ )
158
178
159
179
. put ( "/:projectId/finalize" , param ( { projectId : v . string ( ) } ) , async ( c ) => {
160
180
const browser_id = await getBrowserID ( c ) ;
@@ -165,7 +185,10 @@ const route = new Hono<HonoOptions>()
165
185
const participant_resp = await db ( c )
166
186
. select ( )
167
187
. from ( participants )
168
- . where ( eq ( participants . browser_id , browser_id ) && eq ( participants . project_id , c . req . param ( "projectId" ) ) ) ;
188
+ . where (
189
+ eq ( participants . browser_id , browser_id ) &&
190
+ eq ( participants . project_id , c . req . param ( "projectId" ) ) ,
191
+ ) ;
169
192
if ( participant_resp [ 0 ] ?. is_admin !== 1 ) {
170
193
return c . json ( { message : "Unauthorized" } , 401 ) ;
171
194
}
@@ -183,7 +206,10 @@ const route = new Hono<HonoOptions>()
183
206
. where ( eq ( ratings . project_id , projectId ) )
184
207
. orderBy ( ratings . participant_id , ratings . role_id ) ;
185
208
186
- const ratingsByParticipant = Map . groupBy ( participantsData , ( item ) => item . participant_id ) ;
209
+ const ratingsByParticipant = Map . groupBy (
210
+ participantsData ,
211
+ ( item ) => item . participant_id ,
212
+ ) ;
187
213
188
214
const ratingsArray : number [ ] [ ] = [ ] ; // TODO: 型付けをマシにする
189
215
const participantIndexIdMap : string [ ] = [ ] ;
@@ -193,13 +219,21 @@ const route = new Hono<HonoOptions>()
193
219
participantIndexIdMap . push ( r [ 0 ] ?. participant_id ?? "-" ) ;
194
220
} ) ;
195
221
196
- const roleConstraints = await db ( c ) . select ( ) . from ( roles ) . where ( eq ( roles . project_id , projectId ) ) . orderBy ( roles . id ) ;
222
+ const roleConstraints = await db ( c )
223
+ . select ( )
224
+ . from ( roles )
225
+ . where ( eq ( roles . project_id , projectId ) )
226
+ . orderBy ( roles . id ) ;
197
227
const minMaxConstraints = roleConstraints . map ( ( role ) => ( {
198
228
min : role . min ,
199
229
max : role . max ,
200
230
} ) ) ;
201
231
202
- const result = assignRoles ( ratingsArray , at ( ratingsArray , 0 ) . length , minMaxConstraints ) ;
232
+ const result = assignRoles (
233
+ ratingsArray ,
234
+ at ( ratingsArray , 0 ) . length ,
235
+ minMaxConstraints ,
236
+ ) ;
203
237
204
238
await db ( c )
205
239
. insert ( matches )
0 commit comments