public async Task <ActionResult <IEnumerable <OrganizationListViewModel> > > GetOrganizations()
        {
            IEnumerable <OrganizationListViewModel> organizations;

            if (User.HasAdminView())
            {
                var notifiedIds = await _siteService.GetNotifiedSiteIdsForAdminAsync(User);

                organizations = await _organizationService.GetOrganizationsAsync();

                foreach (var organization in organizations)
                {
                    organization.Sites = organization.Sites.Select(s => s.SetNotification(notifiedIds.Contains(s.Id)));
                }
            }
            else
            {
                var party = await _partyService.GetPartyForUserIdAsync(User.GetPrimeUserId());

                organizations = (party != null)
                    ? await _organizationService.GetOrganizationsByPartyIdAsync(party.Id)
                    : Enumerable.Empty <OrganizationListViewModel>();
            }

            return(Ok(ApiResponse.Result(organizations)));
        }
        public async Task <ActionResult <IEnumerable <Organization> > > GetOrganizations([FromQuery] bool verbose)
        {
            IEnumerable <Organization> organizations = null;

            if (User.HasAdminView())
            {
                organizations = await _organizationService.GetOrganizationsAsync();
            }
            else
            {
                var party = await _partyService.GetPartyForUserIdAsync(User.GetPrimeUserId());

                organizations = (party != null)
                    ? await _organizationService.GetOrganizationsAsync(party.Id)
                    : Enumerable.Empty <Organization>();
            }

            if (verbose)
            {
                return(Ok(ApiResponse.Result(organizations)));
            }
            else
            {
                return(Ok(ApiResponse.Result(_mapper.Map <IEnumerable <OrganizationListViewModel> >(organizations))));
            }
        }
Пример #3
0
        public async Task <ActionResult <IEnumerable <Organization> > > GetOrganizations()
        {
            IEnumerable <Organization> organizations = null;

            if (User.IsAdmin() || User.HasAdminView())
            {
                organizations = await _organizationService.GetOrganizationsAsync();
            }
            else
            {
                var party = await _partyService.GetPartyForUserIdAsync(User.GetPrimeUserId());

                organizations = (party != null)
                    ? await _organizationService.GetOrganizationsAsync(party.Id)
                    : new List <Organization>();
            }

            return(Ok(ApiResponse.Result(organizations)));
        }
Пример #4
0
        public async Task <IActionResult> UpdateSite(int siteId, Site updatedSite, [FromQuery] bool isCompleted)
        {
            var site = await _siteService.GetSiteNoTrackingAsync(siteId);

            if (site == null)
            {
                return(NotFound(ApiResponse.Message($"Site not found with id {siteId}")));
            }

            var party = await _partyService.GetPartyForUserIdAsync(User.GetPrimeUserId());

            if (!User.CanEdit(party))
            {
                return(Forbid());
            }

            await _siteService.UpdateSiteAsync(siteId, updatedSite, isCompleted);

            return(NoContent());
        }
        public async Task <int> CreateOrganizationAsync(Party signingAuthority)
        {
            signingAuthority.ThrowIfNull(nameof(signingAuthority));

            var userId = _httpContext.HttpContext.User.GetPrimeUserId();

            var partyExists = await _partyService.PartyUserIdExistsAsync(userId);

            if (!partyExists)
            {
                await _partyService.CreatePartyAsync(signingAuthority);
            }

            signingAuthority = await _partyService.GetPartyForUserIdAsync(userId);

            var organizations = await GetOrganizationsAsync(signingAuthority.Id);

            if (organizations.Count() != 0)
            {
                throw new InvalidOperationException("Could not create Organization. Only one organization can exist for a party.");
            }

            var organization = new Organization
            {
                SigningAuthorityId = signingAuthority.Id
            };

            _context.Organizations.Add(organization);

            var created = await _context.SaveChangesAsync();

            if (created < 1)
            {
                throw new InvalidOperationException("Could not create Organization.");
            }

            await _businessEventService.CreateOrganizationEventAsync(organization.Id, signingAuthority.Id, "Organization Created");

            return(organization.Id);
        }