public async Task <Result <OutstandingIposResultDto> > Handle(GetOutstandingIposForCurrentPersonQuery request, CancellationToken cancellationToken) { try { var currentUserOid = _currentUserProvider.GetCurrentUserOid(); //We could have filtered based on project access, however //the scenario that a person should lose access to a single project //in a plant after being added to the invitation is (according to the client) //so unlikely that we do not need to take it into consideration var nonCancelledInvitations = await(from i in _context.QuerySet <Invitation>() .Include(ss => ss.Participants) where !i.AcceptedAtUtc.HasValue where i.Status != IpoStatus.Canceled select i).ToListAsync(cancellationToken); var currentUsersOutstandingInvitations = new List <Invitation>(); foreach (var invitation in nonCancelledInvitations) { var currentUsersFunctionalRoleCodes = await _meApiService.GetFunctionalRoleCodesAsync(_plantProvider.Plant); if (UserWasInvitedAsPersonParticipant(invitation, currentUserOid)) { currentUsersOutstandingInvitations.Add(invitation); } else if (UserWasInvitedAsPersonInFunctionalRole(invitation, currentUsersFunctionalRoleCodes)) { currentUsersOutstandingInvitations.Add(invitation); } } var outstandingIposResultDto = new OutstandingIposResultDto( currentUsersOutstandingInvitations.Select(invitation => { var organization = invitation.CompletedAtUtc.HasValue ? Organization.ConstructionCompany : Organization.Contractor; return(new OutstandingIpoDetailsDto { InvitationId = invitation.Id, Description = invitation.Description, Organization = organization }); })); return(new SuccessResult <OutstandingIposResultDto>(outstandingIposResultDto)); } catch (Exception) { return(new SuccessResult <OutstandingIposResultDto>(new OutstandingIposResultDto( new List <OutstandingIpoDetailsDto>()))); } }
protected override void SetupNewDatabase(DbContextOptions <IPOContext> dbContextOptions) { _query = new GetOutstandingIposForCurrentPersonQuery(); _person = new Person(_currentUserOid, "*****@*****.**", "FirstName", "LastName", "UserName"); using (var context = new IPOContext(dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider)) { IList <string> pcsFunctionalRoleCodes = new List <string> { _functionalRoleCode }; _meApiServiceMock = new Mock <IMeApiService>(); _meApiServiceMock .Setup(x => x.GetFunctionalRoleCodesAsync(TestPlant)) .Returns(Task.FromResult(pcsFunctionalRoleCodes)); _invitationWithPersonParticipant = new Invitation( TestPlant, _projectName, "TestInvitation1", "TestDescription1", DisciplineType.DP, new DateTime(), new DateTime(), null, new List <McPkg> { new McPkg(TestPlant, _projectName, "Comm", "Mc", "d", "1|2") }, new List <CommPkg>()); _invitationWithFunctionalRoleParticipant = new Invitation( TestPlant, _projectName, "TestInvitation2", "TestDescription2", DisciplineType.DP, new DateTime(), new DateTime(), null, new List <McPkg> { new McPkg(TestPlant, _projectName, "Comm", "Mc", "d", "1|2") }, new List <CommPkg>()); _cancelledInvitation = new Invitation( TestPlant, _projectName, "TestInvitation3", "TestDescription3", DisciplineType.DP, new DateTime(), new DateTime(), null, new List <McPkg> { new McPkg(TestPlant, _projectName, "Comm", "Mc", "d", "1|2") }, new List <CommPkg>()); _invitationWithPersonParticipantContractor = new Invitation( TestPlant, _projectName, "TestInvitation4", "TestDescription4", DisciplineType.DP, new DateTime(), new DateTime(), null, new List <McPkg> { new McPkg(TestPlant, _projectName, "Comm", "Mc", "d", "1|2") }, new List <CommPkg>()); _invitationWithFunctionalRoleParticipantContractor = new Invitation( TestPlant, _projectName, "TestInvitation5", "TestDescription5", DisciplineType.DP, new DateTime(), new DateTime(), null, new List <McPkg> { new McPkg(TestPlant, _projectName, "Comm", "Mc", "d", "1|2") }, new List <CommPkg>()); _functionalRoleParticipant = new Participant( TestPlant, Organization.ConstructionCompany, IpoParticipantType.FunctionalRole, _functionalRoleCode, null, null, null, null, null, 1); _functionalRoleParticipant.SetProtectedIdForTesting(1); _personParticipant = new Participant( TestPlant, Organization.ConstructionCompany, IpoParticipantType.Person, null, null, null, null, null, _currentUserOid, 1); _personParticipant.SetProtectedIdForTesting(2); _personParticipant2 = new Participant( TestPlant, Organization.Operation, IpoParticipantType.Person, null, null, null, null, null, _currentUserOid, 3); _personParticipant2.SetProtectedIdForTesting(3); _functionalRoleParticipantContractor = new Participant( TestPlant, Organization.Contractor, IpoParticipantType.FunctionalRole, _functionalRoleCode, null, null, null, null, null, 0); _functionalRoleParticipantContractor.SetProtectedIdForTesting(4); _personParticipantContractor = new Participant( TestPlant, Organization.Contractor, IpoParticipantType.Person, null, null, null, null, null, _currentUserOid, 0); _personParticipantContractor.SetProtectedIdForTesting(5); _invitationWithPersonParticipant.AddParticipant(_personParticipant); _invitationWithFunctionalRoleParticipant.AddParticipant(_functionalRoleParticipant); _cancelledInvitation.AddParticipant(_personParticipant2); _invitationWithPersonParticipantContractor.AddParticipant(_personParticipantContractor); _invitationWithFunctionalRoleParticipantContractor.AddParticipant(_functionalRoleParticipantContractor); _invitationWithPersonParticipant.CompleteIpo( _personParticipant, _personParticipant.RowVersion.ConvertToString(), _person, new DateTime()); _invitationWithFunctionalRoleParticipant.CompleteIpo( _functionalRoleParticipant, _functionalRoleParticipant.RowVersion.ConvertToString(), _person, new DateTime()); _cancelledInvitation.CompleteIpo( _personParticipant, _personParticipant.RowVersion.ConvertToString(), _person, new DateTime()); _cancelledInvitation.CancelIpo(_person); context.Invitations.Add(_invitationWithPersonParticipant); context.Invitations.Add(_invitationWithFunctionalRoleParticipant); context.Invitations.Add(_cancelledInvitation); context.Invitations.Add(_invitationWithPersonParticipantContractor); context.Invitations.Add(_invitationWithFunctionalRoleParticipantContractor); context.SaveChangesAsync().Wait(); } }