public async Task ChangeTeamOwner(CreateTeamMemberRequest req) { long?newOwnerMemberId = await FindTeamMemberIdByEmailAddress(req.TeamId, req.EmailAddress); if (newOwnerMemberId == null) { req.IsNonPlayer = true; req.IsManager = true; newOwnerMemberId = await CreateAndInviteTeamMember(req); } var changeOwnerCommand = new { team_id = req.TeamId, member_id = newOwnerMemberId }; HttpResponseMessage resp = await _httpClient.PostAsJsonAsync("teams/change_owner", changeOwnerCommand); if (!resp.IsSuccessStatusCode) { throw new HttpRequestException(JsonConvert.DeserializeObject <ReadDocument>(await resp.Content.ReadAsStringAsync()).Collection.Error.Message); } }
public async Task <long> CreateAndInviteTeamMember(CreateTeamMemberRequest req) { long?existingId = await FindTeamMemberIdByEmailAddress(req.TeamId, req.EmailAddress); if (existingId.HasValue) { return(existingId.Value); } WriteDocument doc = new WriteDocument { Template = new Template() }; doc.Template.Data.Add(new Data() { Name = "first_name", Value = req.FirstName }); doc.Template.Data.Add(new Data() { Name = "last_name", Value = req.LastName }); doc.Template.Data.Add(new Data() { Name = "team_id", Value = req.TeamId }); if (req.IsManager) { doc.Template.Data.Add(new Data() { Name = "is_manager", Value = true }); } if (req.IsNonPlayer) { doc.Template.Data.Add(new Data() { Name = "is_non_player", Value = true }); } HttpResponseMessage resp = await _httpClient.PostAsJsonAsync("/members", doc); string str = await resp.Content.ReadAsStringAsync(); ReadDocument rDoc = JsonConvert.DeserializeObject <ReadDocument>(str); long member_id = 0; if (resp.IsSuccessStatusCode) { member_id = rDoc.Collection.Items.Select(x => long.Parse(x.Data.GetDataByName("id").Value.ToString())).First(); if (!string.IsNullOrWhiteSpace(req.EmailAddress)) { WriteDocument eDoc = new WriteDocument { Template = new Template() }; eDoc.Template.Data.Add(new Data() { Name = "member_id", Value = member_id }); eDoc.Template.Data.Add(new Data() { Name = "email", Value = req.EmailAddress }); eDoc.Template.Data.Add(new Data() { Name = "receives_team_emails", Value = true }); eDoc.Template.Data.Add(new Data() { Name = "is_hidden", Value = true }); HttpResponseMessage eResp = await _httpClient.PostAsJsonAsync("/member_email_addresses", eDoc); string eStr = await resp.Content.ReadAsStringAsync(); ReadDocument erDoc = JsonConvert.DeserializeObject <ReadDocument>(eStr); if (eResp.IsSuccessStatusCode) { long emailId = erDoc.Collection.Items.Select(x => long.Parse(x.Data.GetDataByName("id").Value.ToString())).First(); long teamOwnerId = await GetTeamOwner(req.TeamId); var inviteCommand = new { team_id = req.TeamId, member_id, introduction = req.InvitationMessage, notify_as_member_id = teamOwnerId }; HttpResponseMessage eEviteResp = await _httpClient.PostAsJsonAsync("teams/invite", inviteCommand); ReadDocument evrDoc = JsonConvert.DeserializeObject <ReadDocument>(await resp.Content.ReadAsStringAsync()); eEviteResp.EnsureSuccessStatusCode(); return(member_id); } else { throw new HttpRequestException(erDoc.Collection.Error.Message); } } else { throw new HttpRequestException(rDoc.Collection.Error.Message); } } else { return(member_id); } }