public async Task <CustomerProfileErrorCodes> UpdateEmailAsync(string customerId, string email, bool isEmailVerified) { using (var context = _contextFactory.CreateDataContext()) { using (var transaction = context.Database.BeginTransaction()) { var entity = await context.CustomerProfiles .IgnoreQueryFilters() .FirstOrDefaultAsync(o => o.CustomerId == customerId); if (entity == null) { return(CustomerProfileErrorCodes.CustomerProfileDoesNotExist); } var encryptedEmail = _encryptionService.EncryptValue(email); var existingEntity = await context.CustomerProfiles.FirstOrDefaultAsync(o => o.Email == encryptedEmail); if (existingEntity != null) { return(CustomerProfileErrorCodes.CustomerProfileAlreadyExists); } entity = _encryptionService.Decrypt(entity); var archiveEntity = CustomerProfileArchiveEntity.Create(entity); archiveEntity.CustomerId = $"{archiveEntity.CustomerId}_{DateTime.UtcNow:yyMMddHHmmss}"; archiveEntity = _encryptionService.Encrypt(archiveEntity); context.CustomerProfilesArchive.Add(archiveEntity); entity.Email = email; entity.LowerCasedEmail = email.ToLower(); entity.IsEmailVerified = isEmailVerified; entity = _encryptionService.Encrypt(entity); context.CustomerProfiles.Update(entity); await context.SaveChangesAsync(); transaction.Commit(); return(CustomerProfileErrorCodes.None); } } }
public async Task <bool> DeleteAsync(string customerId) { using (var context = _contextFactory.CreateDataContext()) { var entity = await context.CustomerProfiles .IgnoreQueryFilters() .FirstOrDefaultAsync(c => c.CustomerId == customerId); if (entity == null) { return(false); } using (var transaction = context.Database.BeginTransaction()) { try { var archiveEntity = CustomerProfileArchiveEntity.Create(entity); context.CustomerProfilesArchive.Add(archiveEntity); context.CustomerProfiles.Remove(entity); await context.SaveChangesAsync(); transaction.Commit(); } catch (Exception e) { _log.Error(e, "Error occured while deleting customer profile", $"customerId = {customerId}"); return(false); } } } return(true); }