@@ -12,8 +12,11 @@ import Ajv, { ErrorObject } from 'ajv';
1212import { FAStatus , FusionauthService } from './fusionauth/fusionauth.service' ;
1313import { LoginResponse , UUID , User } from '@fusionauth/typescript-client' ;
1414
15+ import { ChangePasswordDTO } from './dto/changePassword.dto' ;
1516import ClientResponse from '@fusionauth/typescript-client/build/src/ClientResponse' ;
1617import { Injectable } from '@nestjs/common' ;
18+ import { OtpService } from './otp/otp.service' ;
19+ import { SMSResponseStatus } from './sms/sms.interface' ;
1720import { UserDBService } from './user-db/user-db.service' ;
1821import { v4 as uuidv4 } from 'uuid' ;
1922
@@ -39,6 +42,7 @@ export class UserService {
3942 constructor (
4043 private readonly userDBService : UserDBService ,
4144 private readonly fusionAuthService : FusionauthService ,
45+ private readonly otpService : OtpService ,
4246 ) {
4347 this . ajv = new Ajv ( { strict : false } ) ;
4448 this . ajv . addSchema ( addressSchema , 'address' ) ;
@@ -342,6 +346,89 @@ export class UserService {
342346 } ) ;
343347 }
344348
349+ async changePassword ( data : ChangePasswordDTO ) : Promise < SignupResponse > {
350+ // Verify OTP
351+ const {
352+ statusFA,
353+ userId,
354+ user,
355+ } : { statusFA : FAStatus ; userId : UUID ; user : User } =
356+ await this . fusionAuthService . getUser ( data . username ) ;
357+ const response : SignupResponse = new SignupResponse ( ) . init ( uuidv4 ( ) ) ;
358+ if ( statusFA === FAStatus . USER_EXISTS ) {
359+ const verifyOTPResult = await this . otpService . verifyOTP ( {
360+ phone : user . mobilePhone ,
361+ otp : data . OTP ,
362+ } ) ;
363+
364+ if ( verifyOTPResult . status === SMSResponseStatus . success ) {
365+ const result = await this . fusionAuthService . updatePassword (
366+ userId ,
367+ data . password ,
368+ ) ;
369+
370+ if ( result . statusFA == FAStatus . SUCCESS ) {
371+ response . result = {
372+ responseMsg : 'Password updated successfully' ,
373+ } ;
374+ response . responseCode = ResponseCode . OK ;
375+ response . params . status = ResponseStatus . success ;
376+ } else {
377+ response . responseCode = ResponseCode . FAILURE ;
378+ response . params . err = 'UNCAUGHT_EXCEPTION' ;
379+ response . params . errMsg = 'Server Error' ;
380+ response . params . status = ResponseStatus . failure ;
381+ }
382+ } else {
383+ response . responseCode = ResponseCode . FAILURE ;
384+ response . params . err = 'INVALID_OTP_USERNAME_PAIR' ;
385+ response . params . errMsg = 'OTP and Username did not match.' ;
386+ response . params . status = ResponseStatus . failure ;
387+ }
388+ } else {
389+ response . responseCode = ResponseCode . FAILURE ;
390+ response . params . err = 'INVALID_USERNAME' ;
391+ response . params . errMsg = 'No user with this Username exists' ;
392+ response . params . status = ResponseStatus . failure ;
393+ }
394+ return response ;
395+ }
396+
397+ async changePasswordOTP ( username : string ) : Promise < SignupResponse > {
398+ // Get Phone No from username
399+ const {
400+ statusFA,
401+ userId,
402+ user,
403+ } : { statusFA : FAStatus ; userId : UUID ; user : User } =
404+ await this . fusionAuthService . getUser ( username ) ;
405+ const response : SignupResponse = new SignupResponse ( ) . init ( uuidv4 ( ) ) ;
406+ // If phone number is valid => Send OTP
407+ if ( statusFA === FAStatus . USER_EXISTS ) {
408+ const re = / ^ [ 6 - 9 ] { 1 } [ 0 - 9 ] { 9 } $ / ;
409+ if ( re . test ( user . mobilePhone ) ) {
410+ const result = await this . otpService . sendOTP ( user . mobilePhone ) ;
411+ response . result = {
412+ data : result ,
413+ responseMsg : `OTP has been sent to ${ user . mobilePhone } .` ,
414+ } ;
415+ response . responseCode = ResponseCode . OK ;
416+ response . params . status = ResponseStatus . success ;
417+ } else {
418+ response . responseCode = ResponseCode . FAILURE ;
419+ response . params . err = 'INVALID_PHONE_NUMBER' ;
420+ response . params . errMsg = 'Invalid Phone number' ;
421+ response . params . status = ResponseStatus . failure ;
422+ }
423+ } else {
424+ response . responseCode = ResponseCode . FAILURE ;
425+ response . params . err = 'INVALID_USERNAME' ;
426+ response . params . errMsg = 'No user with this Username exists' ;
427+ response . params . status = ResponseStatus . failure ;
428+ }
429+ return response ;
430+ }
431+
345432 private isOldSchoolUser ( fusionAuthUser : User ) {
346433 return (
347434 fusionAuthUser . registrations [ 0 ] . roles === undefined ||
0 commit comments