public async Task <JsonResult> AddNewMemberToTeam([Required] TeamMemberViewModel model) { if (!ModelState.IsValid) { return(Json(new InvalidParametersResultModel().AttachModelState(ModelState))); } return(await JsonAsync(_teamService.AddNewMemberToTeamAsync(model))); }
/// <summary> /// Set owner for lead /// </summary> /// <param name="userId"></param> /// <param name="leadId"></param> /// <param name="listMembersId"></param> /// <returns></returns> public virtual async Task <ResultModel <Guid> > SetLeadOwnerAsync(Guid?userId, Guid?leadId, IEnumerable <Guid> listMembersId) { if (userId == null || leadId == null) { return(new InvalidParametersResultModel <Guid>()); } var leadRequest = await FindLeadByIdAsync(leadId); if (!leadRequest.IsSuccess) { return new ResultModel <Guid> { IsSuccess = leadRequest.IsSuccess, Errors = leadRequest.Errors } } ; var listUsersIdAssignedToLead = listMembersId.ToList(); listUsersIdAssignedToLead.Add(userId.Value); var lead = leadRequest.Result; if (lead.HasTeam()) { var currentOwner = lead.Team.TeamMembers .FirstOrDefault(x => x.TeamRoleId.Equals(TeamResources.Owner)); if (currentOwner != null) { await _teamService.DeleteMemberToTeamAsync(currentOwner.Id); } var members = lead.Team.TeamMembers.Where(x => x.TeamRoleId.Equals(TeamResources.Member)).ToList(); if (members.Any()) { foreach (var member in members) { await _teamService.DeleteMemberToTeamAsync(member.Id); } } var addResult = await _teamService.AddNewMemberToTeamAsync(new TeamMemberViewModel { TeamRoleId = TeamResources.Owner, TeamId = lead.TeamId.GetValueOrDefault(), UserId = userId.Value }); if (listMembersId == null || !listMembersId.Any()) { return(addResult); } foreach (var memberId in listMembersId) { await _teamService.AddNewMemberToTeamAsync(new TeamMemberViewModel { TeamRoleId = TeamResources.Member, TeamId = lead.TeamId.GetValueOrDefault(), UserId = memberId }); } if (addResult.IsSuccess) { var notification = new Notification { Content = $"Update lead {lead?.Name} ", Subject = "Info", NotificationTypeId = NotificationType.Info }; await _notify.SendNotificationAsync(listUsersIdAssignedToLead, notification); await _notify.SendNotificationToSystemAdminsAsync(notification); } return(addResult); } var addTeamRequest = await _teamService.AddTeamAsync(new AddTeamViewModel { Name = $"{lead.Name} Team" }); if (!addTeamRequest.IsSuccess) { return(addTeamRequest.Map <Guid>()); } var teamId = (Guid)addTeamRequest.Result; var mapTeamToLead = await SetTeamForLeadAsync(lead, teamId); if (!mapTeamToLead.IsSuccess) { //remove team return(mapTeamToLead.Map <Guid>()); } var addMemberResult = await _teamService.AddNewMemberToTeamAsync(new TeamMemberViewModel { TeamRoleId = TeamResources.Owner, TeamId = teamId, UserId = userId.Value }); if (listMembersId == null || !listMembersId.Any()) { return(!addMemberResult.IsSuccess ? addMemberResult.Map <Guid>() : addMemberResult.Map(addMemberResult.Result)); } foreach (var memberId in listMembersId) { await _teamService.AddNewMemberToTeamAsync(new TeamMemberViewModel { TeamRoleId = TeamResources.Member, TeamId = lead.TeamId.GetValueOrDefault(), UserId = memberId }); } if (addMemberResult.IsSuccess) { var notification = new Notification { Content = $"Add lead {lead?.Name} ", Subject = "Info", NotificationTypeId = NotificationType.Info }; await _notify.SendNotificationAsync(listUsersIdAssignedToLead, notification); await _notify.SendNotificationToSystemAdminsAsync(notification); } return(!addMemberResult.IsSuccess ? addMemberResult.Map <Guid>() : addMemberResult.Map(addMemberResult.Result)); }