Skip to content

Commit

Permalink
Update the way the deletion is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
rosehgal committed Jun 28, 2020
1 parent 21a8c56 commit e9033ed
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

@Repository
public interface UserRepository extends CrudRepository<User, Integer>{
public List<User> findByChatId(long chatId);
public User findByEmailId(String emailId);
public boolean existsByEmailId(String emailId);
public List<User> findByChatIdAndIsActiveTrue(long chatId);
public User findByEmailIdAndIsActiveTrue(String emailId);
public User findByEmailIdAndChatId(String emailId , long chatId);
public boolean existsByEmailIdAndIsActiveTrue(String emailId);
public void delete(User user);

public long count();

public List<User> findByEmailIdEndsWith(String domain);

@Query(value = "select max(u.id) from User u")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public String deleteEmail(User user)
throws HttpClientErrorException{
Boolean isDeleted = emailServerInteraction.deleteEmailId(user);
if(isDeleted){
userRepository.delete(user);

user.setIsActive(false);
userRepository.save(user);

return "Email Id *deleted* and added to open pool.";
}
return "No mail ID on the server was identified with" +
Expand Down Expand Up @@ -96,8 +99,10 @@ public TelegramResponse handleRequest(long chatId, String text) {
);

case "/create":
if(userRepository.findByChatId(chatId).size() >= trashemailConfig.getMaxEmailsPerUser()) {
responseText = "Only " + trashemailConfig.getMaxEmailsPerUser() + " " +
if(userRepository.findByChatIdAndIsActiveTrue(chatId).size()
>= trashemailConfig.getMaxEmailsPerUser()) {
responseText = "Only " +
trashemailConfig.getMaxEmailsPerUser() + " " +
"email Ids are allowed per-user\n" +
"You can get the list of all the emails @ /emails";
return new TelegramResponse(
Expand All @@ -110,7 +115,7 @@ public TelegramResponse handleRequest(long chatId, String text) {
if(argument != null) {
/*
User entered something like : /create username.
No offer him interactive response.
Now offer him interactive response.
*/
String emailRegex = "^[A-Za-z0-9._%+-]+@(" +
String.join("|",
Expand All @@ -122,11 +127,12 @@ public TelegramResponse handleRequest(long chatId, String text) {
// A valid domain email is inputted by the user
if (matcher.matches()) {
String emailId = argument;
if(userRepository.existsByEmailId(emailId)){
if(userRepository.existsByEmailIdAndIsActiveTrue(
emailId)){
// Email ID Already taken
responseText = "Email ID *" + argument + "* " +
"is already taken, " +
"please get some other";
"please try some other email id.";
return new TelegramResponse(
chatId,
responseText
Expand Down Expand Up @@ -165,7 +171,19 @@ public TelegramResponse handleRequest(long chatId, String text) {
}

if(response!=null) {
userRepository.save(user);
// Check if the same user has taken the same
// email before, then it would exist in DB so
// just set isActive to true.
User existingUser =
userRepository.findByEmailIdAndChatId(
emailId,
chatId);
if(existingUser == null)
userRepository.save(user);
else{
existingUser.setIsActive(true);
userRepository.save(existingUser);
}
responseText = "Email successfully created" +
" - " +
"" +
Expand Down Expand Up @@ -280,7 +298,8 @@ else if(argument == null) {
case "/emails":
String response = "Currently, you have below mentioned " +
"emails with you.\n*";
List<User> allEmailsWithUser = userRepository.findByChatId(
List<User> allEmailsWithUser =
userRepository.findByChatIdAndIsActiveTrue(
chatId);

for(User emailWithUser: allEmailsWithUser){
Expand All @@ -297,7 +316,8 @@ else if(argument == null) {
if(argument == null){
responseText = "Pick an email to delete.";

List<User> emailsWithUser = userRepository.findByChatId(
List<User> emailsWithUser =
userRepository.findByChatIdAndIsActiveTrue(
chatId);

int buttonPerRow = 1;
Expand Down Expand Up @@ -340,9 +360,10 @@ else if(argument == null) {
// A valid domain email is inputted by the user
if (matcher.matches()) {
String emailId = argument;
User user = userRepository.findByEmailId(emailId);
User user = userRepository.findByEmailIdAndIsActiveTrue(
emailId);
// user should only delete email owned by user.
if (userRepository.existsByEmailId(emailId)) {
if (userRepository.existsByEmailIdAndIsActiveTrue(emailId)) {
if (((Long) chatId).equals(user.getChatId())) {
responseText = this.deleteEmail(user);
return new TelegramResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public TelegramResponse messageHandler(
@GetMapping(value = "/getChatId")
public User getChatIdFortargetEmailAddress(@RequestParam String emailId){

User user = userRepository.findByEmailId(emailId);
User user = userRepository.findByEmailIdAndIsActiveTrue(emailId);
emailCounterRepository.updateCount();

return user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Date;

Expand All @@ -25,18 +21,25 @@ public class User {
private Integer id;
private long chatId;

@Column(unique = true)
private String emailId;

private String forwardsTo;

@CreationTimestamp
private LocalDateTime createDateTime;

public User(long chatId, String emailId, String forwardsTo) {
private Boolean isActive;

public User(long chatId, String emailId, String forwardsTo,
Boolean isActive) {
this.chatId = chatId;
this.emailId = emailId;
this.forwardsTo = forwardsTo;
this.isActive = isActive;
}

public User(long chatId, String emailId, String forwardsTo) {
this(chatId, emailId, forwardsTo, Boolean.TRUE);
}

@Override
Expand Down

0 comments on commit e9033ed

Please sign in to comment.