private void SendPetSharingEmail(petOwner PetOwner) { CurrentUser currentUser = (CurrentUser)HttpContext.Current.User; EmailService emailService = new EmailService(); String emailBodyHtml = emailService.GetBodyHtml("PETSHARING"); if (String.IsNullOrEmpty(emailBodyHtml)) { logger.Error("Email template not found for type - EMAILVERIFICATION"); throw new CustomException("Email type not found", (int)ErrorCode.EMAILERROR); } String VerificationToken = PetOwner.userId + "|" + PetOwner.petId + "|" + PetOwner.sharedUserId + "|" + DateTime.Now.ToLongDateString(); emailBodyHtml = emailBodyHtml.Replace("{UserName}", char.ToUpper(PetOwner.user.userName[0]) + PetOwner.user.userName.Substring(1)) .Replace("{petName}", PetOwner.pet.petName) .Replace("{requestedUser}", char.ToUpper(currentUser.username[0]) + currentUser.username.Substring(1)) .Replace("{VerifyLink}", HostedBaseUrl + "pet/confirmpetsharingrequest?token=" + Encryptor.Encrypt(VerificationToken).Replace("+", "%2b")); MailMessage email = new MailMessage() { From = new MailAddress("*****@*****.**"), Subject = "Pet sharing request on Petwhizz", Body = emailBodyHtml, IsBodyHtml = true, }; email.To.Add(PetOwner.user.eMail); AlternateView view = emailService.EmbedLogosForEmailBody(email.Body); email.AlternateViews.Add(view); emailService.SendEmail(email); }
internal void ConfirmPetSharingRequest(string token) { logger.Trace("Recived Confirm Pet share request"); try { if (String.IsNullOrEmpty(token)) { logger.Error("Recived Confirm Pet share request token is empty"); throw new CustomException("Recived Confirm Pet share request token is empty", (int)ErrorCode.VALIDATIONFAILED); } var userId = Decryptor.Decrypt(token).Split('|')[0]; var petId = Decryptor.Decrypt(token).Split('|')[1]; var sharedUserId = Decryptor.Decrypt(token).Split('|')[2]; if (String.IsNullOrEmpty(userId) || String.IsNullOrEmpty(petId) || String.IsNullOrEmpty(sharedUserId)) { logger.Error("Recived Confirm Pet share request some of the properties empty"); throw new CustomException("Recived Confirm Pet share request some of the properties empty", (int)ErrorCode.VALIDATIONFAILED); } int iUserId = int.Parse(userId); Int64 ipetId = Int64.Parse(petId); int isharedUserId = int.Parse(sharedUserId); using (var ctx = new PetWhizzEntities()) { petOwner PetOwner = ctx.petOwners.Where(a => a.userId == iUserId && a.petId == ipetId && a.sharedUserId == isharedUserId).FirstOrDefault(); if (PetOwner == null) { logger.Error("No Owner request found for userId -" + userId + " & petId -" + petId + " by userId- " + sharedUserId); throw new CustomException("Recived Confirm Pet share request some of the properties empty", (int)ErrorCode.NORECORDFOUND); } ctx.petOwners.Attach(PetOwner); PetOwner.isActive = true; PetOwner.acceptedTime = DateTime.Now; PetOwner.isMainOwner = false; ctx.SaveChanges(); logger.Trace("Confirm Pet share request completed successfully"); } } catch (CustomException) { throw; } catch (Exception ex) { logger.Error(MethodBase.GetCurrentMethod().Name + ": exception: " + ex.Message + ", " + ex.InnerException); throw new CustomException("SystemError", ex, (int)ErrorCode.PROCEESINGERROR); } }
internal void PetEnrollment(PetEnrollmentRequest petEnrollmentRequest) { logger.Trace("Recived pet enroll request"); try { CurrentUser currentUser = (CurrentUser)HttpContext.Current.User; if (String.IsNullOrEmpty(petEnrollmentRequest.petName) || petEnrollmentRequest.breedId == 0) { logger.Error("Required fiedls not found on pet emrollment reuest"); throw new CustomException("Breed or Pet Name is empty", (int)ErrorCode.VALIDATIONFAILED); } //creating pet object pet Pet = new pet() { profileImage = petEnrollmentRequest.profileImage, coverImage = petEnrollmentRequest.coverImage, sex = petEnrollmentRequest.sex, birthDay = petEnrollmentRequest.birthDay, breedId = petEnrollmentRequest.breedId, entryDate = DateTime.Now, isActive = true, isDeleted = false, petName = petEnrollmentRequest.petName }; using (var ctx = new PetWhizzEntities()) { using (var dbContextTransaction = ctx.Database.BeginTransaction()) { try { ctx.pets.Add(Pet); ctx.SaveChanges(); petOwner PetOwner = new petOwner() { enteryDate = DateTime.Now, petId = Pet.id, userId = currentUser.userId, isActive = true, isMainOwner = true }; ctx.petOwners.Add(PetOwner); ctx.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); logger.Error("DB error occure when enrolling Pet"); throw new CustomException("Pet Enrolling failed", (int)ErrorCode.PROCEESINGERROR); } } } } catch (CustomException) { throw; } catch (Exception ex) { logger.Error(MethodBase.GetCurrentMethod().Name + ": exception: " + ex.Message + ", " + ex.InnerException); throw new CustomException("SystemError", ex, (int)ErrorCode.PROCEESINGERROR); } }
internal void SharePet(PetShareRequest petShareRequest) { logger.Trace("Recived Pet share request"); try { if (String.IsNullOrEmpty(petShareRequest.username) || String.IsNullOrEmpty(petShareRequest.email) || petShareRequest.petId <= 0) { logger.Error("Recived Pet share request"); throw new CustomException("Pet share request dosent have required fields", (int)ErrorCode.VALIDATIONFAILED); } CurrentUser currentUser = (CurrentUser)HttpContext.Current.User; using (var ctx = new PetWhizzEntities()) { user User = ctx.users.Where(a => a.eMail.ToLower() == petShareRequest.email.ToLower()).FirstOrDefault(); if (User == null) { logger.Error("Requested user details not matching for Petwhizz user email - " + petShareRequest.email); throw new CustomException("Requested user details not matching for Petwhizz user", (int)ErrorCode.USERNOTFOUND); } if (User.userName.ToLower() != petShareRequest.username.ToLower()) { logger.Error("Requested user details not matching for Petwhizz user username - " + petShareRequest.username); throw new CustomException("Requested user details not matching for Petwhizz user username", (int)ErrorCode.VALIDATIONFAILED); } pet Pet = ctx.pets.Where(a => a.id == petShareRequest.petId).FirstOrDefault(); if (Pet.petOwners.Where(a => a.userId == currentUser.userId && a.isActive == true && a.isMainOwner == true).FirstOrDefault() == null) { logger.Error("Requested user not belongs to the pet or not the main owner"); throw new CustomException("Requested user not belongs to the pet or not the main owner", (int)ErrorCode.UNAUTHORIZED); } petOwner PetOwner = ctx.petOwners.Where(a => a.petId == petShareRequest.petId && a.userId == User.id).FirstOrDefault(); if (PetOwner != null && PetOwner.isActive == true) { logger.Error("Pet is already shared with requestd user"); throw new CustomException("Pet is already shared with requestd user", (int)ErrorCode.ALREADYEXIST); } else if (PetOwner != null && PetOwner.isActive == false) { logger.Error("Pet is already shared with this user but not yet confirmed."); throw new CustomException("Pet is already shared with this user but not yet confirmed.", (int)ErrorCode.NOTCONFIRMED); } else { //share pet PetOwner = new petOwner() { enteryDate = DateTime.Now, isActive = false, isMainOwner = false, petId = petShareRequest.petId, sharedTime = DateTime.Now, sharedUserId = currentUser.userId, acceptedTime = null, userId = User.id }; ctx.petOwners.Add(PetOwner); ctx.SaveChanges(); // var EmailService = new EmailService(); SendPetSharingEmail(PetOwner); } } } catch (CustomException) { throw; } catch (Exception ex) { logger.Error(MethodBase.GetCurrentMethod().Name + ": exception: " + ex.Message + ", " + ex.InnerException); throw new CustomException("SystemError", ex, (int)ErrorCode.PROCEESINGERROR); } }