private void SaveNewMember(responseUser memberData, VkGroup @group) { Member member = this.memberMapper.CreateMember(memberData, @group); this.memberMapper.UpdateMemberFields(member, memberData); this.memberRepository.SaveMember(member); }
private bool UpdateMemberInterests(Member member, responseUser memberData) { const string KeyTemplate = "{0}-{1}"; bool changeDetected = false; IDictionary <string, MemberInterest> newInterests = this.GetNewInterests(member, memberData).ToDictionary(x => string.Format(KeyTemplate, x.Title.ToLower(), x.Type)); IDictionary <string, MemberInterest> currentInterests = member.Interests.Distinct(new MemberInterestEqualityComparer()).ToDictionary(x => string.Format(KeyTemplate, x.Title.ToLower(), x.Type)); foreach (var currentInterest in currentInterests) { if (!newInterests.ContainsKey(currentInterest.Key)) { member.Interests.Remove(currentInterest.Value); changeDetected = true; } } foreach (var newInterest in newInterests) { if (!currentInterests.ContainsKey(newInterest.Key)) { member.Interests.Add(newInterest.Value); changeDetected = true; } } return(changeDetected); }
public MemberChangeContext UpdateMemberFields(Member member, responseUser memberData) { if (memberData == null) { return(null); } MemberChangeContext changeContext = new MemberChangeContext(); Member newInstance = new Member() { VkGroupId = member.VkGroupId, VkMemberId = member.VkMemberId }; newInstance.Name = this.GetMemberName(memberData); newInstance.Gender = this.GetGender(memberData.sex); this.AssignBirthDate(newInstance, memberData.bdate); newInstance.CityId = string.IsNullOrWhiteSpace(memberData.city) ? 0 : int.Parse(memberData.city); newInstance.CountryId = string.IsNullOrWhiteSpace(memberData.country) ? 0 : int.Parse(memberData.country); newInstance.Education = this.GetMemberEducation(memberData); newInstance.MaritalStatus = this.GetMaritalStatus(memberData.relation); newInstance.Status = this.GetMemberStatus(memberData); changeContext.IsMemberChanged = this.UpdateMemberInfo(member, newInstance); changeContext.IsMemberInterestsChanged = this.UpdateMemberInterests(member, memberData); return(changeContext); }
private string GetMemberName(responseUser memberData) { int partsCount = 0; SeparatedStringBuilder name = new SeparatedStringBuilder(" "); if (!string.IsNullOrWhiteSpace(memberData.first_name)) { name.AppendWithSeparator(memberData.first_name.Trim()); partsCount++; } if (!string.IsNullOrWhiteSpace(memberData.last_name)) { name.AppendWithSeparator(memberData.last_name.Trim()); partsCount++; } if (partsCount == 2) { return(name.ToString()); } if (!string.IsNullOrWhiteSpace(memberData.screen_name)) { return(memberData.screen_name.Trim()); } return(name.ToString()); }
private void UpdateExistingMember(Member savedMember, responseUser memberData) { this.log.DebugFormat("Member with VkId={0} is already in database", memberData.uid); MemberChangeContext context = this.memberMapper.UpdateMemberFields(savedMember, memberData); savedMember.IsDeleted = false; this.memberRepository.UpdateMember(savedMember, context); }
public Member CreateMember(responseUser memberData, VkGroup group) { Member member = new Member() { IsDeleted = false, VkGroupId = group.Id, VkMemberId = long.Parse(memberData.uid), }; return(member); }
private void ProcessUser(responseUser memberData, VkGroup group) { var savedMember = this.memberRepository.GetMember(group.Id, memberData.uid); if (savedMember != null) { this.UpdateExistingMember(savedMember, memberData); } else { this.SaveNewMember(memberData, group); } }
private IEnumerable <MemberInterest> GetNewInterests(Member member, responseUser memberData) { List <MemberInterest> currentInterests = new List <MemberInterest>(); currentInterests.AddRange(this.GetInterestCollection(memberData.interests, MemberInterestType.General)); currentInterests.AddRange(this.GetInterestCollection(memberData.movies, MemberInterestType.Movie)); currentInterests.AddRange(this.GetInterestCollection(memberData.tv, MemberInterestType.TVShow)); currentInterests.AddRange(this.GetInterestCollection(memberData.books, MemberInterestType.Book)); currentInterests.AddRange(this.GetInterestCollection(memberData.games, MemberInterestType.VideoGame)); currentInterests.ForEach(i => { i.VkMemberId = member.VkMemberId; i.VkGroupId = member.VkGroupId; }); return(currentInterests); }
private MemberStatus GetMemberStatus(responseUser memberData) { if (string.Compare(memberData.photo_rec, "http://vk.com/images/deactivated_c.gif", StringComparison.InvariantCultureIgnoreCase) == 0) { if (string.Compare(memberData.first_name, "no", StringComparison.InvariantCultureIgnoreCase) == 0 && string.Compare(memberData.last_name, "name", StringComparison.InvariantCultureIgnoreCase) == 0) { return(MemberStatus.Deleted); } return(MemberStatus.Blocked); } return(MemberStatus.Active); }
public static async Task <IActionResult> assignDirectReportToManager( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "Managers/{newManagerUserID}")] HttpRequest req, string newManagerUserID, ILogger log) { var token = await Authenticate.getTokenOnBehalf(req.Headers["Authorization"], log); if (!token.isAuthenticated) { return(new UnauthorizedResult()); } responseUser DirectReport = JsonConvert.DeserializeObject <responseUser>(new StreamReader(req.Body).ReadToEnd()); if (!security.isManager(token.onBehalfToken, log)) { return(new UnauthorizedResult()); } if (!security.isManager(token.onBehalfToken, newManagerUserID, log)) { return(new NotFoundObjectResult(new JObject(new JProperty("error", "Failed when looking for manager with ID: " + newManagerUserID)))); } if (!security.isDirectReport(token.onBehalfToken, DirectReport.Id)) { log.LogError("could not find directReport with id " + DirectReport.Id); return(new NotFoundObjectResult(new JObject(new JProperty("error", "could not find directReport with id " + DirectReport.Id)))); } var requestContent = new JObject(new JProperty("@odata.id", "https://graph.microsoft.com/v1.0/users/" + newManagerUserID)); var request = new HttpRequestMessage(HttpMethod.Put, "/v1.0/users/" + DirectReport.Id + "/manager/$ref"); request.Headers.Add("Authorization", "Bearer " + token.onBehalfToken); request.Content = new StringContent(requestContent.ToString(), Encoding.UTF8, "application/json"); HttpResponseMessage response = await graphController.Client.SendAsync(request); if (!response.IsSuccessStatusCode) { var content = new ContentResult(); content.StatusCode = (int)response.StatusCode; content.Content = await response.Content.ReadAsStringAsync(); return(content); } return(new OkObjectResult(new JObject(new JProperty("id", newManagerUserID)))); }
private EducationLevel GetMemberEducation(responseUser memberData) { if (!string.IsNullOrWhiteSpace(memberData.university) && memberData.university != "0") { int graduationYear; if (int.TryParse(memberData.graduation, out graduationYear)) { return(this.dateTimeHelper.GetDateTimeNow().Year >= graduationYear ? EducationLevel.Higher : EducationLevel.UncompletedHigher); } return(EducationLevel.UncompletedHigher); } if (memberData.schools != null && memberData.schools.Length > 0) { return(EducationLevel.Middle); } return(EducationLevel.Unknown); }
public static async Task <IActionResult> getDirectReport( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "DirectReports/{DirectReportUserID}")] HttpRequest req, string DirectReportUserID, ILogger log) { var token = await Authenticate.getTokenOnBehalf(req.Headers["Authorization"], log); if (!token.isAuthenticated) { return(new UnauthorizedResult()); } string managerUserID = token.GetUserID(); if (!security.isManager(token.onBehalfToken, log)) { return(new UnauthorizedResult()); } var request = new HttpRequestMessage(HttpMethod.Get, "/v1.0/users/" + managerUserID + "/DirectReports/" + DirectReportUserID + "?" + props.userProperties); request.Headers.Add("Authorization", "Bearer " + token.onBehalfToken); HttpResponseMessage response = await graphController.Client.SendAsync(request); if (!response.IsSuccessStatusCode) { var content = new ContentResult(); content.StatusCode = (int)response.StatusCode; content.Content = await response.Content.ReadAsStringAsync(); return(content); } responseUser directReport = JsonConvert.DeserializeObject <responseUser>(await response.Content.ReadAsStringAsync()); return(new OkObjectResult(directReport)); }
public static async Task <IActionResult> createDirectReport( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "DirectReports")] HttpRequest req, ILogger log) { var token = await Authenticate.getTokenOnBehalf(req.Headers["Authorization"], log); if (!token.isAuthenticated) { return(new UnauthorizedResult()); } string managerUserID = token.GetUserID(); if (!security.isManager(token.onBehalfToken, log)) { return(new UnauthorizedResult()); } responseUser NewDirectReport; try { NewDirectReport = JsonConvert.DeserializeObject <responseUser>(new StreamReader(req.Body).ReadToEnd(), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }); } catch (Exception ex) { return(new BadRequestObjectResult(new JObject(new JProperty("error", ex.Message)))); } NewDirectReport.passwordProfile = new PasswordProfile { ForceChangePasswordNextSignIn = props.ForceChangePasswordNextSignIn, ForceChangePasswordNextSignInWithMfa = props.ForceChangePasswordNextSignInWithMfa, Password = security.generatePassword() }; string JsonNewDirectReport = JsonConvert.SerializeObject(NewDirectReport, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var request = new HttpRequestMessage(HttpMethod.Post, "/v1.0/users/"); request.Headers.Add("Authorization", "Bearer " + token.onBehalfToken); request.Content = new StringContent(JsonNewDirectReport, Encoding.UTF8, "application/json"); HttpResponseMessage response = await graphController.Client.SendAsync(request); if (!response.IsSuccessStatusCode) { return(new BadRequestObjectResult(await response.Content.ReadAsStringAsync())); } //----------------------------------------------------------------------------------------------------------------------// // When a user is created, we also need to bind it to the manager that creates the user. responseUser DirectReport = JsonConvert.DeserializeObject <responseUser>(response.Content.ReadAsStringAsync().Result); var requestContent = new JObject(new JProperty("@odata.id", "https://graph.microsoft.com/v1.0/me/")); request = new HttpRequestMessage(HttpMethod.Put, "/v1.0/users/" + DirectReport.Id + "/manager/$ref"); request.Headers.Add("Authorization", "Bearer " + token.onBehalfToken); request.Content = new StringContent(requestContent.ToString(), Encoding.UTF8, "application/json"); response = graphController.Client.SendAsync(request).Result; if (!response.IsSuccessStatusCode) { return(new BadRequestObjectResult(response.Content.ReadAsStringAsync().Result)); } //----------------------------------------------------------------------------------------------------------------------// return(new OkObjectResult(DirectReport)); }