Beispiel #1
0
        //private static ConcurrentDictionary<CultureInfo, IForm<AppoinmentForm>> _forms = new ConcurrentDictionary<CultureInfo, IForm<AppoinmentForm>>();

        public static IForm <AppoinmentForm> BuildForm()
        {
            OnCompletionAsyncDelegate <AppoinmentForm> processOrder = async(context, state) =>
            {
                await context.PostAsync("We will send you the status.");
            };
            CultureInfo ci = new CultureInfo("en");

            Thread.CurrentThread.CurrentCulture   = ci;
            Thread.CurrentThread.CurrentUICulture = ci;
            FormBuilder <AppoinmentForm> form = new FormBuilder <AppoinmentForm>();

            return(form.Message("Fill the information for schedule an appoinment, please")
                   .Field("Office", validate:
                          async(state, value) =>
            {
                ValidateResult vResult = new ValidateResult();
                List <ACFAppointment> listAppoinments = await Services.AppoinmentService.GetAppoinments();
                StringBuilder response = new StringBuilder();
                if (listAppoinments.Count <= 0)
                {
                    response.Append("No se encontraron items ").ToString();
                    vResult.Feedback = string.Format(response.ToString());
                    vResult.IsValid = false;
                }
                else
                {
                    foreach (ACFAppointment item in listAppoinments)
                    {
                        response.Append(item.Name + ", \n");
                    }
                    //vResult.Feedback = string.Format(response.ToString());
                    vResult.Feedback = "sdas";
                    vResult.Value = "asdas";
                    vResult.IsValid = true;
                }
                return vResult;
            })
                   .Field(new FieldReflector <AppoinmentForm>(nameof(Service))
                          .SetType(null)
                          .SetDefine((state, field) =>
            {
                string office;
                if (!String.IsNullOrEmpty(state.Office))
                {
                    office = state.Office.ToString();
                }
                if (GetServicesOtempues().Count > 0)
                {
                    foreach (var prod in GetServicesOtempues())
                    {
                        field
                        .AddDescription(prod.Name, prod.Name)
                        .AddTerms(prod.Name, prod.Name);
                    }
                    return Task.FromResult(true);
                }
                else
                {
                    return Task.FromResult(false);
                }
            }))
                   .Field("StartDate", validate:
                          async(state, response) =>
            {
                ValidateResult vResult = new ValidateResult();
                var result = new ValidateResult {
                    IsValid = true, Value = response
                };
                var address = (response as string).Trim();

                List <OTempus.Library.Class.Calendar> listCalendars = new List <OTempus.Library.Class.Calendar>();
                if (GetServicesOtempues().Count > 0)
                {
                    //Service two and date...
                    listCalendars = GetCalendar("2", "9/21/2017");
                }
                List <ACFAppointment> listAppoinments = await Services.AppoinmentService.GetAppoinments();
                List <CalendarGetSlotsResults> listGetAvailablesSlots = new List <CalendarGetSlotsResults>();
                StringBuilder responses = new StringBuilder();
                responses.Append("No existen slots").ToString();
                vResult.Feedback = string.Format(responses.ToString());

                vResult.IsValid = false;
                if (listCalendars.Count > 0)
                {
                    listGetAvailablesSlots = AppoinmentService.GetAvailablesSlots(listCalendars[0].Id);
                    bool availablesSlots = true;
                    foreach (OTempus.Library.Class.CalendarGetSlotsResults calendarSlots in listGetAvailablesSlots)
                    {
                        //The status 0 is available, the status 4 is locked, 1 reserved
                        if (calendarSlots.Status != 0)
                        {
                            availablesSlots = false;
                            //In the example here we reserve the first that we find
                            //AppoinmentService.ReserveSlots(listCalendars[0].Id,calendarSlots.OrdinalNumber,1);

                            /*Example to freeup slots*/
                            AppoinmentService.FreeUpSlots(listCalendars[0].Id, calendarSlots.OrdinalNumber);
                            /*Example to lock slots*/
                            //AppoinmentService.LockSlots(listCalendars[0].Id, calendarSlots.OrdinalNumber);
                        }
                    }
                    if (availablesSlots)
                    {
                        vResult.Feedback = "Si hay slots";
                        vResult.Value = "slot 1";
                        vResult.IsValid = true;
                    }
                    else
                    {
                        responses.Append("No existen slots disponibles").ToString();
                        vResult.Feedback = string.Format(response.ToString());
                        vResult.IsValid = false;
                    }
                }
                return vResult;
            })
                   .Field(new FieldReflector <AppoinmentForm>(nameof(Calendar))
                          .SetType(null)
                          .SetDefine((state, field) =>
            {
                string date;
                string service;
                if (!String.IsNullOrEmpty(state.StartDate) && !String.IsNullOrEmpty(state.Service))
                {
                    date = state.StartDate.ToString();
                    service = state.Service.ToString();
                    if (GetServicesOtempues().Count > 0)
                    {
                        // foreach (var prod in GetCalendar("2", "9/19/2017"))
                        foreach (var prod in GetCalendar(service, date))
                        {
                            field
                            .AddDescription(prod.CalendarDate.ToString(), prod.CalendarDate.ToString())
                            .AddTerms(prod.CalendarDate.ToString(), prod.CalendarDate.ToString());
                        }
                        return Task.FromResult(true);
                    }
                    else
                    {
                        return Task.FromResult(false);
                    }
                }

                return Task.FromResult(false);
            }))

                   /*.Field(nameof(Calendar),validate: async (state, value) =>
                    * {
                    * ValidateResult vResult = new ValidateResult();
                    * var values = ((List<object>)value).OfType<Appoinment>();
                    * var result = new ValidateResult { IsValid = true, Value = values };
                    *
                    *
                    * List<Appoinment> listAppoinments = await AppoinmentService.GetAppoinments();
                    * StringBuilder response = new StringBuilder();
                    * //vResult.Feedback = string.Format(response.ToString());
                    * // vResult.Feedback = state.Office.ToString();
                    * vResult.IsValid = true;
                    * vResult.Value = state.Office.ToString() + " " + state.Service.ToString();
                    * vResult.Feedback = state.Office.ToString() + " " + state.Service.ToString();
                    * return vResult;
                    * })*/
                   .Field("Test", validate:
                          async(state, value) =>
            {
                string office = state.Calendar.ToString();
                ValidateResult vResult = new ValidateResult();
                List <ACFAppointment> listAppoinments = await AppoinmentService.GetAppoinments();
                StringBuilder response = new StringBuilder();
                //vResult.Feedback = string.Format(response.ToString());
                // vResult.Feedback = state.Office.ToString();
                vResult.IsValid = true;
                vResult.Value = state.Office.ToString() + " " + state.Service.ToString();
                vResult.Feedback = state.Office.ToString() + " " + state.Service.ToString();
                return vResult;
            })
                   .Field(nameof(Color))

                   .Confirm("Are you selected the office: {Office} \n, the color: {Color}  \n  And the Service {Service}? (yes/no)")
                   .AddRemainingFields()
                   .Message("The process for create the appoinment has been started!")
                   .OnCompletion(processOrder)
                   .Build());
        }
Beispiel #2
0
        /// <summary>
        /// Create a IForm(Form flow) based in BookForm object
        /// </summary>
        /// <returns></returns>
        ///
        public static IForm <BookForm> BuildForm()
        {
            #region processOrder
            OnCompletionAsyncDelegate <BookForm> processOrder = async(context, state) =>
            {
                //Get the actual user state of the customer
                ACFCustomer customerState = new ACFCustomer();
                try
                {
                    if (!context.UserData.TryGetValue <ACFCustomer>("customerState", out customerState))
                    {
                        customerState = new ACFCustomer();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Not exists a user session");
                }
                try
                {
                    ResultObjectBase resultObjectBase = new ResultObjectBase();
                    int serviceID = 0;
                    int unitId    = 0;
                    unitId = Utilities.Util.GetUnitIdFromBotOption(state.Office);
                    string ordinalNumber = Utilities.Util.GetOrdinalNumberFromBotOption(state.Hour);
                    try
                    {
                        List <Service> listService = AppoinmentService.listServicesByName(state.Service, 1, "en", false, unitId);
                        serviceID = listService[0].Id;
                    }
                    catch (Exception)
                    {
                        throw new Exception("I don't found appointments with the service: " + state.Service);
                    }

                    try
                    {
                        resultObjectBase = AppoinmentService.SetAppoinment(0, serviceID, customerState.CustomerId, Convert.ToInt32(ordinalNumber), Convert.ToInt32(
                                                                               state.CalendarId));
                        await context.PostAsync("Your appointment has been scheduled with the id: " + resultObjectBase.Id);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("I can't book the appointment, error: " + ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    await context.PostAsync($"Failed with message: {ex.Message}");
                }
            };
            #endregion
            CultureInfo ci = new CultureInfo("en");
            Thread.CurrentThread.CurrentCulture   = ci;
            Thread.CurrentThread.CurrentUICulture = ci;
            FormBuilder <BookForm> form = new FormBuilder <BookForm>();
            return(form.Message("Fill the information for schedule an appointment, please")
                   .Field(new FieldReflector <BookForm>(nameof(Office))
                          .SetType(null)
                          .SetDefine((state, field) =>
            {
                List <Unit> list = GetListUnitsConfigured();
                string data = string.Empty;
                if (list.Count > 0)
                {
                    foreach (var unit in list)
                    {
                        //This format is important, follow this structure
                        data = unit.Id + "." + unit.Name;
                        field
                        .AddDescription(data, data)
                        .AddTerms(data, data);
                    }
                    return Task.FromResult(true);
                }
                else
                {
                    return Task.FromResult(false);
                }
            }))
                   .Field(new FieldReflector <BookForm>(nameof(Service))
                          .SetType(null)
                          .SetDefine((state, field) =>
            {
                int unitId = 0;
                if (!String.IsNullOrEmpty(state.Office))
                {
                    //Get the unit id by the option above selected
                    unitId = Utilities.Util.GetUnitIdFromBotOption(state.Office);
                    if (GetServicesOtempues(unitId).Count > 0)
                    {
                        foreach (var prod in GetServicesOtempues(unitId))
                        {
                            field
                            .AddDescription(prod.Name, prod.Name)
                            .AddTerms(prod.Name, prod.Name);
                        }
                        return Task.FromResult(true);
                    }
                    else
                    {
                        return Task.FromResult(false);
                    }
                }
                return Task.FromResult(true);
            }))
                   .Field(new FieldReflector <BookForm>(nameof(StartDateAndTime))
                          .SetType(null)
                          .SetDefine((state, field) =>
            {
                string date;
                string service;
                List <Service> listService;
                int unitId = 0;
                List <OTempus.Library.Class.Calendar> listCalendars;
                StringBuilder response = new StringBuilder();
                List <CalendarGetSlotsResults> listGetAvailablesSlots = new List <CalendarGetSlotsResults>();
                if (!String.IsNullOrEmpty(state.Service) && !String.IsNullOrEmpty(state.Office))
                {
                    unitId = Utilities.Util.GetUnitIdFromBotOption(state.Office);
                    try
                    {
                        listService = AppoinmentService.listServicesByName(state.Service, 1, "en", false, unitId);
                        int serviceID = listService[0].Id;
                        listCalendars = new List <OTempus.Library.Class.Calendar>();

                        if (GetServicesOtempues(unitId).Count > 0)
                        {
                            //listCalendars = GetCalendar(serviceID.ToString(), dateAndTime);
                            //i commented this line beacuse i will take today and Thre days more, for get the calendars, and then get the dates of this calendars
                            listCalendars = GetCalendar(serviceID.ToString(), DateTime.Today.ToString());

                            if (listCalendars.Count == 0)
                            {
                                response.Append("Not exists calendars in this date, try it one more time , or write 'quit' for exit").ToString();
                                //vResult.Feedback = string.Format(response.ToString());
                                //vResult.IsValid = false;
                                return Task.FromResult(false);
                            }
                            else
                            {
                                foreach (var calendar in listCalendars)
                                {
                                    string data = calendar.Id + ".-" + calendar.CalendarDate.ToString();
                                    field
                                    .AddDescription(data, data)
                                    .AddTerms(data, data);
                                }
                                return Task.FromResult(true);
                            }          //End else
                        }              //End if
                    }                  //End try
                    catch (Exception e) { }
                }
                return Task.FromResult(true);
            }))

                   /*.Field("StartDateAndTime", validate:
                    * async (state, responses) =>
                    * {
                    *  string date;
                    *  string service;
                    *  List<Service> listService;
                    *  List<OTempus.Library.Class.Calendar> listCalendars;
                    *  //ValidateResult vResult = new ValidateResult();
                    *  var vResult = new ValidateResult { IsValid = true, Value = responses };
                    *  var dateAndTime = (responses as string).Trim();
                    *  StringBuilder response = new StringBuilder();
                    *  List<CalendarGetSlotsResults> listGetAvailablesSlots = new List<CalendarGetSlotsResults>();
                    *  if (!String.IsNullOrEmpty(state.Service) && !String.IsNullOrEmpty(state.Office) && !String.IsNullOrEmpty(dateAndTime))
                    *  {
                    *      int unitId = Utilities.Util.GetUnitIdFromBotOption(state.Office);
                    *      try
                    *      {
                    *      listService = AppoinmentService.listServicesByName(state.Service, 1, "en", false, unitId);
                    *      //It find almost the time one record, but in the case that found two,
                    *      int serviceID = listService[0].Id;
                    *          listCalendars = new List<OTempus.Library.Class.Calendar>();
                    *          date = dateAndTime; service = state.Service;
                    *          if (GetServicesOtempues(unitId).Count > 0)
                    *          {
                    *          //Service two and date...
                    *          listCalendars = GetCalendar(serviceID.ToString(), dateAndTime);
                    *              if (listCalendars.Count == 0)
                    *              {
                    *                  response.Append("Not exists calendars in this date, try it one more time , or write 'quit' for exit").ToString();
                    *                  vResult.Feedback = string.Format(response.ToString());
                    *                  vResult.IsValid = false;
                    *              }
                    *              else
                    *              {
                    *                  vResult.IsValid = true;
                    *                  listGetAvailablesSlots = AppoinmentService.GetAvailablesSlots(listCalendars[0].Id);
                    *                  if (listGetAvailablesSlots.Count > 0)
                    *                  {
                    *                      vResult.IsValid = true;
                    *                  }
                    *                  else
                    *                  {
                    *                      response.Append("There are'n t availables slots in this date, try it one more time , or write 'quit' for exit").ToString();
                    *                      vResult.Feedback = string.Format(response.ToString());
                    *                      vResult.IsValid = false;
                    *                  }
                    *              }//End else
                    *      }//End if GetServicesOtempues(unitId).Count > 0
                    *  }//End try
                    *  catch (Exception ex)
                    *      {
                    *      //throw new Exception("Here are the error: " + ex.Message);
                    *      await context.PostAsync($"Failed with message: {ex.Message}");
                    *      }
                    *  }
                    *  return vResult;
                    * })*/
                   .Field(new FieldReflector <BookForm>(nameof(CalendarId)).SetActive(InactiveField))
                   .Field(new FieldReflector <BookForm>(nameof(OrdinalSlot)).SetActive(InactiveField))
                   /*new source implementation 10/09/17 */
                   .Field(new FieldReflector <BookForm>(nameof(GeneralHour))
                          .SetType(null)
                          .SetDefine(async(state, value) =>
            {
                string date;
                string service;
                List <Service> listService;
                List <OTempus.Library.Class.Calendar> listCalendars;
                List <CalendarGetSlotsResults> listGetAvailablesSlots;
                if (!String.IsNullOrEmpty(state.StartDateAndTime) && !String.IsNullOrEmpty(state.Service) && !String.IsNullOrEmpty(state.Office))
                {
                    int unitId = Utilities.Util.GetUnitIdFromBotOption(state.Office);
                    string calendarId = Utilities.Util.GetCalendarIdFromBotOption(state.StartDateAndTime);
                    //asign the calendar id
                    state.CalendarId = calendarId;
                    string dateSelected = Utilities.Util.GetDateFromBotOption(state.StartDateAndTime);


                    try
                    {
                        listService = AppoinmentService.listServicesByName(state.Service, 1, "en", false, unitId);
                        //It find almost the time one record, but in the case that found two,
                        int serviceID = listService[0].Id;
                        listCalendars = new List <OTempus.Library.Class.Calendar>();
                        date = dateSelected; service = state.Service;
                        if (GetServicesOtempues(unitId).Count > 0)
                        {
                            //Service two and date...
                            listCalendars = GetCalendar(serviceID.ToString(), date);
                        }
                        //List<Appoinment> listAppoinments = await Services.AppoinmentService.GetAppoinments();
                        listGetAvailablesSlots = new List <CalendarGetSlotsResults>();
                        StringBuilder response = new StringBuilder();
                        response.Append("Not exists slots").ToString();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Here are the error: " + ex.Message);
                    }
                    date = dateSelected;
                    service = state.Service.ToString();
                    if (listCalendars.Count > 0)
                    {
                        listGetAvailablesSlots = AppoinmentService.GetAvailablesSlots(Convert.ToInt32(state.CalendarId));
                        int cont = 0;
                        foreach (OTempus.Library.Class.CalendarGetSlotsResults calendarSlots in listGetAvailablesSlots)
                        {
                            if (calendarSlots.Status.ToString() == "Vacant")
                            {
                                //I commented this line because I need to cut the message
                                // string data =calendarSlots.OrdinalNumber+".-"+ calendarSlots.DisplayStartTime.ToString() +"-"+calendarSlots.DisplayEndTime+"=>"+calendarSlots.Status;
                                string data = calendarSlots.OrdinalNumber + ".-" + calendarSlots.DisplayStartTime.ToString() + "-" + calendarSlots.DisplayEndTime;
                                string hour = Utilities.Util.GetHourFromStartDate(calendarSlots.DisplayStartTime.ToString());;
                                value
                                // .AddDescription(data, data).AddTerms(data, data);
                                .AddDescription(hour, hour).AddTerms(hour, hour);

                                cont++;
                            }
                        }
                        return await Task.FromResult(true);
                    }
                    else
                    {
                        return await Task.FromResult(false);
                    }
                }
                return await Task.FromResult(false);
            }))
                   /* New source implementation*/
                   .Field(new FieldReflector <BookForm>(nameof(Hour))
                          .SetType(null)
                          .SetDefine(async(state, value) =>
            {
                string date;
                string service;
                List <Service> listService;
                List <OTempus.Library.Class.Calendar> listCalendars;
                List <CalendarGetSlotsResults> listGetAvailablesSlots;
                if (!String.IsNullOrEmpty(state.StartDateAndTime) && !String.IsNullOrEmpty(state.Service) && !String.IsNullOrEmpty(state.Office) && !String.IsNullOrEmpty(state.GeneralHour))
                {
                    string generalHour = state.GeneralHour;
                    int unitId = Utilities.Util.GetUnitIdFromBotOption(state.Office);
                    try
                    {
                        listService = AppoinmentService.listServicesByName(state.Service, 1, "en", false, unitId);
                        //It find almost the time one record, but in the case that found two,
                        int serviceID = listService[0].Id;
                        listCalendars = new List <OTempus.Library.Class.Calendar>();
                        date = state.StartDateAndTime; service = state.Service;
                        if (GetServicesOtempues(unitId).Count > 0)
                        {
                            //Service two and date...
                            listCalendars = GetCalendar(serviceID.ToString(), date);
                        }
                        //List<Appoinment> listAppoinments = await Services.AppoinmentService.GetAppoinments();
                        listGetAvailablesSlots = new List <CalendarGetSlotsResults>();
                        StringBuilder response = new StringBuilder();
                        response.Append("Not exists slots").ToString();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Here are the error: " + ex.Message);
                    }
                    date = state.StartDateAndTime.ToString();
                    service = state.Service.ToString();
                    if (listCalendars.Count > 0)
                    {
                        listGetAvailablesSlots = AppoinmentService.GetAvailablesSlots(listCalendars[0].Id);
                        int cont = 0;
                        foreach (OTempus.Library.Class.CalendarGetSlotsResults calendarSlots in listGetAvailablesSlots)
                        {
                            string hour = Utilities.Util.GetHourFromStartDate(calendarSlots.DisplayStartTime.ToString());;

                            if (calendarSlots.Status.ToString() == "Vacant" && hour == generalHour)
                            {
                                //I commented this line because I need to cut the message
                                // string data =calendarSlots.OrdinalNumber+".-"+ calendarSlots.DisplayStartTime.ToString() +"-"+calendarSlots.DisplayEndTime+"=>"+calendarSlots.Status;
                                string data = calendarSlots.OrdinalNumber + ".-" + calendarSlots.DisplayStartTime.ToString() + "-" + calendarSlots.DisplayEndTime;
                                //assign the calendar id
                                //state.CalendarId = calendarSlots.CalendarId.ToString();
                                value
                                .AddDescription(data, data)
                                .AddTerms(data, data);
                                cont++;
                            }
                        }
                        return await Task.FromResult(true);
                    }
                    else
                    {
                        return await Task.FromResult(false);
                    }
                }
                return await Task.FromResult(false);
            }))

                   /* .Confirm("Are you selected the information: " +
                    * "\n* Office: {Office} " +
                    * "\n* Slot:  {Hour} " +
                    * "\n* Service:  {Service}? \n" +
                    * "(yes/no)") This lines are commented because, when the user select no, can crate inconsistence when user book the appointment (I try to solve by the best way)*/
                   .AddRemainingFields()
                   .Message("The process for create the appointment has been started!")
                   .OnCompletion(processOrder)
                   .Build());
        }