private FieldReflector <TimeRegistrationModel> BuildHourTypeField(ExactOnlineConnector connector) { var reflector = new FieldReflector <TimeRegistrationModel>(nameof(TimeRegistrationModel.HourType)); reflector.SetType(null); reflector.SetDefine((state, field) => { var recentHourTypes = _cacheService.RetrieveForUser <TimeAndBillingRecentHourCostType[]>(key_hourTypes, _userId); if (recentHourTypes == null) { TimeRegistrationConnector timeConnector = new TimeRegistrationConnector(); recentHourTypes = timeConnector.GetRecentHourCostTypes(connector).ToArray(); _cacheService.CacheForUser(key_hourTypes, recentHourTypes, _userId); } foreach (var recentHourType in recentHourTypes) { field .AddDescription(recentHourType.ItemId.ToString(), recentHourType.ItemDescription) .AddTerms(recentHourType.ItemId.ToString(), recentHourType.ItemDescription); } return(Task.FromResult(true)); }); return(reflector); }
private FieldReflector <TimeRegistrationModel> BuildCustomerField(ExactOnlineConnector connector) { var reflector = new FieldReflector <TimeRegistrationModel>(nameof(TimeRegistrationModel.Customer)); reflector.SetType(null); reflector.SetDefine((state, field) => { var recentAccounts = _cacheService.RetrieveForUser <TimeAndBillingRecentAccount[]>(key_customers, _userId); if (recentAccounts == null) { TimeRegistrationConnector timeConnector = new TimeRegistrationConnector(); recentAccounts = timeConnector.GetRecentAccounts(connector).ToArray(); _cacheService.CacheForUser(key_customers, recentAccounts, _userId); } foreach (var recentAccount in recentAccounts) { field .AddDescription(recentAccount.AccountId.ToString(), recentAccount.AccountName) .AddTerms(recentAccount.AccountId.ToString(), recentAccount.AccountName); } return(Task.FromResult(true)); }); return(reflector); }
public async Task MissingHours(IDialogContext context, IAwaitable <IMessageActivity> activity, LuisResult result) { if (await VerifyExactOnlineAuthorization(context, activity, "")) { DateTime startDate; DateTime endDate; string weekString; var message = await activity; if (message.Text.ToLower().Contains("last week")) { weekString = "last week"; DateTimeUtils.GetThisWeek(DateTime.Now.AddDays(-7), out startDate, out endDate); } else { weekString = "this week"; DateTimeUtils.GetThisWeek(DateTime.Now, out startDate, out endDate); } await context.PostAsync($"Just a minute, going to check your hours for {weekString}..."); ExactOnlineConnector eolConnector = ExactOnlineHelper.GetConnector(); TimeRegistrationConnector connector = new TimeRegistrationConnector(); double bookedHours = await connector.GetBookedHours(eolConnector.EmployeeId, startDate, endDate, eolConnector); await context.PostAsync($"For {weekString} I found {bookedHours} hours booked."); context.Wait(MessageReceived); } }
private async Task <TimeRegistrationModel> TimeRegistrationCompleted(IBotContext context, TimeRegistrationModel model) { var message = "Booking your hours in Exact, just a sec..."; await context.PostAsync(message); ExactOnlineConnector connector = ExactOnlineHelper.GetConnector(); TimeRegistrationConnector timeConnector = new TimeRegistrationConnector(); Guid projectId = String.IsNullOrEmpty(model.Project) || model.Project == "none" ? Guid.Empty : new Guid(model.Project); Guid customerId = String.IsNullOrEmpty(model.Customer) ? Guid.Empty : new Guid(model.Customer); Guid hourTypeId = String.IsNullOrEmpty(model.HourType) ? Guid.Empty : new Guid(model.HourType); try { // the user will have booked time for either this week or for a specific date if (!model.ThisWeek) { timeConnector.BookHours(connector.EmployeeId, customerId, hourTypeId, projectId, model.Date, model.Amount, connector); } else { // if the hours were booked for the entire will, there will be 5 numbers in the string that need to be split // out and entered for each day of the week individually int dayOfWeek = DateTimeUtils.GetISODayOfWeek(DateTime.Now); DateTime currentDay = DateTime.Now.AddDays((dayOfWeek - 1) * -1); string[] hours = model.AmountPerDay.Trim().Split(' '); for (int i = 0; i < 5; i++) { double amount = Double.Parse(hours[i]); if (amount > 0) { timeConnector.BookHours(connector.EmployeeId, customerId, hourTypeId, projectId, currentDay, amount, connector); } currentDay = currentDay.AddDays(1); } } } catch (RequestFailedException ex) { await context.PostAsync($"Hmm, that didn't work. The request failed, it returned the following:"); await context.PostAsync($"\"{ ex.Message}\""); await context.PostAsync($"Sorry about that. Please try again or notify my maker."); return(null); } await context.PostAsync("All set! Anything else I can do for you?"); return(model); }
public async Task SubmitHours(IDialogContext context, IAwaitable <IMessageActivity> activity, LuisResult result) { if (await VerifyExactOnlineAuthorization(context, activity, "")) { await context.PostAsync($"Let me check whether you're all set..."); ExactOnlineConnector eolConnector = ExactOnlineHelper.GetConnector(); TimeRegistrationConnector connector = new TimeRegistrationConnector(); DateTime startDate, endDate; DateTimeUtils.GetThisWeek(DateTime.Now, out startDate, out endDate); double bookedHours = await connector.GetBookedHours(eolConnector.EmployeeId, startDate, endDate, eolConnector); ConfirmDialog.Text = $"You've registered a total number of {0} hours for this week. Do you want me to submit those?"; var confirmationDialog = new FormDialog <ConfirmModel>(new ConfirmModel(), ConfirmDialog.BuildForm, FormOptions.PromptInStart); context.Call(confirmationDialog, this.ResumeAfterSubmitHoursDialog); } }
private async Task <double?> GetBookedHours(AuthenticationResult authenticationResult) { try { ExactOnlineConnector connector = new ExactOnlineConnector(authenticationResult.AccessToken); DateTime startDate, endDate; DateTimeUtils.GetThisWeek(DateTime.Now, out startDate, out endDate); TimeRegistrationConnector timeConnector = new TimeRegistrationConnector(); double bookedHours = await timeConnector.GetBookedHours(connector.EmployeeId, startDate, endDate, connector); return(bookedHours); } catch (Exception ex) { return(null); } }
private FieldReflector <TimeRegistrationModel> BuildProjectField(ExactOnlineConnector connector) { var reflector = new FieldReflector <TimeRegistrationModel>(nameof(TimeRegistrationModel.Project)); reflector.SetType(null); reflector.SetDefine((state, field) => { Guid?customerId = !String.IsNullOrEmpty(state.Customer) ? Guid.Parse(state.Customer) : (Guid?)null; if (customerId != null) { string key = key_hours + customerId.ToString(); var recentProjects = _cacheService.RetrieveForUser <RecentHours[]>(key, _userId); if (recentProjects == null) { TimeRegistrationConnector timeConnector = new TimeRegistrationConnector(); recentProjects = timeConnector.GetRecentProjects(connector, customerId).ToArray(); _cacheService.CacheForUser(key, recentProjects, _userId); } foreach (var recentProject in recentProjects) { field .AddDescription(recentProject.ProjectId.ToString(), recentProject.ProjectDescription) .AddTerms(recentProject.ProjectId.ToString(), recentProject.ProjectDescription); } // if there's only one option to select; select it! if (recentProjects.Length == 1) { state.Project = recentProjects.First().ProjectId.ToString(); return(Task.FromResult(false)); } } return(Task.FromResult(true)); }); return(reflector); }
private async Task ResumeAfterSubmitHoursDialog(IDialogContext context, IAwaitable <ConfirmModel> result) { ConfirmModel message = await result; if (message.Confirmation) { DateTime startDate, endDate; DateTimeUtils.GetThisWeek(DateTime.Now, out startDate, out endDate); ExactOnlineConnector eolConnector = ExactOnlineHelper.GetConnector(); TimeRegistrationConnector timeConnector = new TimeRegistrationConnector(); timeConnector.SubmitHours(eolConnector.EmployeeId, startDate, endDate, eolConnector); await context.PostAsync($"Thanks, I've closed your timesheet for this week. Have a nice weekend!"); } else { await context.PostAsync($"Ok. Just give me a nudge when you're ready."); } context.Wait(MessageReceived); }