@@ -43,7 +43,10 @@ public String deleteEmail(User user)
4343 throws HttpClientErrorException {
4444 Boolean isDeleted = emailServerInteraction .deleteEmailId (user );
4545 if (isDeleted ){
46- userRepository .delete (user );
46+
47+ user .setIsActive (false );
48+ userRepository .save (user );
49+
4750 return "Email Id *deleted* and added to open pool." ;
4851 }
4952 return "No mail ID on the server was identified with" +
@@ -96,8 +99,10 @@ public TelegramResponse handleRequest(long chatId, String text) {
9699 );
97100
98101 case "/create" :
99- if (userRepository .findByChatId (chatId ).size () >= trashemailConfig .getMaxEmailsPerUser ()) {
100- responseText = "Only " + trashemailConfig .getMaxEmailsPerUser () + " " +
102+ if (userRepository .findByChatIdAndIsActiveTrue (chatId ).size ()
103+ >= trashemailConfig .getMaxEmailsPerUser ()) {
104+ responseText = "Only " +
105+ trashemailConfig .getMaxEmailsPerUser () + " " +
101106 "email Ids are allowed per-user\n " +
102107 "You can get the list of all the emails @ /emails" ;
103108 return new TelegramResponse (
@@ -110,7 +115,7 @@ public TelegramResponse handleRequest(long chatId, String text) {
110115 if (argument != null ) {
111116 /*
112117 User entered something like : /create username.
113- No offer him interactive response.
118+ Now offer him interactive response.
114119 */
115120 String emailRegex = "^[A-Za-z0-9._%+-]+@(" +
116121 String .join ("|" ,
@@ -122,11 +127,12 @@ public TelegramResponse handleRequest(long chatId, String text) {
122127 // A valid domain email is inputted by the user
123128 if (matcher .matches ()) {
124129 String emailId = argument ;
125- if (userRepository .existsByEmailId (emailId )){
130+ if (userRepository .existsByEmailIdAndIsActiveTrue (
131+ emailId )){
126132 // Email ID Already taken
127133 responseText = "Email ID *" + argument + "* " +
128134 "is already taken, " +
129- "please get some other" ;
135+ "please try some other email id. " ;
130136 return new TelegramResponse (
131137 chatId ,
132138 responseText
@@ -165,7 +171,19 @@ public TelegramResponse handleRequest(long chatId, String text) {
165171 }
166172
167173 if (response !=null ) {
168- userRepository .save (user );
174+ // Check if the same user has taken the same
175+ // email before, then it would exist in DB so
176+ // just set isActive to true.
177+ User existingUser =
178+ userRepository .findByEmailIdAndChatId (
179+ emailId ,
180+ chatId );
181+ if (existingUser == null )
182+ userRepository .save (user );
183+ else {
184+ existingUser .setIsActive (true );
185+ userRepository .save (existingUser );
186+ }
169187 responseText = "Email successfully created" +
170188 " - " +
171189 "" +
@@ -280,7 +298,8 @@ else if(argument == null) {
280298 case "/emails" :
281299 String response = "Currently, you have below mentioned " +
282300 "emails with you.\n *" ;
283- List <User > allEmailsWithUser = userRepository .findByChatId (
301+ List <User > allEmailsWithUser =
302+ userRepository .findByChatIdAndIsActiveTrue (
284303 chatId );
285304
286305 for (User emailWithUser : allEmailsWithUser ){
@@ -297,7 +316,8 @@ else if(argument == null) {
297316 if (argument == null ){
298317 responseText = "Pick an email to delete." ;
299318
300- List <User > emailsWithUser = userRepository .findByChatId (
319+ List <User > emailsWithUser =
320+ userRepository .findByChatIdAndIsActiveTrue (
301321 chatId );
302322
303323 int buttonPerRow = 1 ;
@@ -340,9 +360,10 @@ else if(argument == null) {
340360 // A valid domain email is inputted by the user
341361 if (matcher .matches ()) {
342362 String emailId = argument ;
343- User user = userRepository .findByEmailId (emailId );
363+ User user = userRepository .findByEmailIdAndIsActiveTrue (
364+ emailId );
344365 // user should only delete email owned by user.
345- if (userRepository .existsByEmailId (emailId )) {
366+ if (userRepository .existsByEmailIdAndIsActiveTrue (emailId )) {
346367 if (((Long ) chatId ).equals (user .getChatId ())) {
347368 responseText = this .deleteEmail (user );
348369 return new TelegramResponse (
0 commit comments