1- import { cacheWrapper } from '@openops/server-shared' ;
1+ import { AppSystemProp , cacheWrapper , system } from '@openops/server-shared' ;
22import {
33 ApplicationError ,
44 assertValidEmail ,
@@ -17,6 +17,7 @@ import {
1717 UserStatus ,
1818 UserWithOrganization ,
1919} from '@openops/shared' ;
20+ import bcrypt from 'bcrypt' ;
2021import dayjs from 'dayjs' ;
2122import { passwordHasher } from '../authentication/basic/password-hasher' ;
2223import { repoFactory } from '../core/db/repo-factory' ;
@@ -29,17 +30,24 @@ export const userService = {
2930 async create ( params : CreateParams ) : Promise < User > {
3031 const hashedPassword = await passwordHasher . hash ( params . password ) ;
3132
32- const user : NewUser = {
33+ return saveUser ( {
3334 id : openOpsId ( ) ,
3435 ...params ,
3536 organizationRole : params . organizationRole ,
3637 status : UserStatus . ACTIVE ,
3738 password : hashedPassword ,
38- } ;
39-
40- sendUserCreatedEvent ( user . id , user . organizationId ) ;
39+ } ) ;
40+ } ,
41+ async createAdminUser ( params : CreateParams ) : Promise < User > {
42+ const hashedPassword = await bcrypt . hash ( params . password , getStaticSalt ( ) ) ;
4143
42- return userRepo ( ) . save ( user ) ;
44+ return saveUser ( {
45+ id : openOpsId ( ) ,
46+ ...params ,
47+ organizationRole : params . organizationRole ,
48+ status : UserStatus . ACTIVE ,
49+ password : hashedPassword ,
50+ } ) ;
4351 } ,
4452 async update ( {
4553 id,
@@ -209,13 +217,13 @@ export const userService = {
209217 } ) ;
210218 } ,
211219
212- async updatePassword ( {
220+ async updateAdminPassword ( {
213221 id,
214222 newPassword,
215223 } : UpdatePasswordParams ) : Promise < User > {
216224 assertValidPassword ( newPassword ) ;
217225
218- const hashedPassword = await passwordHasher . hash ( newPassword ) ;
226+ const hashedPassword = await bcrypt . hash ( newPassword , getStaticSalt ( ) ) ;
219227
220228 await userRepo ( ) . update ( id , {
221229 updated : dayjs ( ) . toISOString ( ) ,
@@ -307,6 +315,16 @@ export const userService = {
307315 } ,
308316} ;
309317
318+ function saveUser ( user : NewUser ) : Promise < User > {
319+ sendUserCreatedEvent ( user . id , user . organizationId ) ;
320+
321+ return userRepo ( ) . save ( user ) ;
322+ }
323+
324+ function getStaticSalt ( ) : string {
325+ return system . getOrThrow < string > ( AppSystemProp . OPENOPS_ADMIN_PASSWORD_SALT ) ;
326+ }
327+
310328type DeleteParams = {
311329 id : UserId ;
312330 organizationId : OrganizationId | null ;
0 commit comments