File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,37 +58,38 @@ export default typedPlugin(
5858 ...secondlyRatelimit ( 5 ) ,
5959 } ,
6060 async ( req , res ) => {
61- const { firstSetup , id } = await getZipline ( ) ;
61+ await getZipline ( ) ;
6262
63- if ( ! firstSetup ) throw new ApiError ( 9001 ) ;
63+ const { username, password } = req . body ;
64+
65+ const hashed = await hashPassword ( password ) ;
66+ const token = createToken ( ) ;
6467
6568 logger . info ( 'first setup running' ) ;
6669
67- const { username, password } = req . body ;
70+ const user = await prisma . $transaction ( async ( tx ) => {
71+ const claimed = await tx . zipline . updateMany ( {
72+ where : { firstSetup : true } ,
73+ data : { firstSetup : false } ,
74+ } ) ;
6875
69- const user = await prisma . user . create ( {
70- data : {
71- username,
72- password : await hashPassword ( password ) ,
73- role : 'SUPERADMIN' ,
74- token : createToken ( ) ,
75- } ,
76- select : userSelect ,
76+ if ( claimed . count === 0 ) throw new ApiError ( 9001 ) ;
77+
78+ return tx . user . create ( {
79+ data : {
80+ username,
81+ password : hashed ,
82+ role : 'SUPERADMIN' ,
83+ token,
84+ } ,
85+ select : userSelect ,
86+ } ) ;
7787 } ) ;
7888
7989 logger . info ( 'first setup complete' ) ;
8090
81- await prisma . zipline . update ( {
82- where : {
83- id,
84- } ,
85- data : {
86- firstSetup : false ,
87- } ,
88- } ) ;
89-
9091 return res . send ( {
91- firstSetup,
92+ firstSetup : false ,
9293 user,
9394 } ) ;
9495 } ,
You can’t perform that action at this time.
0 commit comments