private static async Task <DialogTurnResult> StoreMenuData(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            StoreOrderDuetime storeData;

            try
            {
                storeData =
                    JsonConvert.DeserializeObject <StoreOrderDuetime>(stepContext.Context.Activity.Value.ToString());
            }
            catch
            {
                return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("OOPS! It seems you didn't choose any menu.") }, cancellationToken));
            }

            if (storeData.DueTime == null)
            {
                stepContext.ActiveDialog.State["stepIndex"] = (int)stepContext.ActiveDialog.State["stepIndex"] - 1;
                return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("OOPS! I don't know when will this order END. Please order again!") }, cancellationToken));
            }

            var startTime = (DateTime)stepContext.Values["OrderTime"];
            var endTime   = GetEndTime(startTime, storeData);

            if (startTime >= endTime)
            {
                stepContext.ActiveDialog.State["stepIndex"] = (int)stepContext.ActiveDialog.State["stepIndex"] - 1;
                return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("OOPS! The end time is earlier than start time. Please order again!") }, cancellationToken));
            }

            var sched = new Schedule()
            {
                ScheduleId     = Guid.NewGuid(),
                GroupId        = Guid.Empty,
                TriggerType    = 1,
                TriggerTime    = startTime,
                EndTime        = endTime,
                MenuUri        = storeData.Url,
                RepeatWeekdays = 0
            };

            // startTime = DateTime.Now.AddSeconds(5f);
            // endTime = DateTime.Now.AddSeconds(30f);
            var teamsChannelData = JsonConvert.DeserializeObject <dynamic>(stepContext.Context.Activity.ChannelData.ToString());
            var services         = await _schedulerFactory.GetAllSchedulers();

            var scheduler = new ScheduleCreator(services[0], stepContext.Context.Activity.From.Id, storeData.OrderID, sched.ScheduleId.ToString());

            scheduler.CreateSingleGroupBuy(startTime, endTime, teamsChannelData.channel.id.ToString());
            AddConversationReference(stepContext.Context.Activity as Activity);

            _schedRepo.Create(sched);
            _schedRepo.context.SaveChanges();

            await stepContext.Context.SendActivityAsync(MessageFactory.Attachment(new CreateReservationCard().CreateReserveResult(storeData.StoreName, startTime, endTime)));

            await stepContext.Context.SendActivityAsync(MessageFactory.Text("The group buy has been reserved. I will notify you when the order start :D"));

            return(await stepContext.EndDialogAsync());
        }
Ejemplo n.º 2
0
        protected override async Task <TaskModuleResponse> OnTeamsTaskModuleSubmitAsync(ITurnContext <IInvokeActivity> turnContext, TaskModuleRequest taskModuleRequest, CancellationToken cancellationToken)
        {
            var factory   = new AdaptiveCardDataFactory(turnContext, taskModuleRequest);
            var fetchType = factory.GetCardActionType();

            if (fetchType?.Equals("ResultStoreCard") == true)
            {
                //create group buy card
                var orderId = Guid.NewGuid().ToString();
                var data    = factory.GetGroupBuyCard(orderId);
                _orderService.CreateOrder(orderId, turnContext.Activity.ChannelId, data.StoreName);
                var cardService = new CreateCardService2();
                await turnContext.SendActivityAsync(MessageFactory.Attachment(cardService.GetStore(data)));

                //create scheduler
                var services = await SchedulerFactory.GetAllSchedulers();

                var scheduler = new ScheduleCreator(services[0], turnContext.Activity.From.Id, orderId);
                var dueTime   = DateTime.Parse(data.DueTime);
                scheduler.CreateSingleGroupBuy(dueTime);
                AddConversationReference(turnContext.Activity as Activity);

                return(null);
            }
            if (fetchType?.Equals("FetchSelectedFoods") == true)
            {
                var card = new CreateCardService2().GetChosenFoodFromMenu(factory);

                if (card.Name?.Contains("error") == true)
                {
                    var taskInfo = new TaskModuleTaskInfo();
                    taskInfo.Card = card;
                    return(await Task.FromResult(taskInfo.ToTaskModuleResponse()));
                }
                else
                {
                    await turnContext.SendActivityAsync(MessageFactory.Attachment(card));

                    new CreateCardService2().GetChosenFoodFromMenuCreateOrderDetail(factory, turnContext.Activity.From.Id);
                    return(null);
                }
            }
            else if (fetchType?.Equals("GetCustomizedStore") == true)
            {
                //get order data
                var data = factory.GetCardData <StoreOrderDuetime>();
                data.DueTime = JObject.FromObject(factory.Request.Data).GetValue("DueTime").ToString();
                data.OrderID = Guid.NewGuid().ToString();

                //create an order and group-buy card
                _orderService.CreateOrder(data.OrderID, turnContext.Activity.ChannelId, data.StoreName);
                await turnContext.SendActivityAsync(MessageFactory.Attachment(_menuOrderService.GetStore(factory, data.OrderID)));

                //create scheduler
                var services = await SchedulerFactory.GetAllSchedulers();

                var scheduler = new ScheduleCreator(services[0], turnContext.Activity.From.Id, data.OrderID);
                var dueTime   = DateTime.Parse(data.DueTime);
                scheduler.CreateSingleGroupBuy(dueTime);
                AddConversationReference(turnContext.Activity as Activity);
                return(null);
            }
            //ting 按下按鈕傳資料到data
            else if (fetchType?.Equals("GetCustomizedMenu") == true)
            {
                var teamsId = turnContext.Activity.GetChannelData <TeamsChannelData>()?.Tenant?.Id;
                var menu    = _menuService.CreateMenu(factory, teamsId);
                if (menu == null)
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text("Please create your store first!"));
                }
                else
                {
                    _menuService.CreateMenuDetail(factory, menu.MenuId);
                    await turnContext.SendActivityAsync(MessageFactory.Text("Create Successfully!"));

                    var NewId          = factory.GetCardData <StoreInfoData>().Name;
                    var CustomMenucard = _customMenuService.CallCustomeCard(NewId);
                    var activity       = MessageFactory.Attachment(CustomMenucard);
                    activity.Id = NewId; //指定要更新的activity
                    await turnContext.UpdateActivityAsync(activity, cancellationToken);
                }
                return(null);
            }
            else if (fetchType?.Equals("GetCustomizedMenuDetail") == true)
            {
                var menu = factory.GetCardData <StoreInfoData>().Guid;
                _menuService.CreateMenuDetail(factory, Guid.Parse(menu));
                await turnContext.SendActivityAsync(MessageFactory.Text("Create Successfully!"));

                return(null);
            }
            else if (fetchType?.Equals("reserveStore") == true)
            {
                var orderId = Guid.NewGuid().ToString();
                var data    = factory.GetGroupBuyCard(orderId);
                turnContext.Activity.Value = JsonConvert.SerializeObject(data);
                turnContext.Activity.Type  = "message";
                await Dialog.RunAsync(turnContext, ConversationState.CreateProperty <DialogState>(nameof(DialogState)), cancellationToken);

                return(null);
            }
            //育銨
            else
            {
                var TaskInfo = new TaskModuleTaskInfo();
                TaskInfo.Card = new CreateCardService2().GetResultCustomizedModification(factory);
                new CreateCardService2().SetTaskInfo(TaskInfo, TaskModuleUIConstants.UpdateMenu);
                return(await Task.FromResult(TaskInfo.ToTaskModuleResponse()));
            }
        }