public async Task <OpportunityViewModel> MapToModelAsync(Opportunity entity, OpportunityViewModel viewModel, string requestId = "") { try { //Granular bug fix : Start //Temp fix for checklist process update //Overriding granular access while getting exsiting opportunity model from sharepoint var overrideAccess = _authorizationService.GetGranularAccessOverride(); //Granular bug fix : End //Granular Access : Start var permissionsNeeded = new List <ApplicationCore.Entities.Permission>(); List <string> list = new List <string>(); var access = true; //going for super access list.AddRange(new List <string> { Access.Opportunities_Read_All.ToString(), Access.Opportunities_ReadWrite_All.ToString() }); permissionsNeeded = (await _permissionRepository.GetAllAsync(requestId)).ToList().Where(x => list.Any(x.Name.Contains)).ToList(); if (!(StatusCodes.Status200OK == await _authorizationService.CheckAccessAsync(permissionsNeeded, requestId))) { //going for opportunity access access = await _authorizationService.CheckAccessInOpportunityAsync(entity, PermissionNeededTo.Read, requestId); if (!access) { access = await _authorizationService.CheckAccessInOpportunityAsync(entity, PermissionNeededTo.ReadPartial, requestId); //going for partial accesss if (access) { list.Clear(); list.AddRange(new List <string> { "proposaldocument_read", "proposaldocument_readwrite" }); permissionsNeeded = (await _permissionRepository.GetAllAsync(requestId)).ToList().Where(x => list.Any(x.Name.Contains)).ToList(); access = StatusCodes.Status200OK == await _authorizationService.CheckAccessAsync(permissionsNeeded, requestId) ? true : false; } else { access = false; } } } if (access || overrideAccess) { //Granular Access : End viewModel.ProposalDocument = new ProposalDocumentModel(); viewModel.ProposalDocument.Id = entity.Content.ProposalDocument.Id; viewModel.ProposalDocument.DisplayName = entity.Content.ProposalDocument.DisplayName; viewModel.ProposalDocument.Reference = entity.Content.ProposalDocument.Reference; viewModel.ProposalDocument.DocumentUri = entity.Content.ProposalDocument.Metadata.DocumentUri; viewModel.ProposalDocument.Category = new CategoryModel(); viewModel.ProposalDocument.Category.Id = entity.Content.ProposalDocument.Metadata.Category.Id; viewModel.ProposalDocument.Category.Name = entity.Content.ProposalDocument.Metadata.Category.Name; viewModel.ProposalDocument.Content = new ProposalDocumentContentModel(); viewModel.ProposalDocument.Content.ProposalSectionList = new List <DocumentSectionModel>(); viewModel.ProposalDocument.Notes = new List <NoteModel>(); viewModel.ProposalDocument.Tags = entity.Content.ProposalDocument.Metadata.Tags; viewModel.ProposalDocument.Version = entity.Content.ProposalDocument.Version; // ProposalDocument Notes foreach (var item in entity.Content.ProposalDocument.Metadata.Notes.ToList()) { var docNote = new NoteModel(); docNote.Id = item.Id; docNote.CreatedDateTime = item.CreatedDateTime; docNote.NoteBody = item.NoteBody; docNote.CreatedBy = new UserProfileViewModel { Id = item.CreatedBy.Id, DisplayName = item.CreatedBy.DisplayName, Mail = item.CreatedBy.Fields.Mail, UserPrincipalName = item.CreatedBy.Fields.UserPrincipalName, UserRoles = await _userProfileHelpers.RolesToViewModelAsync(item.CreatedBy.Fields.UserRoles, requestId) }; viewModel.ProposalDocument.Notes.Add(docNote); } // ProposalDocument ProposalSectionList foreach (var item in entity.Content.ProposalDocument.Content.ProposalSectionList.ToList()) { if (!String.IsNullOrEmpty(item.Id)) { var docSectionModel = new DocumentSectionModel(); docSectionModel.Id = item.Id; docSectionModel.DisplayName = item.DisplayName; docSectionModel.LastModifiedDateTime = item.LastModifiedDateTime; docSectionModel.Owner = new UserProfileViewModel(); if (item.Owner != null) { docSectionModel.Owner.Id = item.Owner.Id ?? String.Empty; docSectionModel.Owner.DisplayName = item.Owner.DisplayName ?? String.Empty; if (item.Owner.Fields != null) { docSectionModel.Owner.Mail = item.Owner.Fields.Mail ?? String.Empty; docSectionModel.Owner.UserPrincipalName = item.Owner.Fields.UserPrincipalName ?? String.Empty; docSectionModel.Owner.UserRoles = new List <RoleModel>(); if (item.Owner.Fields.UserRoles != null) { docSectionModel.Owner.UserRoles = await _userProfileHelpers.RolesToViewModelAsync(item.Owner.Fields.UserRoles, requestId); } } else { docSectionModel.Owner.Mail = String.Empty; docSectionModel.Owner.UserPrincipalName = String.Empty; docSectionModel.Owner.UserRoles = new List <RoleModel>(); if (item.Owner.Fields.UserRoles != null) { docSectionModel.Owner.UserRoles = await _userProfileHelpers.RolesToViewModelAsync(item.Owner.Fields.UserRoles, requestId); } } } docSectionModel.SectionStatus = item.SectionStatus; docSectionModel.SubSectionId = item.SubSectionId; docSectionModel.AssignedTo = new UserProfileViewModel(); if (item.AssignedTo != null) { docSectionModel.AssignedTo.Id = item.AssignedTo.Id; docSectionModel.AssignedTo.DisplayName = item.AssignedTo.DisplayName; docSectionModel.AssignedTo.Mail = item.AssignedTo.Fields.Mail; docSectionModel.AssignedTo.Title = item.AssignedTo.Fields.Title; docSectionModel.AssignedTo.UserPrincipalName = item.AssignedTo.Fields.UserPrincipalName; // TODO: Not including role info since it is not relevant but if needed it needs to be set here } docSectionModel.Task = item.Task; viewModel.ProposalDocument.Content.ProposalSectionList.Add(docSectionModel); } } } return(viewModel); } catch (Exception ex) { throw new ResponseException($"RequestId: {requestId} - ProposalStatusProcessService MapToViewModel oppId: {entity.Id} - failed to map opportunity: {ex}"); } }
public async Task <OpportunityViewModel> OpportunityToViewModelAsync(Opportunity entity, string requestId = "") { var oppId = entity.Id; try { //var entityDto = TinyMapper.Map<OpportunityViewModel>(entity); var viewModel = new OpportunityViewModel { Id = entity.Id, DisplayName = entity.DisplayName, Reference = entity.Reference, Version = entity.Version, OpportunityState = OpportunityStateModel.FromValue(entity.Metadata.OpportunityState.Value), DealSize = entity.Metadata.DealSize, AnnualRevenue = entity.Metadata.AnnualRevenue, OpenedDate = entity.Metadata.OpenedDate, Industry = new IndustryModel { Name = entity.Metadata.Industry.Name, Id = entity.Metadata.Industry.Id }, Region = new RegionModel { Name = entity.Metadata.Region.Name, Id = entity.Metadata.Region.Id }, Margin = entity.Metadata.Margin, Rate = entity.Metadata.Rate, DebtRatio = entity.Metadata.DebtRatio, Purpose = entity.Metadata.Purpose, DisbursementSchedule = entity.Metadata.DisbursementSchedule, CollateralAmount = entity.Metadata.CollateralAmount, Guarantees = entity.Metadata.Guarantees, RiskRating = entity.Metadata.RiskRating, OpportunityChannelId = entity.Metadata.OpportunityChannelId, Customer = new CustomerModel { DisplayName = entity.Metadata.Customer.DisplayName, Id = entity.Metadata.Customer.Id, ReferenceId = entity.Metadata.Customer.ReferenceId }, TeamMembers = new List <TeamMemberModel>(), Notes = new List <NoteModel>(), Checklists = new List <ChecklistModel>(), CustomerDecision = new CustomerDecisionModel { Id = entity.Content.CustomerDecision.Id, Approved = entity.Content.CustomerDecision.Approved, ApprovedDate = entity.Content.CustomerDecision.ApprovedDate, LoanDisbursed = entity.Content.CustomerDecision.LoanDisbursed } }; viewModel.ProposalDocument = new ProposalDocumentModel(); viewModel.ProposalDocument.Id = entity.Content.ProposalDocument.Id; viewModel.ProposalDocument.DisplayName = entity.Content.ProposalDocument.DisplayName; viewModel.ProposalDocument.Reference = entity.Content.ProposalDocument.Reference; viewModel.ProposalDocument.DocumentUri = entity.Content.ProposalDocument.Metadata.DocumentUri; viewModel.ProposalDocument.Category = new CategoryModel(); viewModel.ProposalDocument.Category.Id = entity.Content.ProposalDocument.Metadata.Category.Id; viewModel.ProposalDocument.Category.Name = entity.Content.ProposalDocument.Metadata.Category.Name; viewModel.ProposalDocument.Content = new ProposalDocumentContentModel(); viewModel.ProposalDocument.Content.ProposalSectionList = new List <DocumentSectionModel>(); viewModel.ProposalDocument.Notes = new List <NoteModel>(); viewModel.ProposalDocument.Tags = entity.Content.ProposalDocument.Metadata.Tags; viewModel.ProposalDocument.Version = entity.Content.ProposalDocument.Version; // Checklists foreach (var item in entity.Content.Checklists) { var checklistTasks = new List <ChecklistTaskModel>(); foreach (var subitem in item.ChecklistTaskList) { var checklistItem = new ChecklistTaskModel { Id = subitem.Id, ChecklistItem = subitem.ChecklistItem, Completed = subitem.Completed, FileUri = subitem.FileUri }; checklistTasks.Add(checklistItem); } var checklistModel = new ChecklistModel { Id = item.Id, ChecklistStatus = item.ChecklistStatus, ChecklistTaskList = checklistTasks, ChecklistChannel = item.ChecklistChannel }; viewModel.Checklists.Add(checklistModel); } // TeamMembers foreach (var item in entity.Content.TeamMembers.ToList()) { var memberModel = new TeamMemberModel(); memberModel.Status = item.Status; memberModel.AssignedRole = await _userProfileHelpers.RoleToViewModelAsync(item.AssignedRole, requestId); memberModel.Id = item.Id; memberModel.DisplayName = item.DisplayName; memberModel.Mail = item.Fields.Mail; memberModel.UserPrincipalName = item.Fields.UserPrincipalName; memberModel.Title = item.Fields.Title ?? String.Empty; viewModel.TeamMembers.Add(memberModel); } // Notes foreach (var item in entity.Content.Notes.ToList()) { var note = new NoteModel(); note.Id = item.Id; var userProfile = new UserProfileViewModel(); userProfile.Id = item.CreatedBy.Id; userProfile.DisplayName = item.CreatedBy.DisplayName; userProfile.Mail = item.CreatedBy.Fields.Mail; userProfile.UserPrincipalName = item.CreatedBy.Fields.UserPrincipalName; userProfile.UserRoles = await _userProfileHelpers.RolesToViewModelAsync(item.CreatedBy.Fields.UserRoles, requestId); note.CreatedBy = userProfile; note.NoteBody = item.NoteBody; note.CreatedDateTime = item.CreatedDateTime; viewModel.Notes.Add(note); } // ProposalDocument Notes foreach (var item in entity.Content.ProposalDocument.Metadata.Notes.ToList()) { var docNote = new NoteModel(); docNote.Id = item.Id; docNote.CreatedDateTime = item.CreatedDateTime; docNote.NoteBody = item.NoteBody; docNote.CreatedBy = new UserProfileViewModel { Id = item.CreatedBy.Id, DisplayName = item.CreatedBy.DisplayName, Mail = item.CreatedBy.Fields.Mail, UserPrincipalName = item.CreatedBy.Fields.UserPrincipalName, UserRoles = await _userProfileHelpers.RolesToViewModelAsync(item.CreatedBy.Fields.UserRoles, requestId) }; viewModel.ProposalDocument.Notes.Add(docNote); } // ProposalDocument ProposalSectionList foreach (var item in entity.Content.ProposalDocument.Content.ProposalSectionList.ToList()) { if (!String.IsNullOrEmpty(item.Id)) { var docSectionModel = new DocumentSectionModel(); docSectionModel.Id = item.Id; docSectionModel.DisplayName = item.DisplayName; docSectionModel.LastModifiedDateTime = item.LastModifiedDateTime; docSectionModel.Owner = new UserProfileViewModel(); if (item.Owner != null) { docSectionModel.Owner.Id = item.Owner.Id ?? String.Empty; docSectionModel.Owner.DisplayName = item.Owner.DisplayName ?? String.Empty; if (item.Owner.Fields != null) { docSectionModel.Owner.Mail = item.Owner.Fields.Mail ?? String.Empty; docSectionModel.Owner.UserPrincipalName = item.Owner.Fields.UserPrincipalName ?? String.Empty; docSectionModel.Owner.UserRoles = new List <RoleModel>(); if (item.Owner.Fields.UserRoles != null) { docSectionModel.Owner.UserRoles = await _userProfileHelpers.RolesToViewModelAsync(item.Owner.Fields.UserRoles, requestId); } } else { docSectionModel.Owner.Mail = String.Empty; docSectionModel.Owner.UserPrincipalName = String.Empty; docSectionModel.Owner.UserRoles = new List <RoleModel>(); if (item.Owner.Fields.UserRoles != null) { docSectionModel.Owner.UserRoles = await _userProfileHelpers.RolesToViewModelAsync(item.Owner.Fields.UserRoles, requestId); } } } docSectionModel.SectionStatus = item.SectionStatus; docSectionModel.SubSectionId = item.SubSectionId; docSectionModel.AssignedTo = new UserProfileViewModel(); if (item.AssignedTo != null) { docSectionModel.AssignedTo.Id = item.AssignedTo.Id; docSectionModel.AssignedTo.DisplayName = item.AssignedTo.DisplayName; docSectionModel.AssignedTo.Mail = item.AssignedTo.Fields.Mail; docSectionModel.AssignedTo.Title = item.AssignedTo.Fields.Title; docSectionModel.AssignedTo.UserPrincipalName = item.AssignedTo.Fields.UserPrincipalName; // TODO: Not including role info since it is not relevant but if needed it needs to be set here } docSectionModel.Task = item.Task; viewModel.ProposalDocument.Content.ProposalSectionList.Add(docSectionModel); } } // DocumentAttachments viewModel.DocumentAttachments = new List <DocumentAttachmentModel>(); if (entity.DocumentAttachments != null) { foreach (var itm in entity.DocumentAttachments) { var doc = new DocumentAttachmentModel(); doc.Id = itm.Id ?? String.Empty; doc.FileName = itm.FileName ?? String.Empty; doc.Note = itm.Note ?? String.Empty; doc.Tags = itm.Tags ?? String.Empty; doc.Category = new CategoryModel(); doc.Category.Id = itm.Category.Id; doc.Category.Name = itm.Category.Name; doc.DocumentUri = itm.DocumentUri; viewModel.DocumentAttachments.Add(doc); } } return(viewModel); } catch (Exception ex) { // TODO: _logger.LogError("MapToViewModelAsync error: " + ex); throw new ResponseException($"RequestId: {requestId} - OpportunityToViewModelAsync oppId: {oppId} - failed to map opportunity: {ex}"); } }