List <UserProfile.Index> MapIndexes(BL_UserProfile new_bL_UserProfile, UserProfile existingUserProfile, DateTimeOffset utcNow) { if (new_bL_UserProfile?.Indexes == null) { return(existingUserProfile.Indexes); } var indexes = new List <UserProfile.Index>(); if (existingUserProfile?.Indexes != null) { indexes.AddRange(existingUserProfile.Indexes); } foreach (var i in new_bL_UserProfile.Indexes) { var existingIndex = indexes?.FirstOrDefault(e => e.TypeOfIndex == i.TypeOfIndex); if (existingIndex == null) { indexes.Add(new UserProfile.Index { DTLastUpdated = utcNow, TypeOfIndex = i.TypeOfIndex, Value = i.Value }); } else { if (existingIndex.Value != i.Value) { existingIndex.DTLastUpdated = utcNow; existingIndex.Value = i.Value; } } } return(indexes); }
public async Task SetUserIndexes(QuestionnaireTemplate questionnaireTemplate, string userprofileId) { var uq = await GetUserQuestionnaire_ByUserIdAsync(userprofileId, questionnaireTemplate.TypeOfQuestionnaire, questionnaireTemplate.Id); if (!uq.IsQuestionnaireTemplateComplete) { throw new BusinessLogicException(ValidationErrorCode.ValidationFailure_UserQuestionnaire, uq.Id, $"UserQuestionnaire with Id '{uq.Id}' is not Complete so no indexes can be set for UserId '{userprofileId}'"); } if (questionnaireTemplate.QuestionnaireSets?.Count != 1) { throw new BusinessLogicException(ValidationErrorCode.ValidationFailure, questionnaireTemplate.Id, $"QuestionnaireTemplate with Id '{questionnaireTemplate.Id}' is not supported since it contains more than 1 Set of Questions, so no indexes can be set for UserId '{userprofileId}'"); } var bl_usrProReg = new BL_UserProfile { Id = userprofileId, Indexes = new List <BL_UserProfile.Index>() }; var totalWeightForOverallIndex = 0.0M; foreach (var q in questionnaireTemplate.QuestionnaireSets.First().Questions) { foreach (var i in q.IndexesImpacted) { var indexToUpdate = bl_usrProReg.Indexes.SingleOrDefault(e => e.TypeOfIndex == i.TypeOfIndex); if (indexToUpdate == null) { indexToUpdate = new BL_UserProfile.Index { TypeOfIndex = i.TypeOfIndex, Value = 0 }; bl_usrProReg.Indexes.Add(indexToUpdate); } int.TryParse(uq.Answers.SingleOrDefault(e => e.QuestionId == q.Id).Value, out var answerValue); indexToUpdate.Value += (int)(i.CalculationWeight * decimal.Parse(answerValue.ToString())); if (indexToUpdate.TypeOfIndex == IndexTypeEnum.Overall) { totalWeightForOverallIndex += i.CalculationWeight; } } } foreach (var i in bl_usrProReg.Indexes) { if (i.TypeOfIndex == IndexTypeEnum.Overall) { var newValue = i.Value / totalWeightForOverallIndex * 10; i.Value = int.Parse(Math.Round(newValue, MidpointRounding.AwayFromZero).ToString()); } } await SetUserAsync(bl_usrProReg); //TODO: AAAA: persist Indexes in userIndexHistory //await SaveIndexHistory.... }
public async Task <string> CreateInviteeUserAsync(string primaryEmail, string externalRefId, string firstName, string lastName, string invitationId, bool?hasAgreedToPrivacyPolicy, bool?hasAgreedToTandC) { var existingInvitation = await GetPartnerInvitation_byInvitationIdAsync(invitationId); if (existingInvitation == null) { throw new BusinessLogicException(ValidationErrorCode.NotFound, invitationId, $"Unable to find Invitation with Id '{invitationId}' for user with and Email: '{primaryEmail}'"); } if (existingInvitation.InvitationStatus != BL_PartnerInvitationStatusTypeEnum.Accepted) { throw new BusinessLogicException(ValidationErrorCode.ValidationFailure_PartnerInvitation, invitationId, $"Invalid Invitation Status, cannot create user for Invitation with Id '{invitationId}' and Email '{primaryEmail}'"); } if (hasAgreedToPrivacyPolicy != true) { throw new BusinessLogicException(ValidationErrorCode.ValidationFailure_UserProfile_PrivacyPolicy, invitationId, $"Privacy policy must be set to true for Invitation with Id '{invitationId}' and Email '{primaryEmail}'"); } if (hasAgreedToTandC != true) { throw new BusinessLogicException(ValidationErrorCode.ValidationFailure_UserProfile_TermsOfService, invitationId, $"TandC policy must be set to true for Invitation with Id '{invitationId}' and Email '{primaryEmail}'"); } //Get Inviter profile var inviter = await _repo.GetUserProfile_byInvitationId(invitationId); if (true == string.IsNullOrWhiteSpace(inviter.Relationship_Id)) { throw new BusinessLogicException(ValidationErrorCode.ValidationFailure_UserProfile_RelationshipId, invitationId, $"Relationship must be set for Inviter with Id '{inviter.Id}' Invitation with Id '{invitationId}' and Email '{primaryEmail}'"); } //Create new invitee user var newInviteeUser = await CreateUserAsync(primaryEmail, externalRefId, false, firstName, lastName, inviter.Relationship_Id); // Set details for the Invitee as entered by the Invitee in the web app (terms of use and privacy) and, // details from the existingInvitation (the details entered by the Inviter) such as DoB and gender and, // also firstname and last name if the invitee didn't enter them in during th ADB2C registration var bl_usrProReg = new BL_UserProfile { HasAgreedToPrivacyPolicy = hasAgreedToPrivacyPolicy, HasAgreedToTandC = hasAgreedToTandC, DateOfBirth = existingInvitation.DateOfBirth, FirstName = (string.IsNullOrEmpty(firstName) ? existingInvitation.FirstName : firstName), LastName = (string.IsNullOrEmpty(lastName) ? existingInvitation.LastName : lastName), PrimaryMobileNr = existingInvitation.MobilePhone, Gender = map.From(existingInvitation.TypeOfGender), Id = newInviteeUser.Id, }; await SetUserAsync(bl_usrProReg); //Update invitationStatus of inviter await SetPartnerInvitationStatus(invitationId, BL_PartnerInvitationStatusTypeEnum.Completed); return(newInviteeUser.Id); }
public async Task SetUserAsync(BL_UserProfile userProfile) { var bl_entity = DeepClone(userProfile); //Don't want to work with the passed in variable if (string.IsNullOrWhiteSpace(userProfile.Id)) { //new user, not yet support throw new NotImplementedException("Use CreateUserAsync instead"); } var existingUser = await _repo.GetUserProfile_byIdAsync(userProfile.Id); //Adjust data if (bl_entity.DateOfBirth != null) //Reset to only to Year, Month and Day { bl_entity.DateOfBirth = new DateTime(bl_entity.DateOfBirth.Value.Year, bl_entity.DateOfBirth.Value.Month, bl_entity.DateOfBirth.Value.Day, 0, 0, 0, DateTimeKind.Unspecified); } if (validator.CanUpdateUserprofile(existingUser, bl_entity, out var validationResult)) { //Map incoming data to existing user (need to be mapped this way to not overwrite new properties not part of incoming BL_UserProfile) var utcNow = DateTimeOffset.UtcNow; existingUser.Country_Code = bl_entity.Country_Code ?? existingUser.Country_Code; existingUser.DateOfBirth = bl_entity.DateOfBirth ?? existingUser.DateOfBirth; existingUser.DTLastUpdated = utcNow; existingUser.ExternalRefId = existingUser.ExternalRefId; //Cannot be changed existingUser.FirstName = bl_entity.FirstName ?? existingUser.FirstName; existingUser.Gender = bl_entity.Gender ?? existingUser.Gender; existingUser.HasAgreedToPrivacyPolicy = bl_entity.HasAgreedToPrivacyPolicy ?? existingUser.HasAgreedToPrivacyPolicy; existingUser.HasAgreedToRefundPolicy = bl_entity.HasAgreedToRefundPolicy ?? existingUser.HasAgreedToRefundPolicy; existingUser.HasAgreedToTandC = bl_entity.HasAgreedToTandC ?? existingUser.HasAgreedToTandC; existingUser.Id = existingUser.Id; existingUser.Indexes = MapIndexes(bl_entity, existingUser, utcNow); existingUser.IsDevTest = existingUser.IsDevTest; //Cannot be changed existingUser.LastName = bl_entity.LastName ?? existingUser.LastName; existingUser.NotificationOption = bl_entity.NotificationOption ?? existingUser.NotificationOption; existingUser.PrimaryEmail = bl_entity.PrimaryEmail ?? existingUser.PrimaryEmail; existingUser.PrimaryMobileNr = bl_entity.PrimaryMobileNr ?? existingUser.PrimaryMobileNr; existingUser.Relationship_Id = existingUser.Relationship_Id; existingUser.Type = null; //set in repo anyway, validationResult = validator.Validate(existingUser); } if (validationResult.IsValid) { await _repo.SetUserProfileAsync(existingUser); } else { throw new BusinessLogicException(validationResult); } }
public bool CanUpdateUserprofile(UserProfile existingUserprofile, BL_UserProfile bl_newUserprofile, out ValidationResult results) { results = new ValidationResult(); if (existingUserprofile == null) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.EntityIsNull, DebugMessage = @"Existing userprofile is null. Cannot update userprofile", EntityJsonRepresentation = null, EntityIdInError = bl_newUserprofile.Id, }); return(false); //no need to do any further validations } if (existingUserprofile.HasAgreedToPrivacyPolicy == true && bl_newUserprofile.HasAgreedToPrivacyPolicy == false) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.ValidationFailure_UserProfile_PrivacyPolicy, DebugMessage = $"Cannot set value from '{existingUserprofile.HasAgreedToPrivacyPolicy}' to '{bl_newUserprofile.HasAgreedToPrivacyPolicy}'", EntityJsonRepresentation = GetJson(existingUserprofile), EntityIdInError = bl_newUserprofile.Id, }); } if (existingUserprofile.HasAgreedToTandC == true && bl_newUserprofile.HasAgreedToTandC == false) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.ValidationFailure_UserProfile_PrivacyPolicy, DebugMessage = $"Cannot set value from '{existingUserprofile.HasAgreedToTandC}' to '{bl_newUserprofile.HasAgreedToTandC}'", EntityJsonRepresentation = GetJson(existingUserprofile), EntityIdInError = bl_newUserprofile.Id, }); } return(results.IsValid); }
public BL_UserProfile From(UserProfile userProfile) { if (userProfile == null) { return(null); } var r = new BL_UserProfile { Id = userProfile.Id, Country_Code = userProfile.Country_Code, DateOfBirth = userProfile.DateOfBirth, ExternalRefId = userProfile.ExternalRefId, FirstName = userProfile.FirstName, Gender = userProfile.Gender, HasAgreedToPrivacyPolicy = userProfile.HasAgreedToPrivacyPolicy, HasAgreedToRefundPolicy = userProfile.HasAgreedToRefundPolicy, HasAgreedToTandC = userProfile.HasAgreedToTandC, LastName = userProfile.LastName, NotificationOption = userProfile.NotificationOption, PrimaryEmail = userProfile.PrimaryEmail, PrimaryMobileNr = userProfile.PrimaryMobileNr, Relationship_Id = userProfile.Relationship_Id, Indexes = getIndexes(), PartnerInvitationStatus = getInvitationStatus() }; List <BL_UserProfile.Index> getIndexes() { if (userProfile.Indexes?.Count > 0) { var index = new List <BL_UserProfile.Index>(); foreach (var i in userProfile.Indexes) { index.Add(new BL_UserProfile.Index { TypeOfIndex = i.TypeOfIndex, Value = i.Value }); } return(index); } else { return(null); } } BL_PartnerInvitationStatusTypeEnum getInvitationStatus() { if (userProfile.PartnerInvitation == null) { return(BL_PartnerInvitationStatusTypeEnum.Unknown); } else { switch (userProfile.PartnerInvitation.InvitationStatus) { case PartnerInvitationStatusTypeEnum.Accepted: return(BL_PartnerInvitationStatusTypeEnum.Accepted); case PartnerInvitationStatusTypeEnum.Completed: return(BL_PartnerInvitationStatusTypeEnum.Completed); case PartnerInvitationStatusTypeEnum.Declined: return(BL_PartnerInvitationStatusTypeEnum.Declined); case PartnerInvitationStatusTypeEnum.NotApplicable: return(BL_PartnerInvitationStatusTypeEnum.NotApplicable); case PartnerInvitationStatusTypeEnum.Sent: return(BL_PartnerInvitationStatusTypeEnum.Sent); case PartnerInvitationStatusTypeEnum.Submitted: return(BL_PartnerInvitationStatusTypeEnum.Submitted); case PartnerInvitationStatusTypeEnum.Unknown: return(BL_PartnerInvitationStatusTypeEnum.Unknown); default: throw new BusinessLogicException(ValidationErrorCode.NotFound, "", $"Unable to map enum {userProfile.PartnerInvitation.InvitationStatus} to BL value"); } } } return(r); }