@@ -16,6 +16,7 @@ import { ProjectGroupRoleDto } from "../model/dto/project-group-role-dto";
1616import { environment } from "../environment/environment" ;
1717import { ResetCredentialsDto } from "../model/dto/reset-credentials-dto" ;
1818import projectgroupService from "./project-group-service" ;
19+ import { ReferralCodeDto } from "../model/dto/referral-code-dto" ;
1920
2021
2122export class UserManagementService implements IUserManagement {
@@ -103,7 +104,20 @@ export class UserManagementService implements IUserManagement {
103104 async registerUser ( user : RegisterUserDto ) : Promise < UserProfile > {
104105 let userProfile = new UserProfile ( ) ;
105106 try {
106- const result : Response = await fetch ( environment . registerUserUrl as string , {
107+ let referralCodeDetails : ReferralCodeDto | undefined = undefined ;
108+ //Check if referral code is valid
109+ if ( user . code && user . code . length > 0 ) {
110+ let referralCodeQuery = format ( 'SELECT * FROM promo_referrals WHERE UPPER(code) = %L AND is_active = true AND valid_from <= NOW() AND valid_to >= NOW() limit 1' , user . code . toUpperCase ( ) ) ;
111+
112+ const referralCodeResult = await dbClient . query ( referralCodeQuery ) ;
113+ if ( referralCodeResult . rows . length == 0 ) {
114+ throw new HttpException ( 410 , "Invalid/Expired referral code" ) ;
115+ }
116+ referralCodeDetails = ReferralCodeDto . from ( referralCodeResult . rows [ 0 ] ) ;
117+ }
118+
119+
120+ const result : Response = await fetch ( environment . registerUserUrl , {
107121 method : 'post' ,
108122 body : JSON . stringify ( user ) ,
109123 headers : { 'Content-Type' : 'application/json' }
@@ -123,12 +137,32 @@ export class UserManagementService implements IUserManagement {
123137 const data = await result . json ( ) ;
124138 userProfile = new UserProfile ( data ) ;
125139
126- //Assign user with default role and permissions
127- let queryStr = format ( `INSERT INTO user_roles (user_id, project_group_id, role_id)
128- SELECT %L, project_group_id, role_id
129- FROM roles, project_group
130- WHERE roles.name = %L AND project_group.name = %L` , userProfile . id , Role . TDEI_MEMBER , DEFAULT_PROJECT_GROUP ) ;
131- await dbClient . query ( queryStr ) ;
140+ //If referral code is valid, then assign the user to the project group associated with referral code with TDEI member role
141+ //Else assign the user to default project group with TDEI member role
142+ if ( referralCodeDetails ) {
143+ const rolesDetails = await this . getRolesByNames ( [ Role . TDEI_MEMBER ] ) ;
144+ const role_id = rolesDetails . get ( Role . TDEI_MEMBER ) ;
145+ let addRoleProjectQuery = format ( `INSERT INTO user_roles (user_id, project_group_id, role_id) VALUES %L ON CONFLICT ON CONSTRAINT unq_user_role_project_group DO NOTHING` , [ [ userProfile . id , referralCodeDetails . project_group_id , role_id ] ] ) ;
146+ await dbClient . query ( addRoleProjectQuery ) ;
147+
148+ //Update the instructions url in the user profile if exists
149+ userProfile . instructions_url = referralCodeDetails . instructions_url ;
150+ //Generate the auth token and assign refresh token to user profile
151+ const loginDto = new LoginDto ( ) ;
152+ loginDto . username = user . email ;
153+ loginDto . password = user . password ;
154+ const authResponse = await this . login ( loginDto ) ;
155+ userProfile . token = authResponse . refresh_token ;
156+
157+ }
158+ else {
159+ //Assign user with default role and permissions
160+ let queryStr = format ( `INSERT INTO user_roles (user_id, project_group_id, role_id)
161+ SELECT %L, project_group_id, role_id
162+ FROM roles, project_group
163+ WHERE roles.name = %L AND project_group.name = %L` , userProfile . id , Role . TDEI_MEMBER , DEFAULT_PROJECT_GROUP ) ;
164+ await dbClient . query ( queryStr ) ;
165+ }
132166
133167 } catch ( error : any ) {
134168 console . error ( error ) ;
0 commit comments