-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added functionality to send otp for phone number change request --------- Co-authored-by: Roshan Piyush <piyush.roshan@gmail.com>
- Loading branch information
1 parent
9fe9ba7
commit 9a061c4
Showing
42 changed files
with
1,205 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
services/identity/src/main/java/com/crapi/controller/ChangePhoneController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.crapi.controller; | ||
|
||
import com.crapi.model.CRAPIResponse; | ||
import com.crapi.model.ChangePhoneForm; | ||
import com.crapi.service.UserService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.validation.Valid; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@CrossOrigin | ||
@RestController | ||
@RequestMapping("/identity/api") | ||
public class ChangePhoneController { | ||
@Autowired UserService userService; | ||
|
||
/** | ||
* @param changePhoneForm changePhoneForm contains old phone number and new phone number, api will | ||
* send otp to email address. | ||
* @param request getting jwt token for user from request header | ||
* @return first check phone number is already registered or not if it is there then return phone | ||
* number already registered then try with new phone number. | ||
*/ | ||
@PostMapping("/v2/user/change-phone-number") | ||
public ResponseEntity<CRAPIResponse> changesPhone( | ||
@Valid @RequestBody ChangePhoneForm changePhoneForm, HttpServletRequest request) { | ||
CRAPIResponse changePhoneResponse = userService.changePhoneRequest(request, changePhoneForm); | ||
if (changePhoneResponse != null && changePhoneResponse.getStatus() == 403) { | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(changePhoneResponse); | ||
} else if (changePhoneResponse != null && changePhoneResponse.getStatus() == 404) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(changePhoneResponse); | ||
} | ||
return ResponseEntity.status(HttpStatus.OK).body(changePhoneResponse); | ||
} | ||
|
||
/** | ||
* @param changePhoneForm changeEmailForm contains old phone number and new phone number, with | ||
* otp, this function will verify number and otp | ||
* @param request getting jwt token for user from request header | ||
* @return verify if otp is valid then it will update the user phone number | ||
*/ | ||
@PostMapping("v2/user/verify-phone-otp") | ||
public ResponseEntity<CRAPIResponse> verifyPhoneOTP( | ||
@RequestBody ChangePhoneForm changePhoneForm, HttpServletRequest request) { | ||
CRAPIResponse verifyPhoneOTPResponse = userService.verifyPhoneOTP(request, changePhoneForm); | ||
if (verifyPhoneOTPResponse != null && verifyPhoneOTPResponse.getStatus() == 200) { | ||
return ResponseEntity.status(HttpStatus.OK).body(verifyPhoneOTPResponse); | ||
} else if (verifyPhoneOTPResponse != null && verifyPhoneOTPResponse.getStatus() == 404) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(verifyPhoneOTPResponse); | ||
} | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(verifyPhoneOTPResponse); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
services/identity/src/main/java/com/crapi/entity/ChangePhoneRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.crapi.entity; | ||
|
||
import com.crapi.enums.EStatus; | ||
import jakarta.persistence.*; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Table(name = "otp_phoneNumberChange") | ||
@Data | ||
public class ChangePhoneRequest { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private long id; | ||
|
||
@Column(name = "new_phone") | ||
private String newPhone; | ||
|
||
@Column(name = "old_phone") | ||
private String oldPhone; | ||
|
||
@Column(name = "otp") | ||
private String otp; | ||
|
||
private String status; | ||
|
||
@OneToOne private User user; | ||
|
||
public ChangePhoneRequest() {} | ||
|
||
public ChangePhoneRequest(String newPhone, String oldPhone, String otp, User user) { | ||
this.newPhone = newPhone; | ||
this.oldPhone = oldPhone; | ||
this.otp = otp; | ||
this.user = user; | ||
this.status = EStatus.ACTIVE.toString(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
services/identity/src/main/java/com/crapi/model/ChangePhoneForm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.crapi.model; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ChangePhoneForm { | ||
@NotBlank | ||
@Size(max = 15) | ||
private String old_number; | ||
|
||
@NotBlank | ||
@Size(max = 15) | ||
private String new_number; | ||
|
||
@Size(min = 3, max = 4) | ||
private String otp; | ||
} |
11 changes: 11 additions & 0 deletions
11
services/identity/src/main/java/com/crapi/repository/ChangePhoneRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.crapi.repository; | ||
|
||
import com.crapi.entity.ChangePhoneRequest; | ||
import com.crapi.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ChangePhoneRepository extends JpaRepository<ChangePhoneRequest, Long> { | ||
ChangePhoneRequest findByUser(User user); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.