private int InstanceOwnerLookup(InstanceOwnerLookup lookup, ref ActionResult errorResult)
        {
            if (lookup != null)
            {
                try
                {
                    string personOrOrganisationNumber = CollectIdFromLookup(lookup);

                    int?instanceOwnerLookup = LookupIdFromBridgeRegistry(personOrOrganisationNumber).Result;

                    if (instanceOwnerLookup.HasValue)
                    {
                        return(instanceOwnerLookup.Value);
                    }
                    else
                    {
                        errorResult = BadRequest("Instance owner lookup failed.");
                    }
                }
                catch (Exception e)
                {
                    errorResult = BadRequest(e.Message);
                }
            }
            else
            {
                errorResult = BadRequest("InstanceOwnerLookup cannot have null value if instanceOwnerId is not set. Cannot resolve instance owner id");
            }

            return(0);
        }
        private static string CollectIdFromLookup(InstanceOwnerLookup lookup)
        {
            string id = null;

            if (!string.IsNullOrEmpty(lookup.PersonNumber) && !string.IsNullOrEmpty(lookup.OrganisationNumber))
            {
                throw new ArgumentException("InstanceOwnerLookup cannot have both PersonNumber and OrganisationNumber set.");
            }

            if (!string.IsNullOrEmpty(lookup.PersonNumber))
            {
                id = lookup.PersonNumber;
            }
            else if (!string.IsNullOrEmpty(lookup.OrganisationNumber))
            {
                id = lookup.OrganisationNumber;
            }
            else
            {
                throw new ArgumentException("InstanceOwnerLookup must have either PersonNumber or OrganisationNumber set.");
            }

            return(id);
        }
Beispiel #3
0
        public async Task <ActionResult <Instance> > Post(
            [FromRoute] string org,
            [FromRoute] string app,
            [FromQuery] int?instanceOwnerId)
        {
            if (string.IsNullOrEmpty(org))
            {
                return(BadRequest("The path parameter 'org' cannot be empty"));
            }

            if (string.IsNullOrEmpty(app))
            {
                return(BadRequest("The path parameter 'app' cannot be empty"));
            }

            Application application = repositoryService.GetApplication(org, app);

            if (application == null)
            {
                return(NotFound($"AppId {org}/{app} was not found"));
            }

            MultipartRequestReader parsedRequest = new MultipartRequestReader(Request);

            parsedRequest.Read().Wait();

            if (parsedRequest.Errors.Any())
            {
                return(BadRequest($"Error when reading content: {parsedRequest.Errors}"));
            }

            Instance instanceTemplate = ExtractInstanceTemplate(parsedRequest);

            if (!instanceOwnerId.HasValue && instanceTemplate == null)
            {
                return(BadRequest("Cannot create an instance without an instanceOwnerId. Either provide instanceOwnerId as a query parameter or an instanceTemplate object in the body."));
            }

            if (instanceOwnerId.HasValue && instanceTemplate != null)
            {
                return(BadRequest("You cannot provide an instanceOwnerId as a query param as well as an instance template in the body. Choose one or the other."));
            }

            RequestPartValidator requestValidator = new RequestPartValidator(application);

            string multipartError = requestValidator.ValidateParts(parsedRequest.Parts);

            if (!string.IsNullOrEmpty(multipartError))
            {
                return(BadRequest($"Error when comparing content to application metadata: {multipartError}"));
            }

            // extract or create instance template
            if (instanceTemplate != null)
            {
                InstanceOwnerLookup lookup = instanceTemplate.InstanceOwnerLookup;

                if (string.IsNullOrEmpty(instanceTemplate.InstanceOwnerId) && (lookup == null || (lookup.PersonNumber == null && lookup.OrganisationNumber == null)))
                {
                    return(BadRequest($"Error: instanceOwnerId is empty and InstanceOwnerLookup is missing. You must populate instanceOwnerId or InstanceOwnerLookup"));
                }
            }
            else
            {
                instanceTemplate = new Instance();
                instanceTemplate.InstanceOwnerId = instanceOwnerId.Value.ToString();
            }

            Party party = null;

            if (instanceTemplate.InstanceOwnerId != null)
            {
                party = await registerService.GetParty(int.Parse(instanceTemplate.InstanceOwnerId));
            }
            else
            {
                /* todo - lookup personNumber or organisationNumber - awaiting registry endpoint implementation */
            }

            if (!InstantiationHelper.IsPartyAllowedToInstantiate(party, application.PartyTypesAllowed))
            {
                return(Forbid($"Party {party?.PartyId} is not allowed to instantiate this application {org}/{app}"));
            }

            // use process controller to start process
            instanceTemplate.Process = null;

            Instance instance = null;

            try
            {
                instance = await instanceService.CreateInstance(org, app, instanceTemplate);

                if (instance == null)
                {
                    throw new PlatformClientException("Failure instantiating instance. UnknownError");
                }
            }
            catch (Exception instanceException)
            {
                string message = $"Failure in multipart prefil. Could not create an instance of {org}/{app} for {instanceOwnerId}. App-backend has problem accessing platform storage.";

                logger.LogError($"{message} - {instanceException}");
                return(StatusCode(500, $"{message} - {instanceException.Message}"));
            }

            try
            {
                Instance instanceWithData = await StorePrefillParts(instance, parsedRequest.Parts);

                if (instanceWithData != null)
                {
                    instance = instanceWithData;
                }
            }
            catch (Exception dataException)
            {
                string message = $"Failure storing multipart prefil. Could not create a data element for {instance.Id} of {org}/{app}. App-backend has problem accessing platform storage.";
                logger.LogError($"{message} - {dataException}");

                // todo add compensating transaction (delete instance)
                return(StatusCode(500, $"{message} - {dataException.Message}"));
            }

            SetAppSelfLinks(instance, Request);
            string url = instance.SelfLinks.Apps;

            return(Created(url, instance));
        }