예제 #1
0
        public static async Task AllClientOnboarding(
            [BlobTrigger("bank/onboarding/client-{name}")] Stream accountsToOnboard,
            [DurableClient]  IDurableOrchestrationClient newAccountsOrchestration)
        {
            var serializer = new JsonSerializer();

            List <AccountOpeningData> allNewAccounts = new List <AccountOpeningData>();

            using (var sr = new StreamReader(accountsToOnboard))
            {
                using (JsonTextReader reader = new JsonTextReader(sr))
                {
                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.StartObject)
                        {
                            // Load each object from the stream and do something with it
                            AccountOpeningData nextRecord = JsonSerializer.Create().Deserialize <AccountOpeningData>(reader);
                            if (null != nextRecord)
                            {
                                allNewAccounts.Add(nextRecord);
                            }
                        }
                    }
                }
            }

            await newAccountsOrchestration.StartNewAsync(nameof(AllClientOnboardingOrchestration), allNewAccounts);
        }
예제 #2
0
        public static async Task SingleClientOnboardingOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext createAccountContext
            )
        {
            AccountOpeningData payload = createAccountContext.GetInput <AccountOpeningData>();

            if (null != payload)
            {
                string newAccountNumber = await createAccountContext.CallActivityAsync <string>(nameof( CreateRandomAccountNumber), null);

                string jsonContent = JsonConvert.SerializeObject(payload);

                var newAccountRequest = new DurableHttpRequest(
                    HttpMethod.Post,
                    new Uri($"https://retailbankazurefunctionapp.azurewebsites.net/api/OpenAccount/{newAccountNumber}?code=9IcflcYh904F3Dcbp5IGCPuDrqelVJniQK5Ck1ZcVdacOd3Mx4ShWQ=="),
                    content: jsonContent);


                newAccountRequest.Headers.Add(@"Content-Type", @"application/json");

                DurableHttpResponse restartResponse = await createAccountContext.CallHttpAsync(newAccountRequest);
            }
        }
        public static async Task <HttpResponseMessage> OpenAccountRun(
            [HttpTrigger(AuthorizationLevel.Function, "POST", Route = @"OpenAccount/{accountnumber}")] HttpRequestMessage req,
            string accountnumber,
            [EventStream("Bank", "Account", "{accountnumber}")]  EventStream bankAccountEvents)
        {
            // Set the start time for how long it took to process the message
            DateTime startTime = DateTime.UtcNow;

            if (await bankAccountEvents.Exists())
            {
                return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.Forbidden,
                                                             FunctionResponse.CreateResponse(startTime,
                                                                                             true,
                                                                                             $"Account {accountnumber} already exists"),
                                                             FunctionResponse.MEDIA_TYPE));
            }
            else
            {
                // Get request body
                AccountOpeningData data = await req.Content.ReadAsAsync <AccountOpeningData>();

                // Append a "created" event
                DateTime dateCreated            = DateTime.UtcNow;
                Account.Events.Opened evtOpened = new Account.Events.Opened()
                {
                    LoggedOpeningDate = dateCreated
                };
                if (!string.IsNullOrWhiteSpace(data.Commentary))
                {
                    evtOpened.Commentary = data.Commentary;
                }
                try
                {
                    await bankAccountEvents.AppendEvent(evtOpened,
                                                        streamConstraint : EventStreamExistenceConstraint.MustBeNew
                                                        );
                }
                catch (EventStreamWriteException exWrite)
                {
                    return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.Conflict,
                                                                 FunctionResponse.CreateResponse(startTime,
                                                                                                 true,
                                                                                                 $"Account {accountnumber} had a conflict error on creation {exWrite.Message }"),
                                                                 FunctionResponse.MEDIA_TYPE));
                }

                // If there is an initial deposit in the account opening data, append a "deposit" event
                if (data.OpeningBalance.HasValue)
                {
                    Account.Events.MoneyDeposited evtInitialDeposit = new Account.Events.MoneyDeposited()
                    {
                        AmountDeposited   = data.OpeningBalance.Value,
                        LoggedDepositDate = dateCreated,
                        Commentary        = "Opening deposit"
                    };
                    await bankAccountEvents.AppendEvent(evtInitialDeposit);
                }

                // If there is a beneficiary in the account opening data append a "beneficiary set" event
                if (!string.IsNullOrEmpty(data.ClientName))
                {
                    Account.Events.BeneficiarySet evtBeneficiary = new Account.Events.BeneficiarySet()
                    {
                        BeneficiaryName = data.ClientName
                    };
                    await bankAccountEvents.AppendEvent(evtBeneficiary);
                }

                return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.Created,
                                                             FunctionResponse.CreateResponse(startTime,
                                                                                             false,
                                                                                             $"Account { accountnumber} created"),
                                                             FunctionResponse.MEDIA_TYPE));
            }
        }