public async Task <TwilioResponse> SetMiles(TwilioRequest request)
        {
            var response = new TwilioResponse();

            BeginMenu(response);

            int miles;

            if (int.TryParse(request.Digits, out miles))
            {
                using (var db = dbFactory())
                {
                    var signin = db.SignIns.OrderByDescending(f => f.TimeIn).FirstOrDefault(f => f.MemberId == this.session.MemberId);
                    signin.Miles = miles;
                    await db.SaveChangesAsync();

                    this.config.GetPushHub <CallsHub>().updatedRoster(RosterController.GetRosterEntry(signin.Id, db), true);
                }
                response.SayVoice(Speeches.MilesUpdated);
            }

            await EndMenu(response, true);

            return(LogResponse(response));
        }
        public async Task <TwilioResponse> SetTimeOut(TwilioRequest request)
        {
            int minutes;
            var response = new TwilioResponse();

            // add prompt for miles
            response.BeginGather(new { timeout = 10, action = GetAction("SetMiles") });
            if (int.TryParse(request.Digits, out minutes))
            {
                using (var db = dbFactory())
                {
                    var signin = await GetMembersLatestSignin(db, this.session.MemberId);

                    signin.TimeOut = signin.TimeOut.Value.AddMinutes(minutes);
                    await db.SaveChangesAsync();

                    this.config.GetPushHub <CallsHub>().updatedRoster(RosterController.GetRosterEntry(signin.Id, db), true);
                    var sayDate = TimeUtils.GetMiltaryTimeVoiceText(signin.TimeOut.Value);
                    response.SayVoice(Speeches.SignedOutTemplate, this.session.MemberName, sayDate);
                }
            }
            response.SayVoice(Speeches.MilesPrompt);
            response.EndGather();

            return(LogResponse(response));
        }
        public async Task <TwilioResponse> SetSigninEvent(TwilioRequest request)
        {
            var response = new TwilioResponse();

            using (var db = dbFactory())
            {
                var signin = await GetMembersLatestSignin(db, session.MemberId);

                await RosterController.AssignInternal(signin, session.EventId, db, this.config);

                var name = CurrentEvents.Where(f => f.Id == session.EventId).Select(f => f.Name).SingleOrDefault();
                response.SayVoice(Speeches.ChangeEventTemplate, name);
            }

            response.Redirect(Url.Content("~/api/Voice/Menu") + session.ToQueryString());

            return(LogResponse(response));
        }
        public async Task <TwilioResponse> DoSignInOut(TwilioRequest request)
        {
            var response = new TwilioResponse();

            using (var db = dbFactory())
            {
                var signin = await db.SignIns.Where(f => f.MemberId == this.session.MemberId).OrderByDescending(f => f.TimeIn).FirstOrDefaultAsync();

                var call = await db.Calls.SingleAsync(f => f.CallId == request.CallSid);

                DateTimeOffset time    = TimeUtils.GetLocalDateTime(this.config);
                var            sayDate = TimeUtils.GetMiltaryTimeVoiceText(time);

                if (signin == null || signin.TimeOut.HasValue)
                {
                    if (this.session.IsSignedIn)
                    {
                        throw new InvalidOperationException("Tried to sign out when not signed in");
                    }

                    signin = new MemberSignIn
                    {
                        MemberId = this.session.MemberId,
                        isMember = true,
                        Name     = this.session.MemberName,
                        TimeIn   = time,
                        EventId  = (this.CurrentEvents.Count == 1) ? this.CurrentEvents[0].Id : this.session.EventId,
                    };

                    db.SignIns.Add(signin);
                    call.Actions.Add(new CallAction {
                        Call = call, CallId = call.Id, Time = signin.TimeIn, Action = "Signed in " + signin.Name
                    });
                    await db.SaveChangesAsync();

                    this.session.IsSignedIn = true;

                    if (this.CurrentEvents.Count == 0)
                    {
                        await RosterController.AssignInternal(signin, null, db, this.config);

                        BeginMenu(response);
                        response.SayVoice(Speeches.SignedInUnassignedTemplate, this.session.MemberName, sayDate);
                        await EndMenu(response);
                    }
                    else if (this.CurrentEvents.Count == 1)
                    {
                        await RosterController.AssignInternal(signin, this.CurrentEvents[0].Id, db, this.config);

                        BeginMenu(response);
                        response.SayVoice(Speeches.SignedInTemplate, this.CurrentEvents[0].Name, this.session.MemberName, sayDate);
                        await EndMenu(response);
                    }
                    else
                    {
                        await RosterController.AssignInternal(signin, null, db, this.config);

                        BuildSetEventMenu(response, string.Format(Speeches.SignedInUnassignedTemplate, this.session.MemberName, sayDate), Url.Content("~/api/voice/SetSigninEvent"));
                    }
                }
                else
                {
                    signin.TimeOut = time;
                    call.Actions.Add(new CallAction {
                        Call = call, CallId = call.Id, Time = time, Action = "Signed out " + this.session.MemberName
                    });
                    this.session.IsSignedIn = false;
                    await db.SaveChangesAsync();

                    this.config.GetPushHub <CallsHub>().updatedRoster(RosterController.GetRosterEntry(signin.Id, db), true);

                    // add prompt for timeout beyond right now
                    response.BeginGather(new { timeout = 10, action = GetAction("SetTimeOut") });
                    response.SayVoice(Speeches.SignedOutTemplate, this.session.MemberName, sayDate);
                    response.SayVoice(Speeches.TimeoutPrompt);
                    response.EndGather();
                }
            }

            return(LogResponse(response));
        }